3 ms·
Which is the right one for this situation? I want to sort a table with a large number of rows after a user clicks on a column heading. This will take several s
by tzs 8y ago
Which is the right one for this situation?
I want to sort a table with a large number of rows after a user clicks on a column heading. This will take several seconds, so I want to change the column header to a bold, red "Sorting...please wait" while the sort is taking place, then change it back.
After changing the heading, I need to put off calling the sort function until after the heading change is actually rendered for the user.
The animation one sounds promising...but from the description it sounds to me like it runs just before rendering, and I want after rendering.
The idle one also sounds good in that it sounds like the rendering will be done before it runs...but if other stuff is going on it may take a long time for idle to actually happen.
What I've used in the past was setTimeout with a value of 0, because that's what Stackoverflow said to do.
- SimeVidas 8y agoSeveral seconds of JavaScript execution? That sounds like something that would be best performed in a web worker. If you can pass the data to a worker and have it generate the final markup, you won’t have to worry about timers or execution time, since the worker doesn’t block the main thread.
- nfriedly 8y agoIt might be better to use two calls. First call requestAnimationFrame and in the first callback, set the column and make a second rAF or rIC call. In the second callback, do your sorting. Also, it should be possible to do the sorting in a way that mostly doesn't block the browser. The data itself (if it lives outside of the DOM) can be sorted in a web worker. The DOM obviously blocks the UI thread, but you could do small batches of changes with a rAF or a rIC in-between each. Or, you know, just stick with setTimeout(..., 0) since that's already working for you.
- TheAceOfHearts 8y agoIf the list is REALLY long you should consider sorting it from a web worker. You might also want to consider implementing windowing or using a library, that way you only render what's visible on-screen. However this has its own caveats, so I'd suggest considering it carefully. For example, it'll break browser search for non-visible items. If you can get away with it I'd definitely suggest sticking with a traditional <table> element. Can we get an idea of the size of this list?
- tzs 8y agoThis was several years ago, so there is some haziness in my memory. There was an internal tool written in PHP that that put up a form for customer support people to enter information about transactions they were interested in, then queried the customer database to find matching transaction. It then output a page with the filled-in search form and a table with the results. There was no JavaScript used on that page. Just a simple form and the table. It didn't happen often, but occasionally the table could have many thousands of rows. The project to add sorting was one of those "it would be nice if we could sort this...I bet there's code on Stackoverflow that I can just grab and drop in between things I'm actually supposed to be working on today!" things.