jQuery Tablesorter Cookie Widget

Introduction

I’ve recently been doing some work for a client who had links on each table header which reloaded the page and had php reorder the table contents. Very slow and added alot more work onto the server, not a good idea. I was certain that I had seen javascript based tablesorters some where, and didn’t want to write my own.

Enter tableSorter 1.x a great piece of work by Christian Bach which has the wonderful ability to convert any table with the help from jQuery, into a client side sortable table. I had just one problem with this, when the page was reloaded the table sorting was lost. Enter the even better almighty tablesorter 2.x with added widget support, and custom parsers wow this sound like it was for me, I just needed to make a cookie widget. So I did.

Requirements

Note: It’s worth noting that for this to work correctly each table will need it’s own id. When a table of the same id has the table sorter applied it will sort it in the same way it was previously.

Compatibility

So far the widget has been tested in windows using the following browsers.

The Script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
(function($) {
  $.tablesorter.addWidget({
    id: 'cookie',
    format: function(table) {
      var sortList = table.config.sortList;
      var tablesorterCookieJar = $.cookieJar('tablesorter');
      if ( sortList.length > 0) {
        tablesorterCookieJar.set($(table).attr('id'), sortList);
      } else {
        var sortList = tablesorterCookieJar.get($(table).attr('id'));
        if (sortList && sortList.length > 0) {
          jQuery(table).trigger('sorton', [sortList]);
        }
      }
    }
  });
})(jQuery);

Usage

When starting your tablesorter on a table, you just need to add one little bit to say hey use this widget.

1
2
3
4
5
jQuery(function($) {
  $('.tablesort').tablesorter({
    widgets: ['cookie']
  });
});

Explanation

It’s a very simple widget.

Here the widget is add to tablesorter plugin, using the addWidget method. Passing an object containing and id which is a string a nd a format method. The id is used later to specify that it’s this widget we want to use.

1
2
$.tablesorter.addWidget({
    id: 'cookie',

Here is the bulk of the widget, the format method. This method is called when ever the table gets formatted, this is triggered by the user changing the table sort order or when initialised. tablesorter calls the format method passing in a table object, which contains config data and the actual table data it’s self, this can be manipulated to reformat the table. tableSorterCookieJar gets an instance of the cookieJar object with the name tablesorter, this is to store the table id and sort order of the table.

3
4
5
    format: function(table) {
      var sortList = table.config.sortList;
      var tablesorterCookieJar = $.cookieJar('tablesorter');

If the sortList contains data this means the user triggered an event and the format method of the widget was called, we then simple store this information for later use, using the table id as a way of recognising the table from all rest.

6
7
      if ( sortList.length > 0) {
        tablesorterCookieJar.set($(table).attr('id'), sortList);

Else if the sortList is empty and doesn’t yet contain any sort data it means that the table is being initialised. At this point the widget tries to get the previously stored sort order from the cookieJar. If this was successful it triggers the sorton event which was bound to the table by tablesorter (very handy), it passes the sortList from the previously stored sort order, this sorts the table how it was sorted last time. If no data was found in the cookieJar nothing is done.

8
9
10
11
12
13
      } else {
        var sortList = tablesorterCookieJar.get($(table).attr('id'));
        if (sortList && sortList.length > 0) {
          jQuery(table).trigger('sorton', [sortList]);
        }
      }

I hope this breif tutorial to how the tablesorter plugin widgets work make sense and is of use.

Documentation

Pass the widget id “cookie” as parget of the widgets array when you initialise the tablesorter on a table (see usage example).

Downloads

Downloads

jquery.tablesorter.cookie.example.html
jquery.tablesorter.cookie.js (1.26 KB)
jquery.tablesorter.cookie.pack.js (1.27 KB)