4

I'm using jQuery Tablesorter plugin, it works fine. However there is a problem. Imagine that you have some sorting order, but you want to live the page and come back shortly. Unfortunately when you come back you will get the initial sorting order which is simply wrong. So I was trying to find a clue how to save the state of Tablesorter (remember the sorting choice when leaving the page) in some variable and pass it via URL using _GET in php. Any ideas and help will be appreciated.

I did a little research and found out the following:
1.You can read the current sortlis

     <script>
    $(window).unload( function () {   
    var sortList;  
    $(table).tablesorter().bind("sortEnd", function(sorter)   
    {      
        sortList = sorter.target.config.sortList;  
        $_GET['sortList'] = sortList;  
    });   
    }  
    );
    </script>

2.I've tried to save the sortlist like above and read it when page loads:

<script>  
$(document).ready(function()   
    {   
        sortList=$_GET['sortList'];  
        $.tablesorter.defaults.widgets = ['zebra'];   
        $.tablesorter.defaults.sortList = [[1,0]];   
        $("table").tablesorter();   
    }   
);      
</script>
  1. Doesn't work, I think these 2 lines are questionable:

    sortList=$_GET['sortList']; ... $_GET['sortList'] = sortList;

I'm mixing here languages JavaScript with PHP and data types. But I'm not a professional programmer and I can't connect the dots. Any help?

Alex

user985456
  • 91
  • 1
  • 2
  • 5

3 Answers3

8

I have a fork of the tablesorter plugin on github and I wrote a widget called "saveSort" (demo) which stores the last sort into localStorage if the browser has HTML5 or into a cookie as a fallback.

Mottie
  • 84,355
  • 30
  • 126
  • 241
1

try this library jQuery Address to work in a solution using the window.location.hash object. I'm currently using it in a project and works fine.

Diego Marafetti
  • 465
  • 3
  • 7
0

Depending on how you coded the page, some browsers will automatically remember what the last state was. More than likely though, it will not, especially on older browsers.

A basic, cross browser solution is to store the state information in the window.location.hash object. In other words, for any event or state that you want to remember, you change the hash to a unique identifier:

window.location.hash = 'column1'

This adds #clicked-button to the end of your URL. You would want to call this when the user sorts the table. That alone won't do anything though. The second step is to listen for changes to the hash. IE has a onhashchange event but I dont think other browsers support that event. The alternative is to set up an interval to listen manually.

var last_hash = '';
setInterval(function()
{
    if(window.location.hash != last_hash)
    {
        //a new event happened, change the state of the page
        last_hash = window.location.hash;

        if(last_hash == 'column1')
        {
            //sort based on column1
        }
        else if(last_hash == 'column2')
        {
            //sort based on column2
        }
        //etc
    }
}, 500);

A script like this will continue to run even when a user navigates away from the page and then comes back.

You can also read these for more info on other techniques.

Community
  • 1
  • 1
Jeff
  • 13,943
  • 11
  • 55
  • 103