Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

default sort on page load ignores direction #30

Open
kickofitall opened this issue Aug 5, 2016 · 15 comments
Open

default sort on page load ignores direction #30

kickofitall opened this issue Aug 5, 2016 · 15 comments

Comments

@kickofitall
Copy link

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()) {

@kylefox
Copy link
Owner

kylefox commented Aug 20, 2016

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 😁

@kickofitall
Copy link
Author

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

@kickofitall
Copy link
Author

kickofitall commented Oct 13, 2016

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.

@akeith2002
Copy link

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

@Safihre
Copy link

Safihre commented Jan 3, 2017

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

@N2481
Copy link

N2481 commented May 21, 2019

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

@kickofitall
Copy link
Author

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

Can you be a bit more specific?

@N2481
Copy link

N2481 commented May 22, 2019

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

@kickofitall
Copy link
Author

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?

@N2481
Copy link

N2481 commented May 22, 2019

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

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

@kickofitall
Copy link
Author

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.

@N2481
Copy link

N2481 commented May 22, 2019

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.

@kickofitall
Copy link
Author

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

@N2481
Copy link

N2481 commented May 22, 2019

Your solution is working now. Thank you.

@kickofitall
Copy link
Author

You'r welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants