Jquery tablesorter how to store previous sort list
Hi i am using jquery tablesorter plagin. I want to store the previous sort list when a user close or make a refresh to the page. And after refresh or reload the page, the table will use the previously stored sort order. I try in many ways. Here is one of those:
- $.tablesorter.addWidget({
// give the widget an id
id: "sortPersist",
// format is called when the on init and when a
// sorting has finished
format: function(table) {
// Cookie info
var cookieName = 'MY_SORT_COOKIE';
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 = $.evalJSON(cookie);
}
data[tableId] = sortList;
$.cookie(cookieName, $.toJSON(data), options);
}
// Otherwise...
else {
if (cookieExists) {
// Get the cookie data
var data = $.evalJSON($.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]);
}
}
}
}
}
});
- $.tablesorter.addWidget({
id: "numbering",
format: function(table) {
var c = table.config;
$("tr:visible", table.tBodies[0]).each(function(i) {
$(this).find('td').eq(0).text(c.page*c.size+i + 1);
});
}
});
- $(document).ready(function()
{
try{
$.tablesorterPager.defaults.size=document.getElementById("pagesize").value;
$(".tablesorter").tablesorter(
{
widgets: ['numbering']
}
).tablesorterPager({container: $("#pager")});
}
catch(e){}
$(".tablesorter").tablesorter({widgets: ['numbering', 'sortPersist']});
}
);
In my code
- var cookie = $.cookie(cookieName);
This line cause a error. Can any one please ?
'numbering' - widgets is only using my purpose. And this is not an issue although i add this for making the code more visible for someone.