Search code examples
javascriptjqueryhtmltablesorter

Best practices of exporting records from HTML table


I'm using tablesorter as the view. I have a table of records and there are 3 scenarios of export:

  1. Export all from all pages
  2. Export only selected by a checkbox
  3. Export only filtered records

I'm a bit confusing of how can I implement these scenarios... Here are my assumptions and doubts:

  1. Export all - just use $('#table tr'). Yeah, it will really export all
  2. Export only selected - use again $('#table tr') and for each item check if it the checkbox is enabled
  3. Filtered records. And there I'm confusing. I can use $('#table tr:visible'), but what if I have 12 filtered records, but only 10 records are on the page and another 2 are on another page?

Are there some best-practices of how to derermine which records should be exported (into csv/xls) for these 3 cases?


Solution

  • The output widget has a output_savedRows option which has the following settings:

    • "filtered" which outputs rows that match the filter query. It does this by using the following selector:

      $('tr').not('.filtered')
      

      a class added by the filter_filteredRow option

    • "visible" which only outputs visible rows. Rows hidden by the pager, filter or set to display: none will not be included in the output. The visible selector is used in this case:

      $('tr:visible')
      
    • "all" will output all rows. This includes hidden & filtered rows.

      $('tr')
      

    • I plan to add a new setting "selected" in v2.22.2, which will use a new option output_selected that includes a row class name that the output widget uses to include in the output.

      $('tr.selected')