JQuery表排序器记忆排序器小部件
我在互联网上找到了以下代码:JQuery表排序器记忆排序器小部件,jquery,widget,tablesorter,Jquery,Widget,Tablesorter,我在互联网上找到了以下代码: $.tablesorter.addWidget({ id: "memorizeSortOrder", format: function(table) { if (!table.config.widgetMemorizeSortOrder.isBinded) { // only bind if not already binded table.config.widgetMemorizeSortOrder.isBinded = true;
$.tablesorter.addWidget({
id: "memorizeSortOrder",
format: function(table) {
if (!table.config.widgetMemorizeSortOrder.isBinded) { // only bind if not already binded
table.config.widgetMemorizeSortOrder.isBinded = true;
$("thead th:visible",table).click(function() {
var i = $("thead th:visible",table).index(this);
$.get(table.config.widgetMemorizeSortOrder.url+i+'|'+table.config.headerList[i].order);
});
} // fi
}
});
位于:
我想记住我的ajax表的排序,这样每次更新表都会完全改变,所以没有附加,它会保持原来的排序
问题是。。你怎么能用这个
$("#tablediv").load(
"table.php",
null,
function (responseText, textStatus, req) {
$("#table").trigger("update");
}
);
我需要什么样的改变?有这样的改变。它还使用cookies,以便在页面加载时保持排序。链接版本需要jQuery Cookie和JSON插件
我修改了我的副本,使用Crockford的JSON2脚本,而不是jQuery JSON插件
$(document).ready(function() {
$.tablesorter.addWidget({
// give the widget an id
id: "sortPersist",
// format is called in the on init and when a sorting has finished
format: function(table) {
// Cookie info
var cookieName = 'application_name_tablesorts';
var cookie = $.cookie(cookieName);
var options = {path: '/'};
var data = {};
var sortList = table.config.sortList;
var tableId = $(table).attr('id');
var cookieExists = (typeof(cookie) != "undefined" && cookie != null);
// If the existing sortList isn't empty, set it into the cookie and get out
if (sortList.length > 0) {
if (cookieExists) {
data = JSON.parse(cookie);
}
data[tableId] = sortList;
$.cookie(cookieName, JSON.stringify(data), options);
}
// Otherwise...
else {
if (cookieExists) {
// Get the cookie data
var data = JSON.parse($.cookie(cookieName));
// If it exists
if (typeof(data[tableId]) != "undefined" && data[tableId] != null) {
// Get the list
sortList = data[tableId];
// And finally, if the list is NOT empty, trigger the sort with the new list
if (sortList.length > 0) {
$(table).trigger("sorton", [sortList]);
}
}
}
}
}
});
});
有一种方法可以做到这一点。它还使用cookies,以便在页面加载时保持排序。链接版本需要jQuery Cookie和JSON插件
我修改了我的副本,使用Crockford的JSON2脚本,而不是jQuery JSON插件
$(document).ready(function() {
$.tablesorter.addWidget({
// give the widget an id
id: "sortPersist",
// format is called in the on init and when a sorting has finished
format: function(table) {
// Cookie info
var cookieName = 'application_name_tablesorts';
var cookie = $.cookie(cookieName);
var options = {path: '/'};
var data = {};
var sortList = table.config.sortList;
var tableId = $(table).attr('id');
var cookieExists = (typeof(cookie) != "undefined" && cookie != null);
// If the existing sortList isn't empty, set it into the cookie and get out
if (sortList.length > 0) {
if (cookieExists) {
data = JSON.parse(cookie);
}
data[tableId] = sortList;
$.cookie(cookieName, JSON.stringify(data), options);
}
// Otherwise...
else {
if (cookieExists) {
// Get the cookie data
var data = JSON.parse($.cookie(cookieName));
// If it exists
if (typeof(data[tableId]) != "undefined" && data[tableId] != null) {
// Get the list
sortList = data[tableId];
// And finally, if the list is NOT empty, trigger the sort with the new list
if (sortList.length > 0) {
$(table).trigger("sorton", [sortList]);
}
}
}
}
}
});
});
如果表没有正文行,则应用排序列表将中断,因此最终测试可能为:
ifsortList.length>0&&table.tBodies[0]。rows.length如果表中没有正文行,则应用排序列表将中断,因此最终测试可能为:
ifsortList.length>0&&table.tBodies[0]。rows.length谢谢ranomore!:我一直在寻找这样的解决方案。谢谢ranomore!:我一直在寻找这样的解决方案有一段时间了。我刚刚用一个名为saveSort的新小部件更新了我的fork,它将使用cookie回退功能将最后一个排序保存到本地存储。查看。我刚刚用一个名为saveSort的新小部件更新了我的fork,该小部件将使用cookie回退功能将最后一次排序保存到本地存储。看看这本书。