Notes

  • This page shows you how to add a few jQuery UI widgets to interact with the filter widget using the filter_formatter option.
  • Custom filter widget option filter_formatter was added in version 2.7.7.
  • This widget "should" work with tablesorter v2.0.5.
  • jQuery v1.4.3+ required.

jQuery UI Single Slider ("Rank" column)

  • This example shows how you can add a jQuery UI slider to filter column content.
  • The filter_formatter function provided in the extra "jquery.tablesorter.widgets-filter-formatter.js" file is used to add this custom control within the filter row.
  • Make sure to include all values within the selected range, otherwise rows outside of this range will be forever hidden.
  • Add the following code to apply a slider to filter a column:
    $(function() {
    
      $("table").tablesorter({
        theme: 'blue',
        // hidden filter input/selects will resize the columns, so try to minimize the change
        widthFixed : true,
        // initialize zebra striping and filter widgets
        widgets: ["zebra", "filter"],
        widgetOptions : {
          // jQuery selector string of an element used to reset the filters
          filter_reset : 'button.reset',
          // add custom selector elements to the filter row
          filter_formatter : {
            0 : function($cell, indx){
              return $.tablesorter.filterFormatter.uiSlider( $cell, indx, {
                // add any of the jQuery UI Slider options here
                value: 0,             // starting value
                min: 0,               // minimum value
                max: 100,             // maximum value
                delayed: true,        // delay search (set by filter_searchDelay)
                valueToHeader: false, // add current slider value to the header cell
                exactMatch: true,     // exact (true) or match (false)
                allText: 'all',       // text shown when the slider is at the minimum value
              });
            }
          }
        }
      });
    
    });
  • The tooltip above the slider is added using pure css, which is included in the "filter.formatter.css" file, but it won't work in IE7 and older. But, you set the valueToHeader option to true to add the slider value to the header cell above the filter.
  • Another option named exactMatch was added to allow exact or general matching of column content.
  • Notice that the left-most value, zero in this case, will clear the column filter to allow a method to show all column content. You can modify the "all" text using the allText option.
  • A search delay was added in v2.7.11 (time set by filter_searchDelay option). It can be disabled by setting the delayed option to false.

jQuery UI Range Slider ("Age" and "Total" columns)

  • This example shows how you can add a jQuery UI range slider to filter column content.
  • The filter_formatter function provided in the extra "jquery.tablesorter.widgets-filter-formatter.js" file is used to add this custom control within the filter row.
  • Make sure to include all values within the selected range, otherwise rows outside of this range will be forever hidden.
  • The range slider is actually the same as the single slider described above, but was built to handle a range of values.
  • Add the following code to apply a range slider to filter a column:
    $(function() {
    
      $("table").tablesorter({
        theme: 'blue',
        // hidden filter input/selects will resize the columns, so try to minimize the change
        widthFixed : true,
        // initialize zebra striping and filter widgets
        widgets: ["zebra", "filter"],
        widgetOptions : {
          // jQuery selector string of an element used to reset the filters
          filter_reset : 'button.reset',
          // add custom selector elements to the filter row
          filter_formatter : {
    
            // Age column
            3 : function($cell, indx){
              return $.tablesorter.filterFormatter.uiRange( $cell, indx, {
                values: [1, 70],     // starting range
                min: 1,              // minimum value
                max: 70,             // maximum value
                delayed: true,       // delay search (set by filter_searchDelay)
                exactMatch: true,    // exact (true) or match (false)
                valueToHeader: false // add current slider value to the header cell
              });
            },
    
            // Total column
            4 : function($cell, indx){
              return $.tablesorter.filterFormatter.uiRange( $cell, indx, {
                values: [1, 160],    // starting range
                min: 1,              // minimum value
                max: 160,            // maximum value
                delayed: true,       // delay search (set by filter_searchDelay)
                exactMatch: true,    // exact (true) or match (false)
                valueToHeader: false // add current slider value to the header cell
              });
            }
    
          }
        }
      });
    
    });
  • The tooltip above the slider is added using pure css, which is included in the "filter.formatter.css" file, but it won't work in IE7 and older. But, you set the valueToHeader option to true to add the slider value to the header cell above the filter.
  • Another option named exactMatch was added to allow exact or general matching of column content.
  • A search delay was added in v2.7.11 (time set by filter_searchDelay option). It can be disabled by setting the delayed option to false.

jQuery UI Spinner ("Discount" column)

  • This example shows how you can add a jQuery UI spinner to filter column content.
  • The filter_formatter function provided in the extra "jquery.tablesorter.widgets-filter-formatter.js" file is used to add this custom control within the filter row.
  • Add the following code to apply a spinner to filter a column:
    $(function() {
    
      $("table").tablesorter({
        theme: 'blue',
        // hidden filter input/selects will resize the columns, so try to minimize the change
        widthFixed : true,
        // initialize zebra striping and filter widgets
        widgets: ["zebra", "filter"],
        widgetOptions : {
          // jQuery selector string of an element used to reset the filters
          filter_reset : 'button.reset',
          // add custom selector elements to the filter row
          filter_formatter : {
            5 : function($cell, indx){
              return $.tablesorter.filterFormatter.uiSpinner( $cell, indx, {
                min : 0,
                max : 45,
                value: 1,
                step: 1,
                delayed: true,
                addToggle: true,
                exactMatch: true,
                numberFormat: "n"
              });
            }
          }
        }
      });
    
    });
  • This is the only jQuery UI widget that includes a toggle button. The toggle button is added by default, but if you don't want it, set the addToggle option to false. Without the toggle button, the filter is always active.
  • Another option named exactMatch was added to allow exact or general matching of column content.
  • A search delay was added in v2.7.11 (time set by filter_searchDelay option). It can be disabled by setting the delayed option to false.

jQuery UI Datepicker Range Selector ("Date" column)

  • This example shows how you can add a jQuery UI Datepicker range to filter column content.
  • The filter_formatter function provided in the extra "jquery.tablesorter.widgets-filter-formatter.js" file is used to add this custom control within the filter row.
  • This code follows the date range example from the jQuery UI docs.
  • Add the following code to apply a datepicker range selector to the filter row:
    $(function() {
    
      $("table").tablesorter({
        theme: 'blue',
        // hidden filter input/selects will resize the columns, so try to minimize the change
        widthFixed : true,
        // initialize zebra striping and filter widgets
        widgets: ["zebra", "filter"],
        widgetOptions : {
          // jQuery selector string of an element used to reset the filters
          filter_reset : 'button.reset',
          // add custom selector elements to the filter row
          filter_formatter : {
            6 : function($cell, indx){
              return $.tablesorter.filterFormatter.uiDatepicker( $cell, indx, {
                from : '12/1/2012',
                to   : '2/1/2014',
                changeMonth: true,
                changeYear : true
              });
            },
          }
        }
      });
    
    });
  • Note that the datepicker options are slightly different from the default datepicker options:
    • Instead of using the defaultDate option of the datepicker widget, it has a from and to option to fullfill that purpose.
    • The options added to this function will be applied to both the from and to datepicker inputs.

Custom Filter Formatter Function Information

If you want to write your own custom filter formatter function, there are certain requirements that should be met:
  • Required input element:
    • If your selector isn't an input (e.g. jQuery UI Slider), then you'll need to return an input of type hidden which gets updated by the selector with the filter query for the column.
      filter_formatter : {
        0 : function($cell, indx) {
          var $input = $('<input type="hidden">').appendTo($cell);
          // add your selector code here
          return $input;
        }
      }
    • If the input contains a value that doesn't match a standard filter syntax, then you'll need to return an input of type hidden with the correct format.
    • This returned input element should to be attached to the $cell.
    • The returned input should have a "search" event triggered upon it after being updated.
  • Some method should be added to show the user the current value of the selector - update a data attribute for css3 tooltips, or update the header cell.
  • A reset function needs to also be included; bind to the filterReset event and clear out or disable your custom selector when triggered.
    $cell.closest('table').bind('filterReset', function(){ /* update the input here */ });
  • If your selector needs a parsed value to work with, add the filter-parsed class name to the header cell above the filter, use this code to do that:
    $cell.closest('thead').find('th[data-column=' + indx + ']').addClass('filter-parsed');
  • Since, by default, the filter only matches cell content, a 1 in the filter will show all rows with a one no matter where it is located. So, if you need an exact match, add an equal sign to the end of the value (1=). This forces the filter to exactly match the value in the search input.
  • To include a search delay, trigger the search on the hidden input and pass a boolean. If true or undefined, the search will be delayed and not delayed if false. Delay time set by filter_searchDelay option).
    $input.val( newVal ).trigger('search', false); // no search delay

Demo

Rank Color Name Age Total Discount Date
1 #ff0000 Johnson 25 $5.95 22% Jun 26, 2013 7:22 AM
11 #00ff00 Hibert 12 $2.99 5% Aug 21, 2013 12:21 PM
12 #0000ff Henry 51 $42.29 18% Oct 13, 2013 1:15 PM
51 #00ffff Parker 28 $9.99 20% Jul 6, 2013 8:14 AM
21 #ffff00 Hood 33 $19.99 25% Dec 10, 2012 5:14 AM
013 #ff0000 Kent 18 $15.89 45% Jan 12, 2013 11:14 AM
005 #00ff00 Bruce 45 $153.19 45% Jan 18, 2014 9:12 AM
10 #00ffff Alex 3 $5.29 4% Jan 8, 2013 5:11 PM
16 #ffff00 Franco 24 $14.19 14% Jan 14, 2014 11:23 AM
66 #000000 Evans 22 $13.19 11% Jan 18, 2013 9:12 AM
100 #ffffff Brenda 18 $55.20 15% Feb 12, 2013 7:23 PM
55 #ffff00 Bronson 65 $123.00 32% Jan 20, 2014 1:12 PM
9 #000000 Martha 25 $22.09 17% Jun 11, 2013 10:55 AM

Page Header

<!-- jQuery UI for range slider -->
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/themes/cupertino/jquery-ui.css">
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/jquery-ui.min.js"></script>

<!-- tablesorter plugin -->
<link rel="stylesheet" href="../css/theme.blue.css">
<script src="../js/jquery.tablesorter.js"></script>
<script src="../js/jquery.tablesorter.widgets.js"></script>

<!-- filter formatter code -->
<link rel="stylesheet" href="../css/filter.formatter.css">
<script src="../js/jquery.tablesorter.widgets-filter-formatter.js"></script>

Javascript


HTML



Next up: jQuery custom filter widget formatter (part 2) ››