Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Tablesorter sort same column after update

I have a table which is updated with ajax and after update it if sorted but I need to sort not a fixed column but the same column which was last clicked before update.

function tableUpdated() {
$(".tablesorter").trigger("update");
//alert($(".tablesorter").sorting);
var sorting = [[7, 0]];
$("table").trigger("sorton", [sorting]);
}

In my code above I need to put my selected column index instead of 7

like image 366
HasanG Avatar asked Feb 18 '10 12:02

HasanG


3 Answers

jQuery's .data() will help you here. Whenever the user clicks to sort a table, store the columns on the table itself. Within the sort function, add this:

$("#table").data('sorting', selectedColumn);

Now the element with id="table" has a property sorting with the value of selectedColumn. In tableUpdated, you can use this data:

function tableUpdated() {
    $(".tablesorter").trigger("update");
    var sorting = [[$("#table").data('sorting'), 0]];
    $("#table").trigger("sorton", [sorting]);
}

Data added using .data() can be even more complex, allowing you to add objects of data. See this page for more details.

like image 151
Aaron Avatar answered Nov 03 '22 05:11

Aaron


Using your code, you can do something like this (suppose your table id is #list-table) to maintain current table sorting:

function tableUpdated() {
    $("#list-table").trigger("update");
    var sorting = $("#list-table").get(0).config.sortList;
    $("#list-table").trigger("sorton", [sorting]);
}
like image 23
paulo Avatar answered Nov 03 '22 04:11

paulo


you can pick it up on the 'sortEnd' event on your table:

var lastSortList;

$table.on('sortEnd', function(e) {
    lastSortList = e.target.config.sortList;
});
like image 2
Merl Avatar answered Nov 03 '22 05:11

Merl