How to repopulate manually and keeping sorting?
How to repopulate manually and keeping sorting?
I use DataTables on an existing table (which I repopulate with my own logic) to get sorting and scrolling. All works fine, I put bRetrieve to true to enable the reinitialization of the table, as to avoid the error message about being unable to reinstantiate an already instantiated datatable. However, after I empty and repopulate tbody, the sorting goes missing.
is there any DataTables.Calibrate/Reset/Recalculate or something I could use? Or do I need to hack my way around it if I don't want to use DataTable infrastructure to repopulate the table?
This question has an accepted answers - jump to answer
Answers
Sounds like the append FAQ. In short, use the API to manipulate the table.
Allan
Thank you for the answer to the question.
I'm curious though, wouldn't it be more developer friendly to have that option that I'm asking about? DataTables is obviously an augmentation for a table, thus it could easily be seen a layer above a table that changes it's projection in regards to interface and layout.
But if the usage of DataTables requires us, after initialization, to use the DT API even for changing the basic generic layer of the table, doesn't this come with some drawbacks that could have been avoided taking another design approach of the DT layer?
For certain. And if there was an API that was cross platform and cross browser will provided that information I'd jump at it. However, the DOM mutation API is very immature and not at all well supported in older browsers (which I still have to support) - see caniuse.
Almost certainly. Every single design decision has some benefits and some drawbacks. In this case, the benefit was that it works across just about every browser ever, and can be done very efficiently. Using DOM mutation just wasn't possible back when DataTables was first created (since it didn't exist) and is still impractical today since DataTables needs to support a wide range of browsers.
I do plan to write a blog post about using DOM mutation events with DataTables in future, but for the moment, I'd suggest using the DataTables API in the same way that you have to use the jQuery API to get the most from jQuery.
Allan