在Handsontable中级联下拉

时间:2015-07-27 07:14:27

标签: javascript handsontable cascadingdropdown

我正在尝试制作一个下拉式列,而条件由另一个单元格的内容决定。

列设置是由一个函数构建的,当它应该是一个级联下拉列表时,源代码也是一个函数。

一切正常,双击单元格时调用了该函数。该函数正在运行并返回正确的数组。但是下拉不开放。

JS的简短版本:

data: Object.keys(data[0])[i],
type: "dropdown",
source: function getDropdownList() {
          // function build the array
          return dropdownOptions;
        }

JS的长版本:

var myData = Handsontable.helper.createSpreadsheetData(5, 5);
var container = document.getElementById('table1');
var settings1 = {
   data: myData,
   columns: buildTypeObject()
}

var table1 = new Handsontable(container, settings1);

function buildTypeObject() {
  var typeObject = [];
  for (var i = 0; i < myData.length; i++) {
    if (i=1) {
        typeObject[i] = {
            data: ("column " + i),
            type: "dropdown",
            source: function getDropdownList() {
                var dropdownOptions = [];
                var selectedRow = table1.getSelected()[0];
                var selectedColumn = table1.getSelected()[1];
                var previousCell = table1.getDataAtCell(selectedRow, selectedColumn-1);
                if (previousCell == "A1") {
                    dropdownOptions = ["AB1","BB1"];
                } else {
                    dropdownOptions = ["ZZ1", "ZZ2", "ZZ3", "ZZ4"];                        
                }
                console.log(dropdownOptions);
                return dropdownOptions;            
            }
        }                
    } else {
        typeObject[i] = {
            data: ("column " + i) 
        }            
    }
 };
 return typeObject;
};

2 个答案:

答案 0 :(得分:1)

我已设法使用setCellMeta(row, col, key, value)方法解决此问题。

所以现在JS看起来像这样:

var myData = Handsontable.helper.createSpreadsheetData(5, 5);
var container = document.getElementById('table1');
var settings1 = {
   data: myData,
   columns: buildTypeObject()
}

var table1 = new Handsontable(container, settings1);

function buildTypeObject() {
  var typeObject = [];
  for (var i = 0; i < myData.length; i++) {
    if (i=1) {
        typeObject[i] = {
            data: ("column " + i),
            type: "dropdown",
            source: function getDropdownList() {
                var dropdownOptions = [];
                var selectedRow = table1.getSelected()[0];
                var selectedColumn = table1.getSelected()[1];
                var previousCell = table1.getDataAtCell(selectedRow, selectedColumn-1);
                if (previousCell == "A1") {
                    dropdownOptions = ["AB1","BB1"];
                } else {
                    dropdownOptions = ["ZZ1", "ZZ2", "ZZ3", "ZZ4"];                        
                }
                console.log(dropdownOptions);
                //return dropdownOptions;
                setCellMeta(selectedRow, selectedCol, "source", dropdownOptions);            
            }
        }                
    } else {
        typeObject[i] = {
            data: ("column " + i) 
        }            
    }

唯一的问题是下拉列表在第一次点击时没有打开,它会在第二次点击时打开,或者在我编辑单元格时输入(输入任何字符或删除)

答案 1 :(得分:0)

我希望以下代码可以根据我的发现帮助某人。

select2-editor暴露回调onBeginEditing

$("#hot").handsontable({           
        onBeginEditing: function () {
            var activeEditor = instance.getActiveEditor();
            var selectedRow = activeEditor.cellProperties.row
            var selectedColumn = activeEditor.cellProperties.col;
            if ( selectedColumn == 3) {
                var tester = instance.getDataAtCell(selectedRow, 14);
                var filterlist = [];
                var optionsList = activeEditor.options.data;
                for (var index = 0; index < optionsList.length; index++) {
                    if (tester == optionsList[index].tester) {
                        filterlist.push(optionsList[index]);
                    }
                }
                activeEditor.options.data = filterlist;
            }
        });