Notes

  • This demo was added in v2.17.5, but modification using these instructions works for v2.13.3+; when the filter widget was restructured to allow the adding of custom filter search types.
  • In this demo, two additional search types have been added as an example
    • Start a search from beginning of the content using the ^ designator.
    • Search for a specific ending in the content using the $ designator.
  • Please review the following sections for more details.

Built-in Filters

The built-in filter search types include:
PriorityTypeDesignatorFunction
1regex/./mig$.tablesorter.filter.types.regex
2operators< <= >= >$.tablesorter.filter.types.operators
3not! or !=$.tablesorter.filter.types.notMatch
4exact" or =$.tablesorter.filter.types.exact
5and &&  or  and $.tablesorter.filter.types.and
6range -  or  to $.tablesorter.filter.types.range
7wild* or ?$.tablesorter.filter.types.wild
7or| or  or $.tablesorter.filter.types.wild (built-into wild)
8fuzzy~$.tablesorter.filter.types.fuzzy

How to add Custom filter types

  • The first step is to decide what you want your filter to do.
    • Should it look for a symbol at the beginning, middle or end of the filter?
    • Does the designator need spaces around it (e.g. " and " or " or ") to prevent problems? You wouldn't be able to find the country of "Andorra" if the "and" or "or" designators didn't require spaces.
    • Make sure to name your filter so that it doesn't interfere with already existing ones, unless your filter is meant to replace an existing one. See the "Built-in Filters" section for a full list of filter function names.
    • Within your filter, first test for your designator symbol.
      • If it exists within the filter, then do your comparison and return a boolean of true or false.
      • Four arguments are passed to the filter function:
        • filter - The exact text from the filter input (e.g. ^h).
        • iFilter - The text from the filter in all lower case for case insensitive searches.
        • exact - The exact (or parsed) text from the current table cell; the parsed text is passed when the column has a "filter-parsed" class name set.
        • iExact - The exact (or parsed) text in all lower case for case insensitive searches.
      • If your designator doesn't exist, you *must* return null to allow comparisons with other filter types.
      • Here is a basic example with extensive comments:
        // search for a match from the beginning of a string
        // "^l" matches "lion" but not "koala"
        $.tablesorter.filter.types.start = function( filter, iFilter, exact, iExact ) {
        	// test for filter type designator. In this example, "^" must be at the beginning of the filter
        	// for this test, the use of the case insensitive "iFilter" variable
        	// doesn't matter since we are only looking at the symbol
        	if ( /^\^/.test( iFilter ) ) {
        		// test the table content (exact or parsed) against the filter
        		// * Don't forget to remove the designator first ( the substring(1) part ), so "^h".substring(1) becomes "h"
        		// * In this case, we do care about using "iFilter" since we're comparing it with "iExact"
        		// * We use "indexOf" so that we know the match starts from the beginning of the string.
        		// * Your function should return a boolean indicating a match, or not.
        		return iExact.indexOf( iFilter.substring(1) ) === 0;
        	}
        	// ALWAYS return null if your custom filter type doesn't match
        	return null;
        };

How to remove filter types

  • If one of the built-in search types is interfering or bothersome to your users, then you can remove it using the following command (using fuzzy search as an example):
    $(function(){
    
    	// Remove fuzzy search
    	delete $.tablesorter.filter.types.fuzzy;
    
    	$('table').tablesorter({
    		theme: 'blue',
    		widgets: ['filter']
    	});
    
    });
  • The full list of built-in filter type functions can be found in the "Built-in Filters" section.

Localization

You can change the language of the searches used within the filter widget. This only applies to the "and", "or" and "to" (range) searches. For example, to change the localization to French, do the following:
// add French support
$.extend($.tablesorter.language, {
	to  : 'à',
	or  : 'ou',
	and : 'et'
});
This results in searches that can be performed as follows:
  • "and" search: Pierre et Franc or Pierre && Franc.
  • "or" search: Marie ou Claudette or Marie|Claudette
  • "to" search: 10 à 20 or 10 - 20
Note the symbolic searches never changed ( && , | and  - )
Important Even though the language settings don't include spaces, the user is still required to enter spaces in the filter to perform these searches, e.g. 1 à 10 (shows rows with numbers between 1 and 10).
If you want to support multiple languages, separate the language variables with a vertical bar (|, Shift + \):
// add French & Spanish support
$.extend($.tablesorter.language, {
	to  : 'à|a',
	or  : 'ou|o',
	and : 'et|y'
});

Demo

(beginning of word)
(end of word)

First NameLast NameCityAgeTotalDiscountDate
AaronJohnson SrAtlanta35$5.9522%Jun 26, 2004 7:22 AM
AaronJohnsonYuma12$2.995%Aug 21, 2009 12:21 PM
ClarkHenry JrTampa51$42.2918%Oct 13, 2000 1:15 PM
DenniHenryNew York28$9.9920%Jul 6, 2006 8:14 AM
JohnHoodBoston33$19.9925%Dec 10, 2002 5:14 AM
ClarkKent SrLos Angeles18$15.8944%Jan 12, 2003 11:14 AM
PeterKent EsqSeattle45$153.1944%Jan 18, 2021 9:12 AM
PeterJohnsMilwaukee13$5.294%Jan 8, 2012 5:11 PM
AaronEvanChicago24$14.1914%Jan 14, 2004 11:23 AM
BruceEvansUpland22$13.1911%Jan 18, 2007 9:12 AM
ClarkMcMastersPheonix18$55.2015%Feb 12, 2010 7:23 PM
DennisMastersIndianapolis65$123.0032%Jan 20, 2001 1:12 PM
JohnHoodFort Worth25$22.0917%Jun 11, 2011 10:55 AM

Javascript


	

HTML


	

Next up: jQuery custom filter widget formatter (jQuery UI widgets) ››