stateSave'd table shows empty if last page saved no longer exists
stateSave'd table shows empty if last page saved no longer exists
kjdion84
Posts: 9Questions: 3Answers: 0
When a tables state is saved on a specific page, lets say 6, there is a bug when you return to that table if page 6 no longer exists, and the table states "No matching records found", even though there are.
How do I fix this so that if the stateSave'd page no longer exists, it will just set the page to 1 and redraw the table?
I've tried the following code and it does not work:
drawCallback: function (settings) {
var api = this.api();
if (api.rows({page: 'current'}).count() == 0) {
api.page('first');
}
}
This discussion has been closed.
Answers
BTW, this is using serverSide DataTables.
This works but is not elegant whatsoever and it clears the ENTIRE state, not just the current page of the state:
This works better as it only changes the page, but its still kind of gross because it requires reloading the page:
Nevermind, the above examples are still bad because if the table actually has no rows it will keep refreshing the page in an endless loop.
OK, I've figured out how to only reload the page if datatables thinks there are 0 rows even though there are records:
This works great, but there is still the issue of having to reload the page because the draw() function does not work correctly in the callback. It breaks the browser when you add it because it creates an endless loop.
If anyone has a solution for having to refresh the page in order for this to work, I'm all ears.
Perhaps a fix for this bug could be included in the DataTables core.
Yeah the above solution isn't even ideal either, because if there is a current search saved where there are 0 results (even though there are non-filtered records) the page just keeps refreshing.
Thanks for flagging this up. I've made a note of it and will get it addressed in the core. Thanks for posting your workaround for now.
Allan