gpt4 book ai didi

javascript - 使用 Handsontable-in-handsontable,如何仅从一列返回值?

转载 作者:行者123 更新时间:2023-11-28 06:47:26 24 4
gpt4 key购买 nike

我正在尝试在 Handsontable 网格内使用“Handsontable”编辑器。我希望用户能够从“下拉”类型菜单中进行选择,但该菜单显示多列相关数据(用户正在选择一个 ID 号,我希望能够显示相关的姓名和年龄,例如实例)。我可以正常工作,但用户可以单击弹出编辑器中的/any/单元格,它将返回该单元格中的任何内容。在“ID”单元格上它效果很好,但是当我只需要 ID 列中的值时,我可以轻松地返回“姓名”或“年龄”。

关于如何强制返回值/仅/来自一列有什么想法吗?

http://jsfiddle.net/sq9xs5a7/

document.addEventListener("DOMContentLoaded", function() {

function getCarData() {
return [
["Nissan", 2013, "black", "black"],
["Nissan", 2014, "blue", "blue"],
["Chrysler", 2015, "yellow", "black"],
["Volvo", 2016, "white", "gray"]
];
}

var
carData = getCarData(),
container = document.getElementById('example1'),
manufacturerData,
colors,
color,
colorData = [],
hot;

manufacturerData = [
{name: 'BMW', country: 'Germany', owner: 'Bayerische Motoren Werke AG'},
{name: 'Chrysler', country: 'USA', owner: 'Chrysler Group LLC'},
{name: 'Nissan', country: 'Japan', owner: 'Nissan Motor Company Ltd'},
{name: 'Suzuki', country: 'Japan', owner: 'Suzuki Motor Corporation'},
{name: 'Toyota', country: 'Japan', owner: 'Toyota Motor Corporation'},
{name: 'Volvo', country: 'Sweden', owner: 'Zhejiang Geely Holding Group'}
];
colors = ['yellow', 'red', 'orange', 'green', 'blue', 'gray', 'black', 'white'];

while (color = colors.shift()) {
colorData.push([
[color]
]);
}

hot = new Handsontable(container, {
data: carData,
colHeaders: ['Car', 'Year', 'Chassis color', 'Bumper color'],
columns: [
{
type: 'handsontable',
handsontable: {
colHeaders: ['Marque', 'Country', 'Parent company'],
data: manufacturerData
}
},
{type: 'numeric'},
{
type: 'handsontable',
handsontable: {
colHeaders: false,
data: colorData
}
},
{
type: 'handsontable',
handsontable: {
colHeaders: false,
data: colorData
}
}
]
});

function bindDumpButton() {
if (typeof Handsontable === "undefined") {
return;
}

Handsontable.Dom.addEvent(document.body, 'click', function (e) {

var element = e.target || e.srcElement;

if (element.nodeName == "BUTTON" && element.name == 'dump') {
var name = element.getAttribute('data-dump');
var instance = element.getAttribute('data-instance');
var hot = window[instance];
console.log('data of ' + name, hot.getData());
}
});
}
bindDumpButton();

});

这是我正在做的事情的一个很好的例子(来自 HoT 帮助)。在这里,它的工作原理和我的一模一样。当您单击“汽车”字段时,您可以选择“德国”,而我实际上只需要其中第一列中的值。

想法?

最佳答案

您可以向 HOT-in-HOT 添加 getValue() 方法,如下所示...

document.addEventListener("DOMContentLoaded", function() {

function getCarData() {
return [
["Nissan", 2013, "black", "black"],
["Nissan", 2014, "blue", "blue"],
["Chrysler", 2015, "yellow", "black"],
["Volvo", 2016, "white", "gray"]
];
}

var
carData = getCarData(),
container = document.getElementById('example1'),
manufacturerData,
colors,
color,
colorData = [],
hot;

manufacturerData = [
{name: 'BMW', country: 'Germany', owner: 'Bayerische Motoren Werke AG'},
{name: 'Chrysler', country: 'USA', owner: 'Chrysler Group LLC'},
{name: 'Nissan', country: 'Japan', owner: 'Nissan Motor Company Ltd'},
{name: 'Suzuki', country: 'Japan', owner: 'Suzuki Motor Corporation'},
{name: 'Toyota', country: 'Japan', owner: 'Toyota Motor Corporation'},
{name: 'Volvo', country: 'Sweden', owner: 'Zhejiang Geely Holding Group'}
];
colors = ['yellow', 'red', 'orange', 'green', 'blue', 'gray', 'black', 'white'];

while (color = colors.shift()) {
colorData.push([
[color]
]);
}

hot = new Handsontable(container, {
data: carData,
colHeaders: ['Car', 'Year', 'Chassis color', 'Bumper color'],
columns: [
{
type: 'handsontable',
handsontable: {
colHeaders: ['Marque', 'Country', 'Parent company'],
data: manufacturerData,
getValue: function() {
var selection = this.getSelected();

// Get always manufacture name of clicked row
return this.getSourceDataAtRow(selection[0]).name;
}
}
},
{type: 'numeric'},
{
type: 'handsontable',
handsontable: {
colHeaders: false,
data: colorData
}
},
{
type: 'handsontable',
handsontable: {
colHeaders: false,
data: colorData
}
}
],

});

});

关于javascript - 使用 Handsontable-in-handsontable,如何仅从一列返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33290150/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com