bug with server-side pagination

bug with server-side pagination

eaglesheaglesh Posts: 7Questions: 1Answers: 0

Hi, I'm using server-side processing and I can't set page like this:

table.page(pageIndex).ajax.url('someUrl').load(function() {
            table.columns.adjust();
            window.scrollTo(0, scroll);
        }, false);

I try to change source dynamically that can help me quickly jump into some record on the table to display some another data and jump back. When I jump into record I save page index and sroll position, when I jump back I want to restore page index. But if the page wich I jump from contains only one page then _fnPageChange() can't allow me set page > 0.
P.S. Sorry for my English ((

Answers

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    Thanks for your question - however, per the forum rules can you link to a test case showing the issue please. This will allow the issue to be debugged.

    Information on how to create a test page, if you can't provide a link to your own page can be found here.

    Thanks,
    Allan

  • eaglesheaglesh Posts: 7Questions: 1Answers: 0
    edited August 2016

    Thank you for so fast reply.
    I think it's difficult to set-up a remotely hosted test case with server-side proccessing. But how can I load data from server but from needed page (for example from 6th page)? I think it will help me to resolve my problem.

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    The start parameter that DataTables sends to the server tells the server where the data should start the data from (i.e. page = start * length).

    Beyond that, I really would need a test case i'm afraid.

    Allan

  • eaglesheaglesh Posts: 7Questions: 1Answers: 0
    edited August 2016

    I have found out in debug mode that when I call
    table.page(pageIndex).ajax.url('someUrl').load(null, false)
    start parameter calculated in _fnPageChange based on state (or settings) of current page. But if current page contains no rows then start will be reseted here in _fnPageChange of jquey.dataTables.js:

               var
                start     = settings._iDisplayStart,
                len       = settings._iDisplayLength,
                records   = settings.fnRecordsDisplay();
        
            if ( records === 0 || len === -1 )
            {
                start = 0;
            }
    

    But I have changed source data by .ajax.url('someUrl') and at someUrl there are much data.

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    A page cannot have zero rows. It wouldn't exist if it had zero rows.

    Allan

  • eaglesheaglesh Posts: 7Questions: 1Answers: 0

    Sorry, I mean no records in the table, for example first source server returned answer with zero records.

  • eaglesheaglesh Posts: 7Questions: 1Answers: 0
    edited August 2016

    Here some test case for debug:
    https://output.jsbin.com/yunuzugiwo

    All buttons run scripts that should change source and page. But all of them change only source, and all ways (buttons) will send start=0 to the server

    P.S. Before retry with any other button you should reload page to reset "draw" count parameter

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    Don't call the load() method - since you are server-side processing it will load the data on the next data anyway.

    However, there is another issue - your first page loads only 1 row. So trying to set it to page 3 isn't going to work as there is no page 3.

    So:

    way 1) Doesn't work because there is no page(3) to turn to
    way 2) Doesn't work because again, the is no page(3) and that is executed before the new data is loaded
    way 3) Does work, but it sends a redundant request to the server (one for the load() and one for the page(3).

    Allan

  • eaglesheaglesh Posts: 7Questions: 1Answers: 0

    So, how should I switch data source? For example, current data source returned only one record. Then I want to switch to another source with more than thousand records and load from page 3?
    I tried to switch source, and then reload with page 3 (way 3). But seems like callback executes earlier than settings (or state) get updated (because the new source has much data and many pages, but still start=0 is send to the server).

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    Way 3 appears like it should work. It will send a redundant request as I noted, but then it will send another request to get the third page. You could wait for the first page to load before then sending the request for the third page which might be more reliable.

    There is no way to tell DataTables to page past what it has been told is the current length of the table (which it appears is what you are struggling to do at the moment).

    Allan

  • eaglesheaglesh Posts: 7Questions: 1Answers: 0

    This is works for me.

    table.ajax.url(someUrl).load(function() {
                setTimeout(restorePage, 0);
            });
    

    where restorePage is a function like:

    function restorePage() {
            table.page(pageIndex).draw('page');
            table.columns.adjust();
            window.scrollTo(0, scroll);
        };
    

    I am noob in javascript. I killed much time to find solution. Function setTimeout is very helpful in my case.

This discussion has been closed.