Chad Birch Chad Birch - 1 year ago 81
jQuery Question

Exclude a column from being sorted using jQuery tablesorter

I am looking for a way to exclude a single column from being sorted using jQuery's tablesorter plugin. Specifically, I have a fairly large table and would like to keep a "row number" column fixed so that it is easy to see what position in the table a particular row is, after sorting.

For example:

# name
1 papaya
2 apple
3 strawberry
4 banana

When sorted on the name column, should result in:

# name
1 apple
2 banana
3 papaya
4 strawberry


Answer Source

Here is a widget you can use that will accomplish what you are looking for:

$(function() {
    // add new widget called indexFirstColumn
        // give the widget a id
        id: "indexFirstColumn",
        // format is called when the on init and when a sorting has finished
        format: function(table) {               
            // loop all tr elements and set the value for the first column  
            for(var i=0; i < table.tBodies[0].rows.length; i++) {
                $("tbody tr:eq(" + i + ") td:first",table).html(i+1);

        widgets: ['zebra','indexFirstColumn']

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download