调整(列大小)所有可见数据表

问题描述:

我试图创建一个通用的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! 

的数据表API文档包含an example可以帮助你:

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

Yey,这是一个不同的方法(选择所有“表”标签),但它确实工作。无论如何,我发现我的第一种方法出了什么问题,并且正在进行编辑以包括这一点。谢谢。 –