How to "select" page on preInit

How to "select" page on preInit

markzzzmarkzzz Posts: 49Questions: 8Answers: 1

Here's the code I have:

$(document).on('preInit.dt', function (e, settings) {
    // retrieve values from url's hash

    var apiSettings = new $.fn.dataTable.Api(settings);
    apiSettings.search(keysearch);
    apiSettings.page.len(length);
    apiSettings.page(page);
});

Which correctly set search string and page length to be passed to the server (and displayed on the table on draw), but I'm not able to set which "page". i.e. apiSettings.page(page); has no effect. start is always 0 (and not 10 if page is 3 and length is 5).

What's wrong?

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Use the displayStart option. Equally for page length use pageLength and for the search search.

    Allan

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421
    edited February 2023

    At that point data tables doesn't know what the paging is going to look like. I guess you would need to do that on "init", not on "preInit".

    This worked (actually it skipped the first 3 pages and landed on page 4. So the first page is obviously page 0).

    table
        .on ('init', function ( e, settings, json ) {        
            table.page(3).draw('page');
        } );
    

    Just saw @allan 's comment. Probably the better solution :-)

  • markzzzmarkzzz Posts: 49Questions: 8Answers: 1
    edited February 2023

    I need preInit because I want to set those values before ajax start and call server for filtering.

    @allan where do I find that displayStart? apiSettings.displayStart seems undefined...

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951
    edited February 2023

    Its an option you use in your Datatables init code. See the example in the displayStart docs. You should use the documented options rather than directly access the undocumented API as it could change but the documented options will still work.

    Kevin

  • markzzzmarkzzz Posts: 49Questions: 8Answers: 1
    edited February 2023

    @kthorngren I can't, since I'm making general handlers for different grid, not directly within each grid configuration.

    i.e. I have 20 grids which must have the same behaviour, without write 20 times the same things.

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    Can you set defaults as shown here?

    Kevin

  • markzzzmarkzzz Posts: 49Questions: 8Answers: 1

    Ok I see your point. I'll try and let you know in case of problem, thanks guys!

  • markzzzmarkzzz Posts: 49Questions: 8Answers: 1
    edited February 2023

    It seems to work.

    Thanks

Sign In or Register to comment.