jQuery tablesorter how to find sortList object
Asked Answered
E

3

14

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.

Eyeful answered 10/11, 2010 at 23:33 Comment(0)
H
29

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;
});
Handsome answered 10/11, 2010 at 23:44 Comment(1)
Love it. Love everything about it. That is all :)Ailee
S
1

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.)

Savitt answered 13/7, 2014 at 17:39 Comment(0)
C
-1

this is how I managed to do it:

<?php
// Set session variables
$_SESSION["sortlistsessie"] = "[[0,0],[2,1]]";
?>


<script language="javascript" type="text/javascript">

//document.cookie="TestCookie3=[[0,0],[2,1]]";
$(document).ready(function() { 
// extend the default setting to always include the zebra widget. 
$.tablesorter.defaults.widgets = ['zebra']; 
// extend the default setting to always sort on the first column 
$.tablesorter.defaults.sortList = <?php print_r($_SESSION["sortlistsessie"]
);           ?>//      <?php $_SESSION["sortlistsessie"];?>; //<?php echo       
$_COOKIE["TestCookie3"]; ?>; 
// call the tablesorter plugin 
$("#searchTable").tablesorter(); 
}); 
</script>
Cowled answered 1/12, 2014 at 18:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.