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?
In its most basic case you have to include the library and then call a method, called tablesorter (), to provide the possibility to sort the data of a table. Finally, this plugin has extensive documentation that will help you in handling most of the situations you may encounter in your projects.
The class names from the $.tablesorter.themes. {name} variable are applied to the table as indicated. As before the jQuery UI theme applies the default class names of "ui-icon-arrowthick-2-n-s" for the unsorted column, "ui-icon-arrowthick-1-s" for the descending sort and "ui-icon-arrowthick-1-n" for the ascending sort.
To use the tablesorter plugin, include the jQuery library and the tablesorter plugin inside the <head> tag of your HTML document: tablesorter works on standard HTML tables. You must include THEAD and TBODY tags: Start by telling tablesorter to sort your table when the document is loaded:
More explicitly: sortForce forces the user to have this/these column (s) sorted first (null by default). SortList is the initial sort order of the columns. SortAppend is the default sort that is added to the end of the users sort selection (null by default; v2.24.0 ).
Try the latest version from the tablesorter site - this seems to be fixed somewhere between version 2.0.3 and 2.0.5.
<script type="text/javascript">
$(document).ready(function()
{
$("#theTable").tablesorter({
sortInitialOrder: 'desc',
sortList: [[3,1]] // etc.
});
}
);
</script>
...that worked with the latest version of tablesorter, but didn't with the previous one I had. Hope it helps!
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.
Simply use this, second item in array is sort order (0 = ascending, 1 = descending):
.tablesorter({ sortList: [[0, 1]] });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With