如何为Handsontable创建动态列? [英] How to create dynamic columns for Handsontable?

查看:715
本文介绍了如何为Handsontable创建动态列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与Handsontable一起创建一个可以在web和excel之间复制/粘贴的Web网格,我尝试使用下面的代码,并且效果很好:

I'm working with Handsontable to create a web grid that can copy / paste between web and excel, I tried with code below and it works fine:

  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,
  });

现在我需要创建带有动态列的Web网格,我尝试用下面的函数替换列列表,但是它不起作用:

Now I need create web grid with dynamic columns, I tried replace the column list with function below, but it does not works:

    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网格中创建动态列?以及如何做?

Is it possible to create dynamic columns in Handsontable grid? and how to do it?

我是JavaScript初学者,所以请告诉我是否有任何错误,谢谢!

I'm a JavaScript beginner, so please tell me if there is any error I made, thanks!

推荐答案

我自己解决了此问题,函数不能直接在列定义中使用,但允许使用变量,因此下面的代码可以工作:

Resolved this problem by myself, function can not be used in column definition directly, but variable is allowed, so code below works:

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,
    // ...

这篇关于如何为Handsontable创建动态列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