I've found this code in the internet:
$.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
}
})开发者_如何学运维;
Found in: http://www.adspeed.org/2008/10/jquery-extend-tablesorter-plugin.html
I would like to memorize the sorting of my ajax tables so on each update (table changes completely so there is no append) it keeps sorted the as it was.
Question is.. how can use this?
$("#tablediv").load(
"table.php",
null,
function (responseText, textStatus, req) {
$("#table").trigger("update");
}
);
What changes do I need?
There is another plugin that does this. It also uses cookies so the sorts are persisted across page loads. The linked version requires the jQuery Cookie and JSON plugins.
I modified my copy to use Crockford's JSON2 script instead of the jQuery JSON plugin.
$(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]);
}
}
}
}
}
});
});
If a table has no body rows applying a sort list will break, so the final test could become:
if(sortList.length > 0 && table.tBodies[0].rows.length)
精彩评论