page.info().start is incorrect on preDraw
page.info().start is incorrect on preDraw
Stadly
Posts: 6Questions: 2Answers: 0
Link to test case: https://codepen.io/Stadly/pen/RwgweWV
Debugger code (debug.datatables.net): https://debug.datatables.net/agiruc
Error messages shown: None
Description of problem:
During the preDraw
event, the page.info().start
is not reliable.
Is there a reason for this, or is it a bug?
$(document).ready(function() {
$('#example').DataTable({
"displayStart": 10,
"preDrawCallback": function(settings) {
console.log(this.api().page.info().start); // 0 (incorrect)
},
"drawCallback": function(settings) {
console.log(this.api().page.info().start); // 10 (correct)
}
});
});
See the test case for details.
This question has an accepted answers - jump to answer
Answers
It is before the draw, so the parameters haven't fully been set. What is your goal here?
Allan
Sorry for the late response here. I had set the wrong notification settings, so didn't notice your reply until now.
My goal is to update the url with the current page so that sharing the link with someone else will open the same page for them.
If you open the test case link and change page (and page length or ordering, which can also lead to changing the page) a few times, you'll notice that the only time
page.info().start
is not reliable is on the first load. After that, the value is the same onpreDraw
anddraw
.Since the value is updated before
preDraw
except for the first load, I guess it should be possible to have it up-to-date also for the first load? It isstart
,end
, andpage
that is incorrect on the first load. The rest ofpage.info()
is always reliable.The reason that I want to do the update on
preDraw
instead ofdraw
, is that it can take some time between the two when using server-side processing. I want the url to be updated when the chosen page is clicked, and not have to wait for the data to load.Updating the url on the
page
event would have been an option, but that event is not fired when the page is changed due to ordering or changing the page length. So then I would have to listen to bothpage
,length
andorder
. Bothpage
andlength
are fired beforepreDraw
, butorder
is fired beforedraw
- so after the data has been loaded from the server. So then the url would not be updated when the page is changed, but after the data is loaded.Maybe you can use the
preXhr
to get thestart
and other paging information from the JSON sent to the server.Kevin
Thanks, Kevin. Just tested it, and using
preXhr
works I would, however, prefer a solution that works regardless of using server-side or client-side processing. With this solution, I would have to conditionally put the logic either onpreXhr
ordraw
.Who would be able to figure out whether
page.info()
truly cannot be fully populated on the firstpreDraw
, or if that's easily fixed?Many thanks for the explanation. Yes, I think it is fair to expect the
start
parameter frompage.info()
to be correct at the pre-draw point (there could be an argument that actually it should be the last value, since we haven't done a draw yet - but I can't really see a use case for that...).I've committed a change that fixes your example. The change will be in the nightly soon.
Regards,
Allan
Thanks, allan! That was fast
Are there any plans to make a release containing this soon? When looking at packagist, it seems the previous release came out in May 2020, and the one before that in October 2019. So I guess you're not in the habit of making a new release for every change
That's our legacy package - the new one is at: https://packagist.org/packages/datatables.net/datatables.net . This is the full list of releases the latest of which is September this year.
You are right though, we don't package things up for every change - we tend to group them and do monthly or bi-monthly releases. There are only a couple of minor changes so far since 1.11.3, but I think we'll do a release fairly soon all the same - particularly for the Chrome sub-pixel issue.
Allan
Oh, nice! Thanks