page.info().start is incorrect on preDraw

page.info().start is incorrect on preDraw

StadlyStadly Posts: 5Questions: 1Answers: 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

  • allanallan Posts: 54,881Questions: 1Answers: 8,601 Site admin

    It is before the draw, so the parameters haven't fully been set. What is your goal here?

    Allan

  • StadlyStadly Posts: 5Questions: 1Answers: 0
    edited October 20

    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 on preDraw and draw.

    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 is start, end, and page that is incorrect on the first load. The rest of page.info() is always reliable.

    The reason that I want to do the update on preDraw instead of draw, 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 both page, length and order. Both page and length are fired before preDraw, but order is fired before draw - 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.

  • kthorngrenkthorngren Posts: 14,086Questions: 25Answers: 3,322

    Maybe you can use the preXhr to get the start and other paging information from the JSON sent to the server.

    Kevin

  • StadlyStadly Posts: 5Questions: 1Answers: 0

    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 on preXhr or draw.

    Who would be able to figure out whether page.info() truly cannot be fully populated on the first preDraw, or if that's easily fixed?

  • allanallan Posts: 54,881Questions: 1Answers: 8,601 Site admin
    Answer ✓

    Many thanks for the explanation. Yes, I think it is fair to expect the start parameter from page.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

  • StadlyStadly Posts: 5Questions: 1Answers: 0

    Thanks, allan! That was fast :smiley:

    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 :lol:

  • allanallan Posts: 54,881Questions: 1Answers: 8,601 Site admin

    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

  • StadlyStadly Posts: 5Questions: 1Answers: 0

    Oh, nice! Thanks :)

Sign In or Register to comment.