1

我正在寻找一种在下拉列表中显示数值 0 的方法,该下拉列表还包括单元格为空时的占位符文本。目前,如果选择 0,则占位符文本会显示出来。我希望有一个内置选项,如果可以的话,我想避免将数字转换为字符串并返回(这会破坏我当前的验证方案)。以下示例是根据 HandsOnTable 下拉文档修改的。“机箱颜色”列包含问题。

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']
      }
    ]
  });
4

1 回答 1

0

我发现处理此数字下拉列表的最佳方法是省略“类型”属性并将编辑器和验证器指定为“自动完成”。然后制作一个自定义渲染器以将 NumericRenderer 功能与自动完成下拉列表合并。

为了最终确定与本机下拉功能相似的外观和感觉,然后添加“strict: true”和“filter: false”,如下拉文档中所述。

在内部,cell {type: "dropdown"} 等价于 cell {type: "autocomplete", strict: true, filter: false}。 下拉文档

例子:

  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">▼&lt;/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']
      }
    ]
  });
于 2021-03-10T17:46:18.220 回答