【问题标题】:Get column headers from custom dropdowns从自定义下拉列表中获取列标题
【发布时间】:2015-07-14 01:55:47
【问题描述】:

我有一个带有一些自定义下拉列表的标题的 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,或多或少类似于我的实现方式:Fiddle

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

这是他们页面上示例的链接:Handsontable custom Headers

【问题讨论】:

    标签: jquery handsontable


    【解决方案1】:

    问题是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 的定义一样。我建议你只定义数组,而不是那些长的 switch case。

    【讨论】:

    • 正如我的代码一样, colLocs 未定义意味着右侧也未定义。做你的 forEach 不会导致上述内容保持不变。感谢您提供有关清理代码的提示。
    • 如果是这种情况,右侧会抛出异常,但没关系。不要做colLocs = $("container2").handsontable("getColHeader");,而是做hot2.getColHeader(),告诉我们你从中得到了什么
    • 我实际上也尝试过...这是控制台错误我得到“未捕获的类型错误:无法读取未定义的属性'handsontable'”
    • 你能发布一个 jsFiddle,因为那没有意义。如果您的hot2 变量已定义,那么hot2.getColHeader() 将不会通过handsontable 未定义错误
    • 如果我从 addEventListener 函数中取出所有内容,那么我会收到一条错误消息:“Uncaught TypeError: Cannot read property 'insertBefore' of null”:hot2 = new Handsontable(container2, {
    猜你喜欢
    • 2014-12-21
    • 2017-03-03
    • 1970-01-01
    • 2011-04-19
    • 2015-10-22
    • 2018-06-21
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多