# tablesorter

## Custom Filter Widget Formatter (jQuery UI widgets)

### Flexible client-side table sorting

Back to documentation

### Notes

- 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)

- 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: '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 : {

              0 : function($cell, indx){
                return $.tablesorter.filterFormatter.uiSlider( $cell, indx, {
                  // add any of the jQuery UI Slider options here (http://api.jqueryui.com/slider/)
                  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
                  compare: ''           // any comparison would override the exactMatch option
                });
              },

              1 : function($cell, indx){
                return $.tablesorter.filterFormatter.uiSlider( $cell, indx, {
                  // add any of the jQuery UI Slider options here (http://api.jqueryui.com/slider/)
                  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: false,    // exact (true) or match (false)
                  allText: 'all',       // text shown when slider is at the minimum value; ignored when compare has a value
                  compare: '>='         // show values >= selected value; overrides exactMatch
                });
              }

            }
          }
        });

      });

- 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`.
- 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.

### 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 "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: '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
              2 : function($cell, indx){
                return $.tablesorter.filterFormatter.uiRange( $cell, indx, {
                  // 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
                  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: '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 : {
              3 : function($cell, indx){
                return $.tablesorter.filterFormatter.uiSpinner( $cell, indx, {
                  // add any of the jQuery UI Spinner options here (http://api.jqueryui.com/spinner/)
                  min : 0,
                  max : 45,
                  value: 1,
                  step: 1,
                  delayed: true,
                  addToggle: true,
                  exactMatch: true,
                  compare : ''
                });
              }
            }
          }
        });

      });

- 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)

- 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 "jquery.tablesorter.widgets-filter-formatter.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 : {
              4 : function($cell, indx){
                return $.tablesorter.filterFormatter.uiDateCompare( $cell, indx, {
                  // add any of the jQuery UI Datepicker options here (http://api.jqueryui.com/datepicker/)
                  // defaultDate : '1/1/2014', // default date
                  cellText : 'dates >=', // text added before the input
                  changeMonth : true,
                  changeYear : true,
                  compare : '>='
                });
              }
            }
          }
        });

      });

- 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)

- 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.
- 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 : {
              6 : 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 */ });

- 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 (exact) | Age (greater than) | Total (range) | Discount (exact) | Date (one input; greater than) | Date (two inputs; range)
--- | --- | --- | --- | --- | ---
1 | 25 | $5.95 | 22% | Jun 26, 2013 7:22 AM | Jun 26, 2013 7:22 AM
11 | 12 | $82.99 | 5% | Aug 21, 2013 12:21 PM | Aug 21, 2013 12:21 PM
12 | 51 | $99.29 | 18% | Oct 13, 2013 1:15 PM | Oct 13, 2013 1:15 PM
51 | 28 | $9.99 | 20% | Jul 6, 2013 8:14 AM | Jul 6, 2013 8:14 AM
21 | 33 | $19.99 | 25% | Dec 10, 2012 5:14 AM | Dec 10, 2012 5:14 AM
013 | 18 | $65.89 | 45% | Jan 12, 2013 11:14 AM | Jan 12, 2013 11:14 AM
005 | 45 | $153.19 | 45% | Jan 18, 2014 9:12 AM | Jan 18, 2014 9:12 AM
10 | 3 | $5.29 | 4% | Jan 8, 2013 5:11 PM | Jan 8, 2013 5:11 PM
16 | 24 | $14.19 | 14% | Jan 14, 2014 11:23 AM | Jan 14, 2014 11:23 AM
66 | 22 | $13.19 | 11% | Jan 18, 2013 9:12 AM | Jan 18, 2013 9:12 AM
100 | 18 | $55.20 | 15% | Feb 12, 2013 7:23 PM | Feb 12, 2013 7:23 PM
55 | 65 | $123.00 | 32% | Jan 20, 2014 1:12 PM | Jan 20, 2014 1:12 PM
9 | 25 | $22.09 | 17% | Jun 11, 2013 10:55 AM | 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 (HTML5 Elements) ››