JQuery Tablesorter виджет memorizeSortOrder - PullRequest
1 голос
/ 28 марта 2010

Я нашел этот код в интернете:

$.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  
   }   
});

Найдено в: http://www.adspeed.org/2008/10/jquery-extend-tablesorter-plugin.html

Я хотел бы запомнить сортировку моих таблиц ajax, чтобы при каждом обновлении (таблица полностью изменялась и не добавлялось) она сортировалась так, как была.

Вопрос в том ... как это можно использовать?

$("#tablediv").load(
          "table.php",
          null,
          function (responseText, textStatus, req) {
            $("#table").trigger("update");


          }
        );

Какие изменения мне нужны?

Ответы [ 2 ]

3 голосов
/ 21 сентября 2010

Существует другой плагин , который делает это. Он также использует куки, поэтому сортировки сохраняются при загрузке страницы. Для связанной версии требуются плагины jQuery Cookie и JSON.

Я изменил свою копию, чтобы использовать скрипт Крокфорда 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]);
            }
          }
        }
      }
    }
  });
});
1 голос
/ 14 января 2011

Если в таблице нет строк тела, применение списка сортировки будет прервано, поэтому финальным тестом может стать:

if(sortList.length > 0 && table.tBodies[0].rows.length)

...