api.page(n) does not work in preInit or init

api.page(n) does not work in preInit or init

mpenmpen Posts: 2Questions: 1Answers: 0

I've described the issue here: http://stackoverflow.com/q/41083450/65387 but I now believe this is a bug, not an error on my end.

It appears to be the same issue described here: https://datatables.net/forums/discussion/comment/86374/#Comment_86374

Basically, if you try setting the page in the 'preInit' event, nothing happens. If you try setting it in the 'init' event, it will highlight the correct page but show data from the first page.

Adding a 0ms timeout does work but causes a 2nd ajax request and redraw which I would like to avoid.

I don't think I can use displayStart as suggested in the thread unless there's a way to override the settings inside the preInit event.

Answers

  • allanallan Posts: 61,956Questions: 1Answers: 10,158 Site admin

    I made a small test case here: http://live.datatables.net/hekamicu/1/edit .

    I think that isn't working because of an interaction from the state saving code. It has an initial draw start that is used in preference to one set by the API in preInit.

    Is that a bug? Yes, I think it probably is and it should be fixed, thanks for pointing it out! To be honest I've never thought of doing it that way before! I'm not clear on why you can't use displayStart? If you can use preInit, why can't you use displayStart?

    Allan

    p.s. By the time init is triggered, its too late. The initial load and draw has already been done.

  • mpenmpen Posts: 2Questions: 1Answers: 0

    Hi Allan, thanks for replying.

    Can I modify the settings from inside preInit? displayStart would probably work for me, but I don't want to copy this code into every DataTable I use throughout my site. I'm trying to do a catch-all by listening for the preInit event on document.

    The only way I can think of doing that is to extend DataTables somehow so that I can tamper with the settings (i.e. add the displayStart option) before forwarding the settings to DataTables.

  • allanallan Posts: 61,956Questions: 1Answers: 10,158 Site admin

    Can I modify the settings from inside preInit?

    Some of them! By that time some will already have been set for the initial run. I think the display start is relatively unique in that it has that special code for the saved value.

    Unfortunately that code was added way back in 2010 before I used git to track the versions. I can't remember my exact thinking for that parameter! It was introduced in 1.4.0 (2008) which also introduced state saving. Looking at that code it was specifically for filtering and I think that due to the way DataTables now operates it can probably be removed.

    I've just tried a test build without it and it works okay. I've made a note to check into that.

    What you could do is set both the iInitDisplayStart and settings._iDisplayStart properties of the settings object in the preInit event handler. Messy, but this is getting into the guts of DataTables from years ago! It really needs a clean out :smile:.

    Allan

This discussion has been closed.