【问题标题】:Javascript Assign function does not workJavascript分配功能不起作用
【发布时间】:2018-05-21 07:47:30
【问题描述】:

我有一个显示两个数据表的应用程序。两个表都使用按钮扩展来支持列可见性对话框。 第二个表的第一列没有标题。在列可见性对话框中,按钮应该有一个标题,这就是我覆盖 colvis 扩展的 columnText 函数的原因

var buttons= [{
    extend: 'colvis',
    postfixButtons: ['colvisRestore'],
    collectionLayout: 'fixed three-column'
}];
if("secondTable" === tableName) {
    buttons[0]['columnText'] = function (dt, idx, title) {
        if (idx === 0) {
            return "firstColumnTitle";
        } else {
            return title;
        }
    };
    //add default buttons to buttons
    buttons[0]['buttons'] = [{extend: 'columnsToggle'}];
    //add one extra button to the collection
    buttons[0]['buttons'].push([
    {
        extend: 'columnToggle',
        text: 'HR',
        columns: [1, 2]
    }]);
}
DataTable({
    rowId: 'id',
    ajax: 'content.do',
    buttons: buttons
});

我希望仅对第二个表调用 columntext 函数

如果我添加按钮,则不再调用 columnText 函数。为什么?

【问题讨论】:

  • "secondTable" === tableName 永远不是true
  • 如果条件为真,这应该可以工作。在if("secondTable" === tableName) { ... } 之后,您可以控制台buttons 变量并检查结果。
  • "secondTable" === 表名是真的。根据 chrome 调试器(console.log 显示相同),无论是在创建对象时分配函数还是在之后更改值,它看起来都是一样的。只是函数位置引用了不同的行号。
  • 我猜你使用的是旧版本的 ColVis,columnText 是在 v1.3.0 中引入的(即最近)...
  • 或者执行代码的顺序可能和你这里发的不一样。

标签: javascript datatables


【解决方案1】:

感谢@davidkonrad 和@Adelin 以上的 cmets。如果找到原因。

我需要将 columnText 函数分配给 colvis 扩展,而不是将其分配给里面的 columnsToggle 扩展。

if("secondTable" === tableName) {
  //add default buttons to buttons
  buttons[0]['buttons'] = [{
    extend: 'columnsToggle',
    columnText: function (dt, idx, title) {
    if (idx === 0) {
      return "firstColumnTitle";
    } else {
      return title;
    }
  }}];
  //add one extra button to the collection
  buttons[0]['buttons'].push([
  {
    extend: 'columnToggle',
    text: 'HR',
    columns: [1, 2]
  }]);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-30
    • 2014-03-05
    • 2017-05-29
    • 2014-04-18
    相关资源
    最近更新 更多