2013-04-30 80 views
0

我们有一个页面上有多个表。所有的表格都是相同的(当然数据除外)。 当我单击一个表的列标题时,是否可以对所有表进行排序? 行为应该是,如果我点击列标题'名称'(例如),所有的表格将排序在同一列。jQuery Datatables排序多个表

我试着做到以下几点:

$(document).on("click", ".myTable thead th", function() { 
var index = $(this).closest("thead").children("tr").find("th").index($(this)); 

var allTables = $.fn.dataTable.fnTables(); 

for (var i = 0; i < allTables.length; i++) { 
    $(allTables[i]).dataTable().fnSort([index, "asc"]); 
} 
}) 

但我这样做,我得到以下异常时:

Uncaught TypeError: Cannot read property 'sSortDataType' of undefined

+0

看起来它应该工作。你能链接到一个测试用例吗? – 2013-04-30 10:00:00

+1

已解决:fnSort()的参数必须是2维数组,因为它需要一个数组,其中包含要排序的所有列的排序,包括列的排序选项。像这样:$(allTables [i])。dataTable()。fnSort([[index,“asc”]]); – wvp 2013-04-30 13:19:21

回答

2

为fnSort()的参数必须是一个2二维数组,因为它需要一个数组,其中包含要排序的所有列的排序,包括列的排序选项。像这样:$(allTables [i])。dataTable()。fnSort([[index,“asc”]]);