我正在尝试使用tablesorter.js,但在使用ajax更新我的表时遇到问题。我关注了the code on this page但它似乎不能正常工作。我还注意到的一件事是,即使在示例网站上,代码也无法正常工作。当我单击“追加新表数据”时,它将数据添加到表中,但没有正确排序。如果我复制 JavaScript 代码并将其粘贴到控制台中,它可以正常工作并正确对表格进行排序。我使用的代码如下:
var updateTableSort = function(){
var table = $('#transaction-table')
//tells table sorter that table has been updated
table.trigger("update");
//re sorts after table has been updated based on
//current sort patern
var sorting=table.get(0).config.sortList;
table.trigger('sorton', [sorting]);
}
同样,如果我将其复制并粘贴到控制台中,它可以正常工作,但是当我将其放入成功的 ajax 函数中时,它无法正确对表进行排序。任何帮助找出问题所在的帮助将不胜感激
最佳答案
试试我的fork of tablesorter 。更新后它会自动重新排列表:
// pass anything BUT false and the table will resort
// using the current sort
$("table").trigger("update", [false]);
这是updated append data to the table using ajax demo :
$(function() {
$("table").tablesorter({ theme : 'blue' });
$("#ajax-append").click(function() {
$.get("assets/ajax-content.html", function(html) {
// append the "ajax'd" data to the table body
$("table tbody").append(html);
// let the plugin know that we made a update
// the resort flag set to anything BUT false (no quotes) will
// trigger an automatic
// table resort using the current sort
var resort = true;
$("table").trigger("update", [resort]);
// triggering the "update" function will resort the table using the
// current sort; since version 2.0.14
// use the following code to change the sort; set sorting column and
// direction, this will sort on the first and third column
// var sorting = [[2,1],[0,0]];
// $("table").trigger("sorton", [sorting]);
});
return false;
});
});
关于javascript - 使用tablesorter.js 对表中的ajax 数据进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24049319/