Search code examples
jquerytablesorterpagerjquery-pagination

tablesorter with pager - force update when table is empty


I am using tablesorter with the pager plugin. All of the paging is done server-side so I am using the ajaxUrl, ajaxObject and customUrl options of the pager to handle all the ajax calls needed whenever the user is flipping pages.

Inside the customUrl option I have a function that retrieves a timerange that the user defines in an external field, and add it into the url, in order to filter only entries within that range.

When the user changes that timerange I remove all of the rows from the table using $("tr", tableElement).remove(); and then $(tableElement).trigger("update"); to trigger the ajax call by the pager plugin.

This works when the table has entries in it: The entries get removed, then the update trigger issues a new ajax request, and gets the rows within the selected timerange inside the table with proper pagination.

Problem: When the currently selected range is empty (i.e. no entries were found), and the user selects a new range, the update trigger doesn't see any change in the data/entries (within the table's structure), thus the ajax request doesn't get issued.

Is there a way to force the table to update itself and run the ajax query procedure?


Solution

  • Instead of clearing the table and triggering an update, there is a different method to use to force the pager to update:

    • In the current version (2.18.4), the easiest way to do this is to clear an internal stored variable, then trigger a page change:

      $('table')[0].config.pager.last.ajaxUrl = '';
      $('table').trigger('pageSet');
      

      I know, it's not pretty

    • Because of this question and past issues with the pager needing to be forcibly updated, the next update of tablesorter (either to v2.18.5 or v2.19.0) will include a new pager method which will forcibly update the pager:

      $('table').trigger('pagerUpdate');