views:

1208

answers:

4

I can't figure this out. This question was also asked here http://www.nabble.com/TableSorter-plugin---default-column-sort-DESC-instead--How--to25180761s27240.html#a25180761 with no response.

I've tried

    $.tablesorter.defaults.sortInitialOrder = 'desc';

and altering the jquery.tablesorter.js file to default to 'desc' but it doesn't work. When I click on the column headers, the first sort is still ascending so the user has to click twice to descend the values.

How can I get Tablesorter to sort by descending by default?

+5  A: 

Looks like a bug in the tablesorter code, or I'm misunderstanding what the sortInitialOrder parameter is supposed to do. At line 536 it sets the sorter order by looking at the number of times the column has been sorted and taking the value mod 2. It should also take into account the value of sortInitialOrder.

Change line 536 from

this.order = this.count++ % 2;

to

this.order = this.count++ == 0 ? this.order : (1 - this.order);

And add after this line (so that the first click on a different column gives you the default)

$headers.not($cell).each( function() {
    this.count = 0;
});

and change line 421 from

o.count = s[1];

to

o.order = o.count = s[1];

so that the initial order is overridden if a sortList is applied.

Then you can use the sortInitialOrder parameter to tablesorter to set up a default first sort order for the column. Any ordering provided in the sortList will override the sortInitialOrder provided for the entire table.

Note that this applies to Tablesorter 2.0.

tvanfosson
Yes, it works! Thanks a bunch!
Matt McCormick
Excellent - needed to do this on some admin tools here on the site :)
Jarrod Dixon
A: 

It works fine. Thanks for that! Is it also possible to change this initialSort value for each column individual? For example by editing the headers options when calling the tablesorter function? I tried to add "sortInitialOrder" like this:

$("#SortTable").tablesorter({sortList:[[0,0],[1,0],[2,0],[3,0]],
        headers: { 0: { sorter:\'text\' } , 
                   1: { sorter:\'digit\' } ,
                   2: { sorter:\'text\' } ,
                   3: { sorter:\'text\', sortInitialOrder: \'desc\' } ,
                   4: { sorter:\'digit\'} ,
                 }
        }); 

But it did not work :-(

Michael
A: 

great! or simply use this: .tablesorter(sortList: [[0, 1]]);

jacquet
A: 

This doesnt seems to be working with Fire Fox though.

Osman Mohammed
Oops my Bad, it did work once I clean refreashed.
Osman Mohammed