andriy
andriy

Reputation: 4164

How to get cookie value from jquery tablesorter, which stores sorting?

In JavaScript code of web application, the table sorter is defined by:

$("#my-table").tablesorter({
    headers: { 
       1: { 
               sorter: false
           }
    },
    widgets: ["saveSort"] 
});

So when the page is refreshed the sorting of table is saved, but when browser is closed, the table backs to its original sorting. So what I want is to get how table is sorted and save it to database. Can someone suggest me how I can obtain the cookie(s), which stores how table is sorted? Thanks

Upvotes: 2

Views: 1292

Answers (2)

Mottie
Mottie

Reputation: 86433

When the saveSort widget (demo) saves the information, it tests the browser for localStorage first, then if that isn't available, it falls back to saving the sort to a cookie. So, you can either use the function built into the widgets file like this:

var myTable = $('#table1')[0],
    myLastSort = $.tablesorter.storage( myTable, 'tablesorter-savesort');

or if you are using Chrome, go to that page and press F12. now click on the resources tab and look under "Local Storage"

saveSort

The value may look a bit confusing, but it's just a JSON format:

{
    "/tablesorter/docs/example-widget-savesort.html": {
        "0": {
            "sortList": [ [0,0],[2,1] ]
        },
        "1": {
            "sortList": [ [0,0] ]
        }
    }
}

And it is broken down as follows:

  • "/tablesorter/docs/example-widget-savesort.html" is the url of the web page
  • "0" or "1" would either be the table ID or the index of the table on the page
  • "sortList" contains the actual sort list value

So as you can see in the above data, it is saving sort information for two tables on one web page.

Upvotes: 3

ChrisW
ChrisW

Reputation: 5068

You could use jQuery to save a cookie with the sorted colum. The next time that page is loaded, use either jQuery or server-side logic to get the value of the cookie, and sort the appropriate column. This might be useful reading: Can jQuery read/write cookies to a browser?

Upvotes: -1

Related Questions