Notes

  • Oct 27, 2014 - added a demo to the home wiki page showing how to apply jQuery's selectmenu widget to a filter cell without using the filter formatter functions.
  • As of v2.17.5, if writing a custom filterFormatter function, you'll need to call an additional function after initialization has completed so the "filterInit" event isn't triggered prematurely. See the "Custom Filter Formatter Function Information" section below for more details.
  • In v2.17.0, the filter_formatter column can also be referenced by using a jQuery selector (e.g. class name or ID).
  • In v2.16.0, the filter_placeholder option was added. See the jQuery UI Datepicker Range Selector section for more details.
  • In v2.15.0, the following changes were made:
    • These updated filter widget functions are not completely backward compatible with older versions of the filter widget. Please update both!
    • Added compare & selected options:
      • These options allow the adding of a comparison operator selector to the cell (e.g. >, >=, <, <=, etc).
      • If any cellText is included, it is now wrapped in a label with a class name of "compare-select-label" and "compare-select-label#" (where "#" is the column index).
      • The selector has a class name of "compare-select" and "compare-select#" (where "#" is the column index)
      • Whichever type of input that is added to the cell is then wrapped in a div with class "compare-select-wrapper" and "compare-select-wrapper#" (where "#" is the column index).
      • These class names allow styling of an individual filter to keep elements in line, or however you wish to style it.
        <!-- rendered filter cell (first column) -->
        <td>
        	<!-- cellText -->
        	<label class="compare-select-label compare-select-label0">Rank:</label>
        	<!-- compare: [ '=', '>=', '<=' ] -->
        	<select class="compare-select compare-select0">
        		<option>=</option>
        		<option>&gt;=</option>
        		<option selected>&lt;=</option>
        	</select>
        	<!-- wrapped selector element -->
        	<div class="compare-select-wrapper compare-select-wrapper0">
        		<div class="slider slider0><!-- jQuery UI Slider gets added here --></div>
        	</div>
        	<!-- hidden input -->
        	<input class="filter tablesorter-filter" type="hidden" value="" data-column="0">
        </td>
    • Filter reset now sets these filters to their default values, not an empty string.
    • Updated to now properly restore saved filters.
    • Added endOfDay option for jQuery UI Datepicker.
      • When true search dates will include all times from the date chosen when a comparison is made of dates "less than" the set date.
      • Example 1: if a table entry has a date of "Jan 14, 2014 11:23 AM" and the filter search is set to , the table entry will be included in the search; the default set time would otherwise be "1/14/2014 00:00:00" and not include the entry from "11:23 AM". So, the endOfDay option sets the time to "23:59:59".
      • Example 2: if searching for one specific date, this option will now search for all times within that day. For example, searching for , and the results will include dates from 1/20/2014 00:00:00 to 1/20/2014 23:59:59.
      • When comparing dates greater than the set date, the time will be set to midnight; so this option will not be applied.
      • Example 3: in two date inputs, the endOfDay time is only applied to the "to" input; search for
      • This option is available in both the comparison (one input) and range (two inputs; "to" date input only) date pickers.
  • As of tablesorter version 2.9+, this widget can no longer be applied to versions of tablesorter prior to version 2.8.
  • 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.
  • jQuery v1.4.3+ required.

