how to search and go to last page in one line of code?

how to search and go to last page in one line of code?

luisluixluisluix Posts: 6Questions: 2Answers: 0

I am trying to clear all my filters, then go the last page of information... however I am unable to do it.
If I do this:
dt.search('').draw(false);
dt.page('last').draw(false);
I have a race condition, but if I put a timeout between the lines, it will work just fine.

I am trying to avoid the timeout, however using both in one line:
dt.search('').page('last').draw(false);
doesnt seem to work as intended, If I had a previous filter with one page, and run this code I will still be on page one. (page last wont work)

is there a better way to do both? (I want to run a search, and have datatables be in the last page when it comes back)

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,819Questions: 1Answers: 10,517 Site admin

    Interesting one. The problem is that the search doesn't get enacted until the next draw - so page('last') without a draw is still operating on the previous data state. The idea of that is that you would be able to queue multiple changes for a single draw to operate on, although that backfires in this situation!

    I'm not clear on why your first two lines result in a race condition. Unless your draw is async (server-side processing?) that should work okay.

    Allan

  • luisluixluisluix Posts: 6Questions: 2Answers: 0

    yes its server side processing. Its not like an actual race condition, it seems like only the first search-draw happens, and the second one is ignored. But with the timeout both draws work okay.

  • allanallan Posts: 63,819Questions: 1Answers: 10,517 Site admin
    Answer ✓

    Got it. With SSP you could use:

    dt
      .search('')
      .one( 'draw', function () {
        dt.page('last').draw( false );
      } )
      .draw( false );
    

    Not ideal, but it would stop any timing issues.

    Allan

This discussion has been closed.