How to fall back when there is no data on the current page?

How to fall back when there is no data on the current page?

luisrortegaluisrortega Posts: 79Questions: 6Answers: 1

Hi, I have the following situation where the user might be on page 10 (10 items per page), some events on the server might clear the data on the server leaving only 5 records. Since I refresh from a timer, out of the blue, the software displays no data. If the user is not aware that the page number is currently empty, it will think there is no data in the table, is not until they manually change to page one, that they notice there is more data on the database.

How can I detect this, and automatically move to either last page?

Thanks,
Luis

This question has an accepted answers - jump to answer

Answers

  • luisrortegaluisrortega Posts: 79Questions: 6Answers: 1

  • luisrortegaluisrortega Posts: 79Questions: 6Answers: 1

    I found a work around... I'm sure we can improve over the code... but here we go...

    $(document).on('xhr.dt', function ( e, settings, json ) {
      if (json.recordsFiltered > 0 && json.data.length==0){
        // I wonder if there is a more direct way to access the table instance...
        $("#" + e.target.id).DataTable().page('last').draw(false);
      }
    });
    
  • colincolin Posts: 15,143Questions: 1Answers: 2,586
    Answer ✓

    Hi @luisrortega ,

    You can change

        $("#" + e.target.id).DataTable().page('last').draw(false);
    

    to be

        $(this).DataTable().page('last').draw(false);
    

    slightly cleaner...

    Cheers,

    Colin

  • luisrortegaluisrortega Posts: 79Questions: 6Answers: 1

    @colin thanks!

  • luisrortegaluisrortega Posts: 79Questions: 6Answers: 1

    I think I celebrated too early... on my end works as expected... on the customer test-bed, loop forever... it triggers another request (the first one is expected), but then it keeps triggering over and over again...

  • luisrortegaluisrortega Posts: 79Questions: 6Answers: 1

    I think I celebrated too early... on my end works as expected... on the customer test-bed, loop forever... it triggers another request (the first one is expected), but then it keeps triggering over and over again...

    And once is on that loop, it does not get out ever (or until I refresh the whole page). Not even manually changing the page to page one...

  • colincolin Posts: 15,143Questions: 1Answers: 2,586

    Hi @luisrortega ,

    I tried it here, to see how it would behave, and it seems to truncate the data as I would expect. Could you modify that example to demonstrate the problem, please.

    Cheers,

    Colin

  • luisrortegaluisrortega Posts: 79Questions: 6Answers: 1

    Hi Colin, I'm not sure if the behavior will be the same when the data is local vs when the data is served from a back-end server.

    Your sample seems to work without a problem, unfortunately, I can't replicate the same code. The upper example does work regardless. I might be doing more work than need it, but it so far it works.

    Luis

This discussion has been closed.