jquery-tablesort icon indicating copy to clipboard operation
jquery-tablesort copied to clipboard

default sort on page load ignores direction

Open kickofitall opened this issue 8 years ago • 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()) {

kickofitall avatar Aug 05 '16 21:08 kickofitall

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 😁

kylefox avatar Aug 20 '16 21:08 kylefox

Thanks for your answer. I'm trying to make both in the coming days ...

kickofitall avatar Aug 22 '16 16:08 kickofitall

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.

kickofitall avatar Oct 13 '16 15:10 kickofitall

I noticed this too and your fix did fix it! Thanks.

akeith2002 avatar Nov 02 '16 00:11 akeith2002

Experienced the same problem, the proposed code-change fixed it 👍

Safihre avatar Jan 03 '17 12:01 Safihre

The proposed solution creates a new bug: tablesort.index returns null always.

N2481 avatar May 21 '19 18:05 N2481

The proposed solution creates a new bug: tablesort.index returns null always.

Can you be a bit more specific?

kickofitall avatar May 22 '19 08:05 kickofitall

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; });

N2481 avatar May 22 '19 10:05 N2481

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?

kickofitall avatar May 22 '19 11:05 kickofitall

I added my code to your CodePend demo, please check it here:

https://codepen.io/anon/pen/VOyWjZ

N2481 avatar May 22 '19 13:05 N2481

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.

kickofitall avatar May 22 '19 14:05 kickofitall

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.

N2481 avatar May 22 '19 14:05 N2481

Just rename the "asc" to "desc" on line 44.

kickofitall avatar May 22 '19 14:05 kickofitall

Your solution is working now. Thank you.

N2481 avatar May 22 '19 14:05 N2481

You'r welcome.

kickofitall avatar May 22 '19 14:05 kickofitall