Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery tablesorter how to find sortList object

I am using the jQuery tablesorter plugin. I am wanting to store how a user has sorted the table on the page and automatically sort that way the next time the page loads. To do this, I first need to be able to find the sortList object that stores how the table is sorted. For the life of me I cannot figure out how to get it. The documentation doesn't seem to have anything on this and I've tried everything I can think of.

like image 208
Chris Avatar asked Nov 10 '10 23:11

Chris


People also ask

How to sort the data of a table using tablesorter?

In its most basic case you have to include the library and then call a method, called tablesorter (), to provide the possibility to sort the data of a table. Finally, this plugin has extensive documentation that will help you in handling most of the situations you may encounter in your projects.

How to sort list alphabetically using jQuery?

Given a list of elements, The task is to sort them alphabetically and put each element in the list with the help of jQuery. jQuery text () Method: This method set/return the text content of the selected elements. If this method is used to return content, it provides the text content of all matched elements (HTML tags will be removed).

Is there a way to sort rows in HTML table?

HTML provides an element for tabular data that, not surprisingly, is called table. The problem with it is that this element doesn’t have an API that allows you to sort its rows based on a certain criteria, such as the price from low to high.

What is This list in the tablesorter plugin?

This list contains DOM elements (not jQuery objects of each table header cell like the $headers variable) and is how the original version of tablesorter stored these objects. It is not used in the current version of tablesorter, and is only left in place for backwards compatibility with widgets written for the original tablesorter plugin.


2 Answers

You need to bind your table element to the tablesorter sortEnd event. All the data for that object is passed in to the handler. You can then get the current sort like so:

var currentSort;

$("#yourtableId").tablesorter({
    // initialization
}).bind("sortEnd", function(sorter) {
    currentSort = sorter.target.config.sortList;
});
like image 98
Bryan Avatar answered Oct 01 '22 05:10

Bryan


It might be a bit less overhead to save the last sort only when you need it like this:

lastSortList=$("#mytable")[0].config.sortList;

Remember to declare the variable in the right scope of course.

(I think the questioneer's problem probably was that he had to get the DOM element via [0] and not the jQuery element.)

like image 34
e-motiv Avatar answered Oct 01 '22 06:10

e-motiv