2013-10-03 50 views
0

我試圖創建一個通用的JS方法,將調整(fnAdjustColumnSizing())所有可見的dataTables。問題是,我只是不能得到語法正確...調整(列大小)所有可見數據表

到目前爲止,我得到這個接近:

$.fn.dataTable.fnTables(true); //this gets all visible dataTables... 
$('#givenTable').dataTable().fnAdjustColumnSizing(); //this adjusts a given dataTable 

$.each($.fn.dataTable.fnTables(true), function(singleTable) { 
    $(singleTable).dataTable().fnAdjustColumnSizing(); 
}); // And this just don't work! Don't know why... 

任何意見或建議,在以另一種方式acomplish呢?

編輯:標誌着我下面的答案是正確的答案,但我沒有發現什麼是錯在我原來的方法(將包括,因爲它可能是有用的給別人):這是$.each的語法「 s提供的函數,它應該接收2個參數,第一個是索引,第二個是元素本身。所以:

$.each($.fn.dataTable.fnTables(true), function(idx, singleTable) { 
    $(singleTable).dataTable().fnAdjustColumnSizing(); 
}); // This works! 

回答

1

的數據表API文檔包含an example可以幫助你:

var table = $.fn.dataTable.fnTables(true); 
if (table.length > 0) { 
    $(table).dataTable().fnAdjustColumnSizing(); 
} 
+0

Yey,這是一個不同的方法(選擇所有「表」標籤),但它確實工作。無論如何,我發現我的第一種方法出了什麼問題,並且正在進行編輯以包括這一點。謝謝。 –