joequery/Stupid-Table-Plugin

Sorting - Remembering?

rusrus opened this issue · 2 comments

Is it possible to navigate away from a page after sorting, and come back to it with the sorting entact - the way you left it. I am thinking localStorage or maybe cookies ??

Well I have come up with this and it works using the sessionStorage object. Saves the sort column and direction for a session....

Please let me know if there are ways to improve this code...

$(function(){

var table = $("table").stupidtable();// call function

if (sessionStorage.data_column) {  // check if sessionStorage exists

    var thcol1 = sessionStorage.getItem("data_column"); //retrieve sort col
    var thdir1 = sessionStorage.getItem("data_direction"); //retrieve sort dir

    var $th_to_sort = table.find("thead tr th").eq(thcol1); //select sort column
    $th_to_sort.stupidsort(thdir1); //call direction
}

table.bind('aftertablesort', function (event, data) {

    sessionStorage.setItem("data_direction", data.direction); // data.direction - the sorting direction (either asc or desc)
    sessionStorage.setItem("data_column", data.column); // data.column - the index of the column sorted after a click
  });
});

`

Thank you for this snippet. I may try to incorporate it into the documentation somehow.