首页 > 解决方案 > 如何根据 Tabulator 中的选定选项卡设置列

问题描述

我有几个选项卡,其中每个选项卡都有数据表。我正在尝试使用制表符并基于活动选项卡我正在尝试设置列。

我使用setColumns了方法,但出现错误: Cannot read property 'forEach' of undefined

请在jsfiddle其中找到我的代码的链接:



  var table = new Tabulator("#example-table", {
    layout: "fitColumns",
    //data : tabledata,
    autoColumns: true,

  });
  if (this.activeTabName == "Role Card") {
    let columns: [{
        title: "Name",
        field: "name",
        sorter: "string",
        width: 200
      },
      {
        title: "Progress",
        field: "progress",
        sorter: "number",
        formatter: "progress"
      },
      {
        title: "Gender",
        field: "gender",
        sorter: "string"
      },
      {
        title: "Rating",
        field: "rating",
        formatter: "star",
        align: "center",
        width: 100
      },
      {
        title: "Favourite Color",
        field: "col",
        sorter: "string"
      },
    ]
    table.setColumns(columns)
  }

https://jsfiddle.net/qc9r8t4p/

请帮助我实现这一目标。

标签: javascriptangulardatatablestabulator

解决方案


您的代码中存在错误,导致将变量传递给 setColumns 函数时未定义

您将值分配给变量的位置

 let columns: [{

你有一个冒号而不是等号,它应该是:

 let columns = [{

推荐阅读