jQuery UI Single Slider ("Rank" and "Age" columns)

  • In v2.15.0 the compare option was updated to allow adding a selector along with the input. The selected option allows choosing the default setting.
  • In v2.10.1 the compare option was added. This allows comparing the slider's value to the column value. The slider in the Age column is selecting values greater than or equal to itself.
  • 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.

  • This example shows how you can add a jQuery UI slider to filter column content.
  • The filter_formatter function provided in the extra "widget-filter-formatter-jui.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: 'jui',
        // 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", "uitheme"],
        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 : {
    
            // Rank (jQuery selector added v2.17.0)
            'th:contains("Rank")' : function($cell, indx){
              return $.tablesorter.filterFormatter.uiSlider( $cell, indx, {
                // filterFormatter options
                cellText : 'Rank:',
                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
                compare : [ '=', '>=', '<=' ], // any comparison would override the exactMatch option
                selected : 2,         // zero-based index of compare starting selected value
    
                // add any of the jQuery UI Slider options here (http://api.jqueryui.com/slider/)
                value: 100,           // starting value
                min: 0,               // minimum value
                max: 100              // maximum value
              });
            },
    
            // Age
            1 : function($cell, indx){
              return $.tablesorter.filterFormatter.uiSlider( $cell, indx, {
                delayed: false,       // delay search (set by filter_searchDelay)
                valueToHeader: false, // add current slider value to the header cell
                exactMatch: false,    // exact (true) or match (false)
                allText: 'all',       // text shown when slider is at the minimum value; ignored when compare has a value
                compare: [ '=', '>=', '<=' ], // search values greater/less than selected value; overrides exactMatch
                selected : 1,         // zero-based index of compare starting selected value
    
                // add any of the jQuery UI Slider options here (http://api.jqueryui.com/slider/)
                value: 1,             // starting value
                min: 1,               // minimum value
                max: 65               // maximum 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.

jQuery UI Range Slider ("Total" column)

  • This example shows how you can add a jQuery UI range slider to filter column content.
  • The filter_formatter function provided in the extra "widget-filter-formatter-jui.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: 'jui',
        // 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", "uitheme"],
        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 : {
    
            // Total column (jQuery selector added v2.17.0)
            '.total' : function($cell, indx){
              return $.tablesorter.filterFormatter.uiRange( $cell, indx, {
                delayed: true,        // delay search (set by filter_searchDelay)
                valueToHeader: false, // add current slider value to the header cell
    
                // add any of the jQuery UI Slider options (for range selection) here (http://api.jqueryui.com/slider/)
                values: [1, 160],     // starting range
                min: 1,               // minimum value
                max: 160              // maximum 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.
  • 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)

  • In v2.15.0 the compare option was updated to allow adding a selector along with the input. The selected option allows choosing the default setting.

  • This example shows how you can add a jQuery UI spinner to filter column content.
  • The filter_formatter function provided in the extra "widget-filter-formatter-jui.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: 'jui',
        // 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", "uitheme"],
        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 : {
    
            // Discount column
            3 : function($cell, indx){
              return $.tablesorter.filterFormatter.uiSpinner( $cell, indx, {
                delayed: true,    // delay search (set by filter_searchDelay)
                addToggle: false, // add a toggle switch to activate/deactive the search
                exactMatch: true, // exact (true) or match (false)
                compare : [ '', '=', '>=', '<=' ], // search values greater/less than selected value; overrides exactMatch
                selected : 2,     // zero-based index of compare starting selected value
    
                // add any of the jQuery UI Spinner options here (http://api.jqueryui.com/spinner/)
                min : 0,
                max : 45,
                value: 1,
                step: 1
              });
            }
          }
        }
      });
    
    });
  • 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 Comparison ("Date (one input)" column)

  • In v2.15.0
    • The compare option was updated to allow adding a selector along with the input. The selected option allows choosing the default setting.
    • A endOfDay option was added, which when a <= comparison is made, will include all times within the selected day. Try searching for ; it basically sets the time of the selected day to end at 23:59:59.
    • Additionally, when endOfDay option is true and using an exact (=) comparison, all times within that selected day will be included - try searching for to note that the day include various times.

  • This example shows how you can add a jQuery UI Datepicker to compare to filter column content.
  • The filter_formatter function provided in the extra "widget-filter-formatter-jui.js" file is used to add this custom control within the filter row.
  • This code follows the default functionality example from the jQuery UI docs.
  • Add the following code to apply a datepicker comparison selector to the filter row:
    $(function() {
    
      $("table").tablesorter({
        theme: 'jui',
        // 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", "uitheme"],
        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 : {
    
            // Date (one input)
            4 : function($cell, indx){
              return $.tablesorter.filterFormatter.uiDateCompare( $cell, indx, {
                cellText : 'Dates', // text added before the input
                compare : [ '', '=', '>=', '<=' ], // search values greater/less than selected value; overrides exactMatch
                selected : 3,       // zero-based index of compare starting selected value
    
                // add any of the jQuery UI Datepicker options here (http://api.jqueryui.com/datepicker/)
                // defaultDate : '1/1/2014', // default date
                changeMonth : true,
                changeYear : true
              });
            }
          }
        }
      });
    
    });
  • There is one issue with this comparison script, and that is with dates that contain a time:
    • Table data that contains dates with times will be parsed into values that include time.
    • So if the compare option is set to =, it may not show any filtered table rows as none match that date at midnight. To fix this, either remove times from the table data, use a column parser that ignores the time, or use the Datepicker range selector and set the "from" input to the desired day and the "to" input to be the next day (at midnight).
    • Using a compare of <= will only show filtered dates from the selected day at midnight with earlier dates; i.e. a cell with the selected date but a time set to noon will not be visible. As of v2.10/8, the comparison of dates less than the selected date, now adds a time of 11:59:59 so it will include times within the selected date.

jQuery UI Datepicker Range Selector ("Date (two inputs)" column)

  • In v2.16.0, a filter_placeholder option was added:
    • The option adds a search input placeholder and select (first option) text, but it also includes placeholder text for both the "from" and "to" inputs
    • Set it as follows:
      filter_placeholder : {
      	search : '', // any default search inputs including the datepicker comparison
      	from   : 'Date from...', // datepicker range "from" placeholder
      	to     : 'Date to...'  // datepicker range "to" placeholder
      }
      
    • Note The browser must support the placeholder attribute before this text will be visible.
  • In v2.15.0, an endOfDay option was added, which when true and searching within one day, all times within that selected day will be included - try searching for to note that the day include various times.
  • This example shows how you can add a jQuery UI Datepicker range to filter column content.
  • The filter_formatter function provided in the extra "widget-filter-formatter-jui.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.
  • Updated two input functions so that if the "to" input is empty, all dates greater than the "from" date are shown. If the "from" input is empty, all dates less than the "to" input date are shown (v2.10.1).
  • Add the following code to apply a datepicker range selector to the filter row:
    $(function() {
    
      $("table").tablesorter({
        theme: 'jui',
        // 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", "uitheme"],
        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 : {
    
            // Date (two inputs)
            5 : function($cell, indx){
              return $.tablesorter.filterFormatter.uiDatepicker( $cell, indx, {
                // add any of the jQuery UI Datepicker options here (http://api.jqueryui.com/datepicker/)
                // from : '8/1/2013', // starting date
                // to   : '1/18/2014',  // ending date
                textFrom: 'from',   // "from" label text
                textTo: 'to',       // "to" label text
                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.
    • As of v2.10.8, the comparison of dates less than the selected "to" date, now adds a time of 11:59:59 so it will include times within the selected dates.

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 */ });
  • In v2.15.0, in order to update your custom input with any saved filter searches, you will need to bind to the filterFomatterUpdate event to get the saved filter value from the hidden input and apply it to your custom input.
    $cell.closest('table').bind('filterFomatterUpdate', function(){
    	var savedSearch = $cell.find('input[type=hidden]').val();
    	// apply this saved search to your custom input
    	$.tablesorter.filter.formatterUpdated($cell, indx); // new v2.17.5
    });
    As of v2.17.5, you need to call $.tablesorter.filter.formatterUpdated($cell, indx); so the filter widget knows when all inputs have completed initialization, and fire the "filterInit" event.

  • 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; modify the delay time set by changing the filter_searchDelay option.
    $input.val( newVal ).trigger('search', false); // no search delay

Demo


Rank Age Total (range) Discount Date (one input) Date (two inputs; range)
125$5.9522%Jun 26, 2013 7:22 AMJun 26, 2013 7:22 AM
1112$82.995%Aug 21, 2013 12:21 PMAug 21, 2013 12:21 PM
1251$99.2918%Oct 13, 2013 1:15 PMOct 13, 2013 1:15 PM
5128$9.9920%Jul 6, 2013 8:14 AMJul 6, 2013 8:14 AM
2133$19.9925%Dec 10, 2012 5:14 AMDec 10, 2012 5:14 AM
01318$65.8945%Jan 12, 2013 11:14 AMJan 12, 2013 11:14 AM
00545$153.1945%Jan 18, 2014 9:12 AMJan 18, 2014 9:12 AM
103$5.294%Jan 8, 2013 5:11 PMJan 8, 2013 5:11 PM
1624$14.1914%Jan 14, 2014 11:23 AMJan 14, 2014 11:23 AM
6622$13.1911%Jan 18, 2013 9:12 AMJan 18, 2013 9:12 AM
10018$55.2015%Feb 12, 2013 7:23 PMFeb 12, 2013 7:23 PM
5565$123.0032%Jan 20, 2014 1:12 PMJan 20, 2014 1:12 PM
925$22.0917%Jun 11, 2013 10:55 AMJun 11, 2013 10:55 AM
1312$19.9918%Jan 20, 2014 7:45 PMJan 20, 2014 7:45 PM
7358$129.1939%Jan 20, 2014 10:11 AMJan 20, 2014 10:11 AM

Page Header

<!-- jQuery UI for range slider -->
<link rel="stylesheet" href="css/jquery-ui.min.css">
<script src="js/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/widgets/widget-filter-formatter-jui.js"></script>

CSS


	

Javascript


	

HTML


	

Next up: jQuery custom filter widget formatter (HTML5 Elements) ››