从自定义下拉列表中获取列标题

时间:2015-07-09 19:53:13

标签: jquery handsontable

我有一个Handsontable,其中一些标题是自定义下拉列表。

这是他们的样子:

var txt= "<select id='Value_0'><option value='None'>N/A</option><option value='Name'>Name</option><option value='Long/E'>N/Lat</option><option value='Lat/N'>E/Long</option><option value='State'>State</option><option value='Zone'>Zone</option><option value='Hem'>Hem</option></select>";

hot2 = new Handsontable(container2, {
    data:d,
    columns: [
      {renderer: customRenderer},
      {renderer: customRenderer},
      {renderer: customRenderer},
      {renderer: customRenderer},
      {renderer: customRenderer},
      {renderer: customRenderer},
      {renderer: customRenderer},
      {renderer: customRenderer}
    ],
  colHeaders:[txt,txt,txt,txt,txt,txt,txt], 
  rowHeaders: true,
  minSpareCols: 1,
  //always keep at least 1 spare row at the right
  minSpareRows: 20,
  //always keep at least 1 spare row at the bottom,
  contextMenu: true,
  columnSorting: true,
  search: true
});

我想要做的是获取列标题列表(最好是在数组中),然后查看下拉列表的选择顺序。这是代码:

colLocs = $("container2").handsontable("getColHeader");

LatColumn = colLocs.indexOf("N/Lat");  //Yin
LongColumn = colLocs.indexOf("E/Long"); //Xin
ZoneColumn = colLocs.indexOf("Zone"); //Zone
StateColumn = colLocs.indexOf("State"); //Zone 

我现在得到的是colLocs未定义。

对此有任何帮助将不胜感激。感谢

更新

小提琴,或多或少像我实施它的方式:Fiddle

我认为问题是,为了使这个设置工作,我需要将整个表放在一个事件监听器中,它将它与其他函数调用隔离开来。因此,当函数尝试读取标题时,它无法访问它。

这是其网页上示例的链接:Handsontable custom Headers

1 个答案:

答案 0 :(得分:0)

嗯,getColHeader返回一个标题数组。如果你看看你的标题是什么样的,那就是整个HTML字符串。执行array.indexOf会找到数组元素的完美匹配。例如,

["aa","a"].indexOf("a");

将返回1,而不是0,因为第一次显示元素"a"是数组的第二个元素。所以,你想要的是在每个元素上indexOf询问forEach循环。

colLocs = $("container2").handsontable("getColHeader");

colLocs.forEach(function(header, index) {
    var latIndex = header.indexOf("N/Lat");
    if (latIndex > -1) {LatColumn = index}; // set it to the index of the for loop
    ...
}

请注意,这是凌乱的代码,因为您对colHeaders的定义也是如此。我建议您不要使用那些长开关情况,而只是定义数组。