我有一个用 ajax 更新的表,如果排序后更新它,但我需要排序的不是固定列,而是更新前最后单击的同一列。
function tableUpdated() {
$(".tablesorter").trigger("update");
//alert($(".tablesorter").sorting);
var sorting = [[7, 0]];
$("table").trigger("sorton", [sorting]);
}
在我上面的代码中,我需要放置我选择的列索引而不是 7
最佳答案
jQuery 的 .data()
会在这里帮助你。每当用户单击对表格进行排序时,将列存储在表格本身上。在排序函数中,添加:
$("#table").data('sorting', selectedColumn);
现在 id="table"
的元素有一个属性 sorting
,其值为 selectedColumn
。在 tableUpdated 中,您可以使用此数据:
function tableUpdated() {
$(".tablesorter").trigger("update");
var sorting = [[$("#table").data('sorting'), 0]];
$("#table").trigger("sorton", [sorting]);
}
使用 .data()
添加的数据可能更复杂,允许您添加数据对象。参见 this page了解更多详情。
关于javascript - Jquery Tablesorter 在更新后对同一列进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2288655/