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

Fix: default table sort was always ascending #444

Merged
merged 3 commits into from
Jul 29, 2020

Conversation

octomike
Copy link
Contributor

@octomike octomike commented Dec 2, 2017

An ascending sort order is always triggered when then target index is different
from the tablesort index. This was always true on a page (re)load, because
index is initialized with null. By setting the instance' index to the
default-sort column index we assure that the direction parameter to sort() is
used.

See https://github.com/kylefox/jquery-tablesort/blob/master/jquery.tablesort.js#L42

An ascending sort order is always triggered when then target index is different
from the tablesort index. This was always true on a page (re)load, because
index is initialized with null.  By setting the instance' index to the
default-sort column index we assure that the direction parameter to sort() is
used.

See https://github.com/kylefox/jquery-tablesort/blob/master/jquery.tablesort.js#L42
@coveralls
Copy link

coveralls commented Dec 2, 2017

Coverage Status

Coverage remained the same at 81.76% when pulling 14633ac on octomike:fix_default_table_sort into 3867e1a on voxpupuli:master.

@octomike octomike closed this Dec 2, 2017
@octomike octomike reopened this Dec 2, 2017
@coveralls
Copy link

coveralls commented Dec 2, 2017

Coverage Status

Coverage remained the same at 81.76% when pulling 45a128b on octomike:fix_default_table_sort into 3867e1a on voxpupuli:master.

@emlun007
Copy link
Contributor

emlun007 commented Dec 7, 2017

Tablesort will not work properly with DataTables that is initialized in _macros.html.

If you want to save the sort order on page reload, use stateSave option in DataTables in _macros.html.

@octomike
Copy link
Contributor Author

octomike commented Dec 20, 2017

I'm not trying to save anything.

Maybe it's just me, but right now the default sorting on the index page is broken. I assume the only logical ordering is descending by report timestamp, right? And this PR should address this.

@kirkins
Copy link
Contributor

kirkins commented Dec 20, 2017

It should or it does? @octomike

@octomike
Copy link
Contributor Author

Not for me...

This is current master:

2017-12-20-154414_688x1108_scrot

On month changes this would look even worse, because the ordering is not just in the wrong direction, it's also broken.

This is the feature branch:

2017-12-20-154927_630x848_scrot

$('thead th.date').data('sortBy', function(th, td, tablesort) {
var tdTime = new Date(td.text().replace("-", ""));
if(isNaN(tdTime)) return 0;
else return tdTime;
});

if ($('th.default-sort').data()) {
$.tablesort.DEBUG = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this set to true by default?

if ($('th.default-sort').data()) {
$.tablesort.DEBUG = false;
var tablesort = $('table.sortable').tablesort().data('tablesort');
// explicitly set the current index so we don't get default sort (asc)
Copy link
Contributor

@kirkins kirkins Dec 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is relevant to the commit but I don't think it's necessary in the source.

You should change as little as possible. Don't move line 9 to line 15 if you don't have to.

The commit should simply remove line 10 and replace it with:

var tablesort = $('table.sortable').tablesort().data('tablesort');
tablesort.index = $('th.default-sort').index();
tablesort.sort($("th.default-sort"), 'desc');

Take a look at the "Files changed" tab.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reordering of the two calls is necessary unfortunately.

As for the annotations, I just removed them and force pushed.

@kirkins
Copy link
Contributor

kirkins commented Dec 23, 2017

As for the fix in end behavior. I can confirm that this change fixes the default sorting when you first launch a page.

Nice work 👍

@coveralls
Copy link

coveralls commented Dec 29, 2017

Coverage Status

Coverage increased (+0.6%) to 82.404% when pulling fb564f6 on octomike:fix_default_table_sort into 2da9ba9 on voxpupuli:master.

@octomike
Copy link
Contributor Author

octomike commented Jan 2, 2018

Meh, I force pushed and I guess that deleted the review comments. Sorry about that.
I removed the unnecessary line and the comment (although I would find that helpful as a newcomer).

The reordering of the function calls are necessary for the fix, however.

@octomike
Copy link
Contributor Author

octomike commented Jan 9, 2018

Are there still issues with the proposed fix?

@octomike
Copy link
Contributor Author

Can this be merged?

var tablesort = $('table.sortable').tablesort().data('tablesort');
tablesort.index = $('th.default-sort').index();
tablesort.sort($("th.default-sort"), 'desc');
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO $('th.default-sort') should be assigned to a variable to avoid looking up the same selector three times; I imagine that lookup could be fairly slow.

@larsnaesbye
Copy link
Contributor

larsnaesbye commented Nov 14, 2019

As @octomike said, can't this be merged?

Then the changes requested by @runejuhl could be a different PR.

@runejuhl
Copy link
Contributor

As @octomike said, can't this be merged?

Then the changes requested by @runejuhl could be a different PR.

I'd much rather fix issues beforehand -- my experience is that if we don't fix it now, it'll never be fixed, and for minor things like this it's doubly true.

@larsnaesbye care to give it a spin and see if everything is alright? I've rebased @octomike's work onto master, done some linting and fixed the requested change. Find it here: https://github.com/runejuhl/puppetboard/tree/pr/444

@larsnaesbye
Copy link
Contributor

I say go for a merge.

@raphink raphink merged commit 067438d into voxpupuli:master Jul 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants