TableSorter: Filter Results Based on Search Query

So, you want to use the TableSorter jQuery plugin and you want to be able to search the table and filter the results. Attached is a companion plugin we’ve written, called tablesorterFilter, which will extend tablesorter to provide real-time filtering of rows which match a search query!

Usage

<script type="text/javascript">
  jQuery(document).ready(function() {
    $("#myTable")
      .tablesorter({debug: false, widgets: ['zebra'], sortList: [[0,0]]})
      .tablesorterFilter({filterContainer: $("#filter-box"),
                          filterClearContainer: $("#filter-clear-button"),
                          filterColumns: [0],
                          filterCaseSensitive: false});
  });
</script>
Search: <input name="filter" id="filter-box" value="" maxlength="30" size="30" type="text">
<input id="filter-clear-button" type="submit" value="Clear"/>
 
<table id="myTable">
  <thead>
    <tr>
      <th>Last Name</th>
      <th>First Name</th>
      <th>Email</th>
      <th>Web Site</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Smith</td>
      <td>John</td>
      <td>jsmith@gmail.com</td>
      <td>http://www.jsmith.com</td>
    </tr>
    <tr>
      <td>Doe</td>
      <td>Jason</td>
      <td>jdoe@hotmail.com</td>
      <td>http://www.jdoe.com</td>
    </tr>
  </tbody>
</table>

Configuration

tablesorterFilter takes up to six parameters:

  • filterContainer - The DOM id of the input box where the user will type the search string.
  • filterClearContainer - (optional) The DOM id of the button, image, or whatever which will clear the search string and reset the table to it’s original, unfiltered state.
  • filterColumns - An array of columns, starting at 0, which will be searched.
  • filterCaseSensitive - (optional) Boolean stating whether the search string is case sensitive. The default is false.
  • filterWaitTime - (optional) Time after last key press to start filtering. The default is 500 (milliseconds).
  • filterFunction - (optional) Custom function to filter by column text. The default is the plugin function has_words.

Requirements

jQuery version 1.2.1 or higher and a slightly modified jquery.tablesorter.js version 2.0.3.

The modification to the original tablesorter plugin is the addition of a few lines which will cache all of the rows so they can be searched. You can download the modified jquery.tablesorter.js code below.

Download

jquery.tablesorter.filter.js

jquery.tablesorter.js 2.0.3 (modified for tablesorterFilter)

Updated blog post on 1-April-2009 to reflect two additional configuration parameters.

Tags: ,

Friday, August 22nd, 2008 Programming

88 Comments to TableSorter: Filter Results Based on Search Query

  • RenĂ© says:

    Hi all,

    Great plugin, I want to use checkboxes that narrows down my search to one single column or multiple columns.
    As exemple, i have 3 checkboxes one for each column i have in my table.
    If I select 1 of them I want to search only in that column and if I select more 1 then I want that he is search in more colums.

    Can someone tell me how I can do that.

    Greetz

    René

  • astoinov says:

    I have the same problem as @Satheesh. In IE when i delete the text in the filter input the browser is freezing. I found that this is happening when $(table).trigger(”update”); in doFilter() is executed. Can anyone help me with this problem ?

  • Ryan says:

    Have you ever attempted to run the plugin with noConflict()? I’m having issues where the table doesn’t return any data. Thanks for the plugin. :)

  • Leave a Reply

     
    Located in beautifully weird Austin, Texas, Compulsivo has been obsessing over writing simple Web software using Ruby on Rails for over two years. Learn more
    Prefinery: Simple, online beta management software'

    Launch a private beta for your Web application in minutes. Prefinery takes care of collecting e-mail addresses, generating invitation codes, and sending invitations for your private beta. Your customers never leave your site, and e-mail invitations are sent from your address.