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

Sorting stops working once table is changed #37

Open
ahmadtawakol opened this issue Jan 1, 2016 · 10 comments
Open

Sorting stops working once table is changed #37

ahmadtawakol opened this issue Jan 1, 2016 · 10 comments

Comments

@ahmadtawakol
Copy link

I have a table that I add data and remove data from it using JS. Sorting the table works perfectly once the page loads and the table is initialized. However, once anything in the table changes, sorting stops working.

There is no way to reinitialize the table again once the data changes.

@ckcollab
Copy link

ckcollab commented Mar 8, 2016

+1

@ckcollab
Copy link

ckcollab commented Mar 8, 2016

I've been going over https://github.com/HubSpot/sortable/blob/master/js/sortable.js and it doesn't seem like it exposes any methods to re-initialize, but it should be possible to re-init and things should "just work?"

I'm using RiotJS and I believe on re-draw it destroys the components

@BlaM
Copy link

BlaM commented Mar 9, 2016

I haven't tried to add rows to the table "manually", but in combination with vue.js sorting seems to work even when the table is modified after init. It just does not trigger an automatic resort when new items are added - the user has to click on a header to trigger a resort. It would be nice to have an option to trigger this automatically. So far all sorting seems to be encapsulated in the "onClick" handler - so there is no obvious way to trigger this in code.

@ahmadtawakol
Copy link
Author

I managed to find a workaround after posting the issue, I just forgot to post the solution.

Before doing any changes to the table, you can "un-initialize" the table first using this line of code here:
document.getElementById("InsertYourTableIDHere").setAttribute("data-sortable-initialized", "false");

Or in jQuery it would be: $("#InsertYourTableIDHere").attr("data-sortable-initialized", "false");

After you are done changing the table, just run Sortable.init(); again.

@ckcollab
Copy link

ckcollab commented Mar 9, 2016

Woo! Thanks so much!

It does suck that the sorting doesn't re-sort the proper column, hrmmm

@ahmadtawakol
Copy link
Author

You can easily keep track of which column was last sorted using the column's ID or data attribute, and then programmatically sort the table after changing it.

On Wed, Mar 9, 2016 at 9:34 AM -0800, "Eric Carmichael" notifications@github.com wrote:

Woo! Thanks so much!

It does suck that the sorting doesn't re-sort the proper column, hrmmm


Reply to this email directly or view it on GitHub.

@ckcollab
Copy link

ckcollab commented Mar 9, 2016

Hrm, trying to do something similar by manually firing the click-to-sort event:

sorted_column.dispatchEvent(new Event('onclick'));
sorted_column.dispatchEvent(new Event('onclick'));

Doesn't seem to actually work? Also, it seems like Sortable isn't sorting some columns properly, may have to specify a type somewhere? EDIT Actually maybe it is working, just some columns aren't sorting properly in the first place?

image

4146.57 should be the first row when sorted ascending

@ahmadtawakol
Copy link
Author

As far as I remember, to sort the table programmatically, all you do is give the column you want to sort a certain data attribute. Try sorting a table manually and then use inspector to see what attribute the column was given, then do the same using JavaScript or jQuery.

On Wed, Mar 9, 2016 at 9:46 AM -0800, "Eric Carmichael" notifications@github.com wrote:

Hrm, trying to do something similar by manually firing the click-to-sort event:

sorted_column.dispatchEvent(new Event('onclick'));
sorted_column.dispatchEvent(new Event('onclick'));

Doesn't seem to actually work? Also, it seems like Sortable isn't sorting some columns properly, may have to specify a type somewhere?

4146.57 should be the first row when sorted ascending


Reply to this email directly or view it on GitHub.

@ckcollab
Copy link

ckcollab commented Mar 9, 2016

Cool, added data-sortable-type="numeric" to my <th> and it's all working now, you're the best thanks so much for your time!

@paulewetzel
Copy link

Yeah, I am having the same issue when I add new rows to the table. Seems like the table could go through a re-initialization process, unhooking any events and clearing the value in _results, then initializing the table again, instead of calling
if (table.getAttribute('data-sortable-initialized') === 'true') {
return;
}

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

No branches or pull requests

4 participants