jquery – 如何为Handsontable创建动态列?

前端之家收集整理的这篇文章主要介绍了jquery – 如何为Handsontable创建动态列?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_403_1@我正在使用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",{ 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,50,40,60],//can also be a number or a function
      contextMenu: false,});

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

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初学者,所以请告诉我,如果我有任何错误,谢谢!

解决方法

我自己解决了这个问题,函数不能直接在列定义中使用,但允许变量,所以下面的代码有效:
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,// ...
原文链接:https://www.f2er.com/jquery/181480.html

猜你在找的jQuery相关文章