Search code examples
jqueryfiltertablesorter

jquery TableSorter - How do I turn off filters dynamically


I am using TableSorter Version 2.28.1. I have filters turned on.

 widgets: ["zebra", "filter"]

I want to be able to turn the filters off or on in my code before displaying the table. This based upon parameters coming in from the previous page.

I am using C# and the table on the page is a .net Gridview control.

Anyone have any ideas?


Solution

  • Use a combination of the applyWidgetId and removeWidget methods to toggle the filter widget (demo):

    HTML

    <button type="button">Add Filter</button>
    <table class="tablesorter">...</table>
    

    Script

    $(function() {
        var $table = $('table');
    
        $('button').click(function(){
        var btn = $(this),
            addWidget = /add/i.test(btn.text());
        if (addWidget) {
          btn.text('Remove Filter');
          $table.trigger('applyWidgetId', 'filter');
        } else {
          btn.text('Add Filter');
          $table.trigger('removeWidget', 'filter');
        }
        return false;
      });
    
      $table.tablesorter({
        theme: 'blue',
        widgets: ['zebra']
      });
    });