在handsontable中动态设置下拉单元格的数据

Dynamically setting data of dropdown cell in handsontable

您好,这是我开始使用 Handsontable,我正在尝试实现一个功能,其中一个单元格的下拉值取决于另一个单元格的值。 在单元格 1 的更改事件中,我想将新数据源加载到第 2 行,这是一个下拉单元格。

当用户在酒店单元格中选择一个值时,将进行 ajax 调用以查找房间类型。我希望房间类型在与已更改酒店字段相邻的房间类型单元格中可用。

请帮我解决这个问题。 谢谢

您可能需要查看 afterChange 以触发单元格 1 中的更改。
然后就可以用setCellMeta更新2号单元格的数据源了
最后,看一下 autocomplete with ajax source,因为下拉菜单是基于自动完成的,所以它的工作方式是一样的。

您的代码将如下所示:

var myTable = new Handsontable($(...), {
    ...,
    afterChange: function (change, source) {
        //choose the source you want to trigger, accordingly to the doc
        if((source == 'edit' || source == 'autofill' || source == 'paste'))
        {
            /*if you have multiples lines in your handsontable, then 
              the change array has one line per line in your table */
            for(var i = 0 ; i < change.length ; i++) {
                // I suppose here cell 2 is in row 2 (second argument)
                myTable.setCellMeta(
                     change[i][0],
                     2,
                     'source',
                     function(query,process) {
                         $.ajax({...})
                     }
                 )
            }
        }