views:

163

answers:

1

Hello, I'm using jQuery Tablesorter to sort a table and everything works as expected. What I'd like to do is add a link above my table that says something along the lines of "Sort by Price" and when the user clicks the link I want the "Price" column to sort. Additionally, if they click it again, it would sort the price the other way. Is this possible?

Thanks! Manoj

A: 

From tablesorter.com

$(document).ready(function() { 
    $("table").tablesorter(); 
    $("#trigger-link").click(function() { 
        // set sorting column and direction, this will sort on the first and third column the column index starts at zero 
        var sorting = [[0,0],[2,0]]; 
        // sort on the first column 
        $("table").trigger("sorton",[sorting]); 
        // return false to stop default link action 
        return false; 
    }); 
});
Joel Potter
Thanks, Joel - that works really well. Is there a way to make it toggle so if I click the link again it'll sort the other way? Right now when I click the link it will sort, but if I click it again nothing happens. Thanks very much.
Manoj
You could keep the current state of the sort in a variable and toggle that before setting.
Joel Potter