2012-04-19 72 views
0

我想使用我碰到的jQuery擴展(handsontable)。我有沒有問題,在創建表

var spreadsheet = $("#dataTable").handsontable({ 
      rows: 3, 
      cols: 15, 
      minSpareRows: 2 
}); 

但是我創造我想要調用各種輔助功能我看到在JavaScript的Handsontable對象申報表後。問題是擴展似乎返回this.each(function() { ... });,我不明白我如何可以從這訪問下層Handsontable對象。爲擴展名的JS可以發現here,我把一個小的演示一起以下鏈接

http://jsfiddle.net/7JTG2/7/

上,你可以看到,我想獲得的一個細胞的數據,當我點擊一個按鈕。

+0

看來你的'spreadsheet'變量沒有'grid'成員(未定義)。 – 2012-04-19 18:17:19

+0

我將不得不建議不要使用此功能。只要看看http://datatables.net/,不要回頭。 – Ohgodwhy 2012-04-19 18:23:43

+0

@Ohgodwhy我只想要一個簡單的可編輯網格,而不是多餘的額外功能。這似乎提供了。伊蘭我知道這一點,jsFiddle是我想做的,而不是我能做的。 – Danny 2012-04-19 18:44:25

回答

2

相關的代碼是在末尾:

$.fn.handsontable = function (action, options) { 
    if (typeof action !== 'string') { //init 
     options = action; 
     return this.each(function() { 
      if($(this).data("handsontable")) { 
       instance = $(this).data("handsontable"); 
       ... 
      } else { 
        ... 
       instance = new Handsontable($(this), currentSettings); 
       $(this).data("handsontable", instance); 
      } 
     }); 
    } 
} 

這意味着,該代碼將Handsontable實例作爲數據屬性的元素(和返回所選擇的組爲環連接)。有一個元素,你可以很容易地用instance = $el.data("handsontable")來提取它。如果你有一組元素,你需要循環它 - 例如與each()

+0

謝謝,我顯然沒有看到足夠的功能,因爲我應該。解決了我的問題。 – Danny 2012-04-19 18:47:11

+0

請參閱由jmm解決方案,這是更好的方法(使用onChange方法) – warpech 2012-08-09 15:23:54

2

看起來你可以使用插件的onChange方法來每次自動輸入數據時捕獲數據。不需要按鈕。一個簡單的例子添加到上面的代碼。

onChange: function(data) { 
      $("#data").append(JSON.stringify(data)); 
      } 
+0

我是插件的作者,我可以證實這是正確的方法。謝謝你們的興趣! – warpech 2012-05-20 13:05:49