stateSave'd table shows empty if last page saved no longer exists

stateSave'd table shows empty if last page saved no longer exists

kjdion84kjdion84 Posts: 9Questions: 3Answers: 0

When a tables state is saved on a specific page, lets say 6, there is a bug when you return to that table if page 6 no longer exists, and the table states "No matching records found", even though there are.

How do I fix this so that if the stateSave'd page no longer exists, it will just set the page to 1 and redraw the table?

I've tried the following code and it does not work:

            drawCallback: function (settings) {
                var api = this.api();

                if (api.rows({page: 'current'}).count() == 0) {
                    api.page('first');
                }
            }

Answers

  • kjdion84kjdion84 Posts: 9Questions: 3Answers: 0

    BTW, this is using serverSide DataTables.

  • kjdion84kjdion84 Posts: 9Questions: 3Answers: 0

    This works but is not elegant whatsoever and it clears the ENTIRE state, not just the current page of the state:

                drawCallback: function (settings) {
                    var api = this.api();
    
                    // if row count 0, always set page to 1
                    if (api.rows({page: 'current'}).count() == 0) {
                        api.state.clear();
                        window.location.reload();
                    }
                }
    
  • kjdion84kjdion84 Posts: 9Questions: 3Answers: 0
    edited August 2016

    This works better as it only changes the page, but its still kind of gross because it requires reloading the page:

                drawCallback: function (settings) {
                    var api = this.api();
    
                    // if row count 0, always set page to 1
                    if (api.rows({page: 'current'}).count() == 0) {
                        api.page('first').state.save();
                        window.location.reload();
                    }
                }
    
  • kjdion84kjdion84 Posts: 9Questions: 3Answers: 0

    Nevermind, the above examples are still bad because if the table actually has no rows it will keep refreshing the page in an endless loop.

  • kjdion84kjdion84 Posts: 9Questions: 3Answers: 0
    edited August 2016

    OK, I've figured out how to only reload the page if datatables thinks there are 0 rows even though there are records:

                drawCallback: function (settings) {
                    var api = this.api();
    
                    // if row count 0, always set page to 1
                    if (settings._iRecordsTotal > 0 && api.rows().count() == 0) {
                        api.page('first').state.save();
                        window.location.reload();
                    }
                }
    

    This works great, but there is still the issue of having to reload the page because the draw() function does not work correctly in the callback. It breaks the browser when you add it because it creates an endless loop.

    If anyone has a solution for having to refresh the page in order for this to work, I'm all ears.

    Perhaps a fix for this bug could be included in the DataTables core.

  • kjdion84kjdion84 Posts: 9Questions: 3Answers: 0

    Yeah the above solution isn't even ideal either, because if there is a current search saved where there are 0 results (even though there are non-filtered records) the page just keeps refreshing.

  • allanallan Posts: 63,812Questions: 1Answers: 10,516 Site admin

    Thanks for flagging this up. I've made a note of it and will get it addressed in the core. Thanks for posting your workaround for now.

    Allan

This discussion has been closed.