default sort on page load ignores direction
kickofitall opened this issue ยท 15 comments
A descent sorting on page load seems impossible because of the check for a different column.
The sort function with direction 'desc' still sorts the column in ascending order:
$('table.sortable').tablesort().data('tablesort').sort($("th.default-sort"), 'desc');
Changing the following line fixed that issue for me:
https://github.com/kylefox/jquery-tablesort/blob/master/jquery.tablesort.js#L42
from if (this.index !== th.index()) {
to if (this.index !== null && this.index !== th.index()) {
Thanks for pointing this out โ a CodePen demonstrating the issue would be helpful, and a pull request would be even better because I likely will never have time to patch this myself ๐
Thanks for your answer.
I'm trying to make both in the coming days ...
I made a demo on CodePen: http://codepen.io/anon/pen/ozyVXY
In the JS in line 139 is the init for descent sorting, but the table is "wrong" sorted.
The fix is in line 42/43 on JS.
If my assumption is correct I will make a pull request.
I noticed this too and your fix did fix it! Thanks.
Experienced the same problem, the proposed code-change fixed it ๐
The proposed solution creates a new bug: tablesort.index returns null always.
The proposed solution creates a new bug: tablesort.index returns null always.
Can you be a bit more specific?
With the proposed solution, this code stop working: tablesort.index is allways null:
$('#userList').on('tablesort:complete', function(event, tablesort) {
orderByColumn = tablesort.index;
direction = tablesort.direction;
});
Sorry, but I can't reproduce your problem.
If I add your code to my CodePen demo, the tablesort.index is switching between 0 and 1 after sorting the columns.
Can you provide a CodePen demo for this issue?
I added my code to your CodePend demo, please check it here:
You are right! I think this should solve the problem:
https://codepen.io/anon/pen/wbpqER
I split the [if/else if] in two if-blocks at line 48 and reset the if-statement on line 43 to it's original version.
Ok, it's working. But now, when you click on a column, the default sort is DESC, before was ASC.
Thank you for your help.
Just rename the "asc" to "desc" on line 44.
Your solution is working now. Thank you.
You'r welcome.