具有虚假值 (0) 的 Handsontable 7.4 下拉单元格显示占位符
Handsontable 7.4 dropdown cell with falsy value (0) shows placeholder
我正在寻找一种在下拉列表中显示数值 0 的方法,该下拉列表还包含单元格为空时的占位符文本。目前,如果选择 0,占位符文本会显示出来。我希望有一个内置选项,如果可以的话,我想避免将数字转换为字符串并返回(这会破坏我当前的验证方案)。以下示例修改自 HandsOnTable 下拉文档。 'Chassis Color' 列包含问题。
jsfiddle:
https://jsfiddle.net/y3pL0vjq/
片段:
function getCarData() {
return [
["Tesla", 2017, "black", "black"],
["Nissan", 2018, "blue", "blue"],
["Chrysler", 2019, "yellow", "black"],
["Volvo", 2020, "white", "gray"]
];
}
var
container = document.getElementById('example1'),
hot;
hot = new Handsontable(container, {
data: getCarData(),
colHeaders: ['Car', 'Year', 'Chassis color', 'Bumper color'],
columns: [
{},
{type: 'numeric'},
{
type: 'dropdown',
placeholder: "blah",
source: [null, 0, 1, 2, 3]
},
{
type: 'dropdown',
source: ['yellow', 'red', 'orange', 'green', 'blue', 'gray', 'black', 'white']
}
]
});
我发现处理这个数字下拉列表的最佳方法是省略“类型”属性并将编辑器和验证器指定为“自动完成”。然后制作自定义渲染器以将 NumericRenderer 功能与自动完成下拉列表合并。
要最终确定本机下拉功能的相似外观,然后添加“strict: true”和“filter: false”,如下拉文档中所述。
Internally, cell {type: "dropdown"} is equivalent to cell {type: "autocomplete", strict: true, filter: false}.
Dropdown Docs
示例:
function getCarData() {
return [
["Tesla", 2017, null, "black"],
["Nissan", 2018, 0, "blue"],
["Chrysler", 2019, 1, "black"],
["Volvo", 2020, 2, "gray"]
];
}
var
container = document.getElementById('example1'),
hot;
function myRenderer(instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.NumericRenderer.apply(this, arguments);
td.innerHTML += '<div class="htAutocompleteArrow">▼</div>'
}
hot = new Handsontable(container, {
data: getCarData(),
colHeaders: ['Car', 'Year', 'Chassis color', 'Bumper color'],
columns: [
{},
{type: 'numeric'},
{
editor: 'autocomplete',
validator: 'autocomplete',
renderer: myRenderer,
strict: true,
filter: false,
placeholder: "blah",
source: [null, 0, 1, 2, 3]
},
{
type: 'dropdown',
source: ['yellow', 'red', 'orange', 'green', 'blue', 'gray', 'black', 'white']
}
]
});
我正在寻找一种在下拉列表中显示数值 0 的方法,该下拉列表还包含单元格为空时的占位符文本。目前,如果选择 0,占位符文本会显示出来。我希望有一个内置选项,如果可以的话,我想避免将数字转换为字符串并返回(这会破坏我当前的验证方案)。以下示例修改自 HandsOnTable 下拉文档。 'Chassis Color' 列包含问题。
jsfiddle: https://jsfiddle.net/y3pL0vjq/
片段:
function getCarData() {
return [
["Tesla", 2017, "black", "black"],
["Nissan", 2018, "blue", "blue"],
["Chrysler", 2019, "yellow", "black"],
["Volvo", 2020, "white", "gray"]
];
}
var
container = document.getElementById('example1'),
hot;
hot = new Handsontable(container, {
data: getCarData(),
colHeaders: ['Car', 'Year', 'Chassis color', 'Bumper color'],
columns: [
{},
{type: 'numeric'},
{
type: 'dropdown',
placeholder: "blah",
source: [null, 0, 1, 2, 3]
},
{
type: 'dropdown',
source: ['yellow', 'red', 'orange', 'green', 'blue', 'gray', 'black', 'white']
}
]
});
我发现处理这个数字下拉列表的最佳方法是省略“类型”属性并将编辑器和验证器指定为“自动完成”。然后制作自定义渲染器以将 NumericRenderer 功能与自动完成下拉列表合并。
要最终确定本机下拉功能的相似外观,然后添加“strict: true”和“filter: false”,如下拉文档中所述。
Internally, cell {type: "dropdown"} is equivalent to cell {type: "autocomplete", strict: true, filter: false}. Dropdown Docs
示例:
function getCarData() {
return [
["Tesla", 2017, null, "black"],
["Nissan", 2018, 0, "blue"],
["Chrysler", 2019, 1, "black"],
["Volvo", 2020, 2, "gray"]
];
}
var
container = document.getElementById('example1'),
hot;
function myRenderer(instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.NumericRenderer.apply(this, arguments);
td.innerHTML += '<div class="htAutocompleteArrow">▼</div>'
}
hot = new Handsontable(container, {
data: getCarData(),
colHeaders: ['Car', 'Year', 'Chassis color', 'Bumper color'],
columns: [
{},
{type: 'numeric'},
{
editor: 'autocomplete',
validator: 'autocomplete',
renderer: myRenderer,
strict: true,
filter: false,
placeholder: "blah",
source: [null, 0, 1, 2, 3]
},
{
type: 'dropdown',
source: ['yellow', 'red', 'orange', 'green', 'blue', 'gray', 'black', 'white']
}
]
});