Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery tablesorter with row numbers

I am using the jQuery Tablesorter 2.0 plugin to provide some basic table sorting functionality.

On my table, I would like to have a column for row number. The trouble is, the Tablesorter plugin sorts this column with everything else. I have tried setting the "headers" attribute for the first column, but that only removes sorting capability. It does not prevent the column from being reordered when I sort by a different column.

Is having a list of row numbers possible with this plugin? Can you think of a way around the problem?

like image 415
Brad Avatar asked Jul 01 '11 14:07

Brad


1 Answers

It sounds like you need to renumber the rows every time the table is sorted. Try this (use it after you have already initialized tableSorter on your table):

var table = $("#myTable");
table.bind("sortEnd",function() { 
    var i = 1;
    table.find("tr:gt(0)").each(function(){
        $(this).find("td:eq(0)").text(i);
        i++;
    });
}); 
like image 160
Chris Laplante Avatar answered Nov 11 '22 03:11

Chris Laplante