Handle pagination overflow with server-side
Handle pagination overflow with server-side
Hi,
I got around some trouble lately, basically I happen to have to store some parameters on my implementations of DataTables, for the record I store things like iDisplayStart, iDisplayLength, column filters and some custom filters (Note I use DataTables 1.9, I planned to upgrade but yet I miss time for this). The saving part is fine and working well, so does the restoring part. This is not my issue.
My issue comes that some external filters will affect the record count,
Say for example I open a global client list, I'll have like hundred of pages and we go to the 42th page. Now I open the same page again, but filtered on a specific area, and now I just have a dozen of client. So it will restore the 42th page with iDisplayStart, which is expected (we still would like to have the page restored if we were not filtered on an area). Here comes troubles, because it will show a empty list obviously.
It's certainly not a big trouble, as an user could just click on the page 1 to get back the records, but well... users.... ;]
Anyways, so I started a workaround on the server side, simply that just after doing the filtered count, I check if it's lower than iDisplayStart, and if it does, I set iDisplayStart to 0 and continue the total count and records queries.
This works fine and show the 1st page in case of overflow, but I'm kind of perfectionist, and there is still a bit of a glitch I would like to solve (which lead me here, sorry for the long prelude, I like to give context) : Simply, I didn't yet find a way to warn DataTables that I just changed the page, so the pagination is wrong, the client side will still think that it is on page 42, so the pagination buttons are not selected and the info text is wrong too.
So I would like to have your thought on this, and if there is a easy workaround to send back from a server to DataTables the new DisplayStart, or if there is none.
Thanks
Answers
Hi,
You might be interested in this pull request. Unfortunately I haven't had a chance to integrate it into DataTables yet, but it sounds like this is more or less what you are looking for it think? The ability for the server to say "actually this isn't page x it is page y" (where y will usually be 0 I would think).
Does that sound about right?
Allan
Yeah, that's actually a very accurate and simple way to explain it.
Setting 0 is most likely the best choice in any case, but letting the server answer telling DataTables the page is better.
I checked the pull request changes, is it that simple to implement ? if yes it's pretty cool, definitely look like DataTables is well coded :)
I haven't tried it yet - but yes, I think that should be around about right :-).
For anyone using 1.10 - If you do try it with the latest version of DataTables, let me know how you get on with it. Things changed a lot in that area for v1.10, although that particular part should be about the same (although it expects a camelCase variable by default in the result now).
Allan