2013-04-24 61 views
3

我與Handsontable合作,創建一個Web網格,可以複製/粘貼腹板之間和Excel,我試着用下面的代碼,它工作正常:如何爲Handsontable創建動態列?

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初學者,所以請告訴我,如果我有任何錯誤,謝謝!

回答

11

由我自己解決了這個問題,功能不能在列定義直接使用,而是變量是允許的,所以下面的代碼工作:

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

感謝您分享您的解決方案。你能將你的答案標記爲接受嗎? – 2014-05-27 09:07:42

+0

@GôTô,作爲問題的所有者,您必須選擇正確的答案(在這種情況下,只有一個,而且是正確的答案)。而不是相反。否則,「所有人」可以選擇他們的答案作爲正確的答案:)。 – 2016-06-23 05:26:04

+0

@HelmutGranda本答案由提問者提供........... – 2016-06-23 06:50:10