views:

81

answers:

1

Hi,

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?

+1  A: 

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]);
            }
          }
        }
      }
    }
  });
});
ranomore
Thanks ranomore! :) I've been looking for a solution like this for a while.
echedey lorenzo