如何创建动态列Handsontable?(How to create dynamic columns

2019-09-01 00:01发布

我与Handsontable合作,创建一个Web网格,可以网络之间的复制/粘贴和excel,我试着用下面的代码,它工作正常:

  var first = true;
  var exampleGrid = $("#exampleGrid");
  exampleGrid.handsontable({
      rowHeaders: true,
      colHeaders: true,
      stretchH: 'all',
      minSpareCols: 0,
      minSpareRows: 0,
      height: 600,
      columns: [
      { data: "Id", title: "ID", type: "text" }, //'text' is default, you don't actually have to declare it
      { data: "Name", title: "Name", type: "text" },
      { data: "DisplayColor",
      title: "Display Color",
      type: 'autocomplete',
      source: ["yellow", "red", "orange", "green", "blue", "gray", "black", "white"]
      },
      { data: "Description", title: "Description", type: 'text' },
      { data: "IsDeleted", title: "Is Deleted", type: 'checkbox' }
      ],
      colWidths: [400, 100, 60, 100, 50, 40, 40, 60], //can also be a number or a function
      contextMenu: false,
  });

现在我需要创建动态网页列格,我试图替换以下功能列的列表,但它不工作:

    columns:
        function () {
            var cols = [];
            for (var i = 0; i < 1; i++) {
                var col = new Object();

                col.data = "Name";
                col.title = "Name" + i.toString();
                col.type = "text";
                cols[i] = col;
            }
            return cols;
        },

是否有可能创造Handsontable电网动态列? 怎么办呢?

我是一个JavaScript新手,那么请告诉我,如果有我犯任何错误,谢谢!

Answer 1:

由我自己解决了这个问题,功能不能在列定义直接使用,而是变量是允许的,所以下面的代码工作:

var dynamicColumns = [];
for (var i = 0; i < 366; i++) {
    var col = new Object();
    col.data = "Name";
    col.title = "Name " + i.toString();
    col.type = "text";
    dynamicColumns.push(col);
}

skillGrid.handsontable({
    // ...
    columns: dynamicColumns,
    // ...


文章来源: How to create dynamic columns for Handsontable?