├── .gitignore ├── examples ├── samples-common.js ├── samples-styles.css ├── index.html ├── filtertable-ignore-class.html ├── filtertable-min-rows.html ├── filtertable-quick.html ├── filtertable-sample.html ├── filtertable-any-term.html ├── filtertable-existing-input.html ├── filtertable-all-terms.html ├── filtertable-min-chars.html ├── filtertable-striping.html └── filtertable-ignore-columns.html ├── .editorconfig ├── bower.json ├── FAQ.md ├── package.json ├── filterTable.jquery.json ├── MIT-LICENSE.txt ├── jquery.filtertable.min.js ├── CHANGELOG.md ├── README.md └── jquery.filtertable.js /.gitignore: -------------------------------------------------------------------------------- 1 | # OS generated files # 2 | ###################### 3 | .DS_Store 4 | .DS_Store? 5 | ._* 6 | .Spotlight-V100 7 | .Trashes 8 | Icon? 9 | ehthumbs.db 10 | Thumbs.db 11 | 12 | # Codekit files 13 | config.codekit* 14 | .codekit-cache 15 | examples-codekit/ 16 | -------------------------------------------------------------------------------- /examples/samples-common.js: -------------------------------------------------------------------------------- 1 | ;(function($) { 2 | $(document).ready(function() { 3 | // allow the example links to fill and trigger the filtering 4 | $('.fill-filter').on('click', function(ev) { 5 | ev.preventDefault(); 6 | $('.filter-table input').val($(this).text()).focus().trigger('click'); 7 | }); 8 | }); 9 | })(jQuery); 10 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: http://EditorConfig.org 2 | 3 | # 4 | root = true 5 | 6 | # all files 7 | [*] 8 | end_of_line = lf 9 | insert_final_newline = true 10 | indent_style = space 11 | indent_size = 4 12 | charset = utf-8 13 | trim_trailing_whitespace = true 14 | 15 | # sass files 16 | [*.{scss,sass}] 17 | indent_size = 2 18 | 19 | # markdown 20 | [*.md] 21 | trim_trailing_whitespace = false 22 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "filterTable", 3 | "description": "Live searching/filtering for HTML tables with optional quick search buttons.", 4 | "keywords": [ 5 | "table", 6 | "search", 7 | "filter" 8 | ], 9 | "authors": [ 10 | { 11 | "name": "Sunny Walker", 12 | "homepage": "http://www.miraclesalad.com/" 13 | } 14 | ], 15 | "license": "MIT", 16 | "homepage": "http://sunnywalker.github.io/jQuery.FilterTable/", 17 | "dependencies": { 18 | "jquery": ">=1.4" 19 | }, 20 | "main": "jquery.filtertable.js", 21 | "ignore": [ 22 | "examples/", 23 | "**/.*" 24 | ] 25 | } 26 | -------------------------------------------------------------------------------- /FAQ.md: -------------------------------------------------------------------------------- 1 | # jQuery.filterTable Frequently Asked Questions 2 | 3 | ## How do I keep the header and/or footer rows from disappearing on filter? 4 | 5 | Ensure your header row(s) are in `` tags, your data row(s) are in `
` tags, and your footer row(s) are in `` tags. The plugin only filters `tbody` rows, but if you do not specify `thead`, `tbody`, `tfoot`, the brower puts all of your rows in a `tbody` block. 6 | 7 | ## Why isn't the filtering showing up on small tables? 8 | 9 | The plugin will not show for tables with fewer than `8` rows by default. To have the filter feature always appear, set the `minRows` option accordingly. For example: 10 | 11 | $('table').filterTable({minRows: 0}); 12 | 13 | Or see the [minRows example page](http://sunnywalker.github.io/jQuery.FilterTable/examples/filtertable-min-rows.html). 14 | 15 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jquery.filtertable", 3 | "version": "1.5.6", 4 | "description": "Live searching/filtering for HTML tables with optional quick search buttons.", 5 | "main": "jquery.filtertable.js", 6 | "directories": { 7 | "example": "examples" 8 | }, 9 | "scripts": { 10 | "test": "echo \"Error: no test specified\" && exit 1" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/sunnywalker/jQuery.FilterTable.git" 15 | }, 16 | "dependencies": { 17 | "jquery": ">=1.4" 18 | }, 19 | "keywords": [ 20 | "table", 21 | "search", 22 | "filter" 23 | ], 24 | "author": "Sunny Walker (http://www.miraclesalad.com/)", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/sunnywalker/jQuery.FilterTable/issues" 28 | }, 29 | "homepage": "http://sunnywalker.github.io/jQuery.FilterTable/" 30 | } 31 | -------------------------------------------------------------------------------- /filterTable.jquery.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "filterTable", 3 | "title": "jQuery FilterTable", 4 | "description": "Live searching/filtering for HTML tables with optional quick search buttons.", 5 | "keywords": [ 6 | "table", 7 | "search", 8 | "filter" 9 | ], 10 | "version": "1.5.2", 11 | "author": { 12 | "name": "Sunny Walker", 13 | "url": "http://www.miraclesalad.com/" 14 | }, 15 | "licenses": [ 16 | { 17 | "type": "MIT", 18 | "url": "https://github.com/sunnywalker/jQuery.FilterTable/blob/master/MIT-LICENSE.txt" 19 | } 20 | ], 21 | "bugs": "https://github.com/sunnywalker/jQuery.FilterTable/issues", 22 | "homepage": "http://sunnywalker.github.io/jQuery.FilterTable/", 23 | "docs": "http://sunnywalker.github.io/jQuery.FilterTable/", 24 | "demo": "http://sunnywalker.github.io/jQuery.FilterTable/filtertable-quick.html", 25 | "dependencies": { 26 | "jquery": ">=1.4" 27 | } 28 | } -------------------------------------------------------------------------------- /MIT-LICENSE.txt: -------------------------------------------------------------------------------- 1 | (The MIT License) 2 | 3 | Copyright (c) 2012 Sunny WalkerThe following are very basic pages which use the jQuery Filter Table Plugin.
12 |This is a sample of the jQuery.FilterTable plugin using the ignoreClass option.
In this sample, cells with the no-filter class are ignored—not filtered. Also in this sample, the ignored cells are styled via CSS to help identify that they are ignored.
For example:
23 |no-filter class.no-filter class.no-filter class.| Fruit | 32 |Mesocarp Color(s) When Ripe | 33 |
|---|---|
| Avocado | Green |
| Banana | Yellow |
| Dragon fruit | Pink, White |
| Guava | Pink |
| Lilikoʻi (Passion fruit) | Orange, Yellow |
| Lychee | White |
| Mango | Orange, Yellow |
| Papaya | Orange, Red, Yellow |
| Pineapple | White, Yellow |
| Star fruit | Green, White, Yellow |
$('table').filterTable({
50 | ignoreClass: 'no-filter'
51 | });
52 |
53 |
54 |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/examples/filtertable-min-rows.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | This is a sample of the jQuery.FilterTable plugin which illustrates the minRows option.
By default, the plugin will add filtering only to tables with at least 8 rows in the tbody. In this example, the table has more than the minimum rows, so filtering is added.
| Fruit | 23 |Mesocarp Color(s) When Ripe | 24 |
|---|---|
| Avocado | Green |
| Banana | Yellow |
| Dragon fruit | Pink, White |
| Guava | Pink |
| Lilikoʻi (Passion fruit) | Orange, Yellow |
| Lychee | White |
| Mango | Orange, Yellow |
| Papaya | Orange, Red, Yellow |
| Pineapple | White, Yellow |
| Star fruit | Green, White, Yellow |
$('.example-1').filterTable();
41 |
42 | By default, the plugin will add filtering only to tables with at least 8 rows in the tbody. On a table with fewer than this minimum, no filtering is added.
| Fruit | 48 |Mesocarp Color(s) When Ripe | 49 |
|---|---|
| Avocado | Green |
| Banana | Yellow |
| Mango | Orange, Yellow |
| Papaya | Orange, Red, Yellow |
| Pineapple | White, Yellow |
$('.example-2').filterTable();
61 |
62 | Setting the minRows option to 0 will always add filtering to a table, no matter how many rows the tbody contains.
| Fruit | 68 |Mesocarp Color(s) When Ripe | 69 |
|---|---|
| Avocado | Green |
| Banana | Yellow |
| Mango | Orange, Yellow |
| Papaya | Orange, Red, Yellow |
| Pineapple | White, Yellow |
$('.example-3').filterTable({
81 | minRows: 0
82 | });
83 |
84 |
85 |
86 |
93 |
94 |
--------------------------------------------------------------------------------
/jquery.filtertable.min.js:
--------------------------------------------------------------------------------
1 | /**
2 | * jquery.filterTable
3 | *
4 | * This plugin will add a search filter to tables. When typing in the filter,
5 | * any rows that do not contain the filter will be hidden.
6 | *
7 | * Utilizes bindWithDelay() if available. https://github.com/bgrins/bindWithDelay
8 | *
9 | * @version v1.5.7
10 | * @author Sunny Walker, swalker@hawaii.edu
11 | * @license MIT
12 | */
13 | !function($){var e=$.fn.jquery.split("."),t=parseFloat(e[0]),i=parseFloat(e[1]);t<2&&i<8?($.expr[":"].filterTableFind=function(e,t,i){return $(e).text().toUpperCase().indexOf(i[3].toUpperCase().replace(/"""/g,'"').replace(/"\\"/g,"\\"))>=0},$.expr[":"].filterTableFindAny=function(e,t,i){var n=i[3].split(/[\s,]/),r=[];return $.each(n,function(e,t){var i=t.replace(/^\s+|\s$/g,"");i&&r.push(i)}),!!r.length&&function(e){var t=!1;return $.each(r,function(i,n){if($(e).text().toUpperCase().indexOf(n.toUpperCase().replace(/"""/g,'"').replace(/"\\"/g,"\\"))>=0)return t=!0,!1}),t}},$.expr[":"].filterTableFindAll=function(e,t,i){var n=i[3].split(/[\s,]/),r=[];return $.each(n,function(e,t){var i=t.replace(/^\s+|\s$/g,"");i&&r.push(i)}),!!r.length&&function(e){var t=0;return $.each(r,function(i,n){$(e).text().toUpperCase().indexOf(n.toUpperCase().replace(/"""/g,'"').replace(/"\\"/g,"\\"))>=0&&t++}),t===r.length}}):($.expr[":"].filterTableFind=jQuery.expr.createPseudo(function(e){return function(t){return $(t).text().toUpperCase().indexOf(e.toUpperCase().replace(/"""/g,'"').replace(/"\\"/g,"\\"))>=0}}),$.expr[":"].filterTableFindAny=jQuery.expr.createPseudo(function(e){var t=e.split(/[\s,]/),i=[];return $.each(t,function(e,t){var n=t.replace(/^\s+|\s$/g,"");n&&i.push(n)}),!!i.length&&function(e){var t=!1;return $.each(i,function(i,n){if($(e).text().toUpperCase().indexOf(n.toUpperCase().replace(/"""/g,'"').replace(/"\\"/g,"\\"))>=0)return t=!0,!1}),t}}),$.expr[":"].filterTableFindAll=jQuery.expr.createPseudo(function(e){var t=e.split(/[\s,]/),i=[];return $.each(t,function(e,t){var n=t.replace(/^\s+|\s$/g,"");n&&i.push(n)}),!!i.length&&function(e){var t=0;return $.each(i,function(i,n){$(e).text().toUpperCase().indexOf(n.toUpperCase().replace(/"""/g,'"').replace(/"\\"/g,"\\"))>=0&&t++}),t===i.length}})),$.fn.filterTable=function(e){var t={autofocus:!1,callback:null,containerClass:"filter-table",containerTag:"p",filterExpression:"filterTableFind",hideTFootOnFilter:!1,highlightClass:"alt",ignoreClass:"",ignoreColumns:[],inputSelector:null,inputName:"",inputType:"search",label:"Filter:",minChars:1,minRows:8,placeholder:"search this table",preventReturnKey:!0,quickList:[],quickListClass:"quick",quickListClear:"",quickListGroupTag:"",quickListTag:"a",visibleClass:"visible"},i=function(e){return e.replace(/&/g,"&").replace(/"/g,""").replace(//g,">")},n=$.extend({},t,e),r=function(e,t){var i=e.find("tbody");if(""===t||t.lengthThis is a sample of the jQuery.FilterTable plugin which shows the usage of the quickList optional parameter.
Note that the “neighbor island” quick list item works because within some cells exists code that is hidden with CSS: <span class="hidden">neighbor island</span>.
| Campus | 33 |Island | 34 |Fall 2011 Enrollment |
35 |
|---|---|---|
| University of Hawaiʻi at Hilo | 40 |Hawaiʻi neighbor island | 41 |4,100 | 42 |
| University of Hawaiʻi at Mānoa | 45 |Oʻahu | 46 |20,400 | 47 |
| University of Hawaiʻi—West Oʻahu | 50 |Oʻahu | 51 |1,600 | 52 |
| Hawaiʻi Community College | 55 |Hawaiʻi neighbor island | 56 |3,900 | 57 |
| Honolulu Community College | 60 |Oʻahu | 61 |4,600 | 62 |
| Kapiʻolani Community College | 65 |Oʻahu | 66 |9,000 | 67 |
| Kauaʻi Community College | 70 |Kauaʻi neighbor island | 71 |1,400 | 72 |
| Leeward Community College | 75 |Oʻahu | 76 |7,900 | 77 |
| Maui College | 80 |Maui neighbor island | 81 |4,500 | 82 |
| Windward Community College | 85 |Oʻahu | 86 |2,700 | 87 |
Note that while the filter is case-insensitive, it is UTF-8 aware so searching for oahu will not find Oʻahu.
$('table').filterTable({
94 | quickList: quickList: [
95 | 'Oʻahu',
96 | 'Hawaiʻi',
97 | 'university',
98 | 'college',
99 | 'neighbor island'
100 | ],
101 | quickListClear: '× clear filter'
102 | });
103 |
104 |
105 |
106 |
114 |
115 |
116 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # jQuery.filterTable Change Log
2 |
3 | ## 1.5.7
4 | - Support for `quickListClear` option that appends a quick list link which clears the filter.
5 |
6 | ## 1.5.6
7 | - Fixed filtering to work for special characters `'`, `"`, and `\`.
8 | - Minor documentation updates.
9 |
10 | ## 1.5.5
11 |
12 | - The filtering should now trigger automatically on input on iOS. For some reason, Safari iOS triggers a `blur` event on field change but not `keyup` or other related events.
13 | - A new feature for extending the plugin (option `filterExpression`) has been added. The default is unchanged—the literal filter string.
14 | - To search for _any_ of multiple terms, set `filterExpression` to `filterTableFindAny` to perform an OR search which delimits on space or comma characters in the filter field. Thank you to [Lukas](https://github.com/superlukas) for the implementation. An example page is included.
15 | - To search for _all_ of multiple terms, set `filterExpression` to `filterTableFindAll` to perform an AND search which delimits on space or comma characters in the filter field. Note that the matching is per-cell not per row, so each cell must have all terms to match. An example page is included.
16 | - A new feature for ignoring columns is available via the `ignoreColumns` option. Provide an array of column numbers (0-indexed) to ignore those columns during filtering. The default is no columns are ignored. An example page is included.
17 | - A new feature for ignoring cells with a specific class is available via the `ignoreClass` option. Provide a class name to ignore those cells during filtering. The default is no classes are ignored. An exampled page is included. Thanks to [geda0](https://github.com/geda0) for the idea.
18 | - Added a `minChars` option, thanks to [Darius Kazemi](https://github.com/dariusk), which specifies the minimum number of characters a user must enter into the filter field before filtering occurs. Default is 1, meaning the moment the user begins to type, filtering will occur.
19 | - Merged [Pierre Rudloff](https://github.com/Rudloff)’s Bower support.
20 | - Merged [Jason](https://github.com/deadbeef404)’s `minRows` bug fix.
21 | - Added an FAQ file.
22 |
23 | ## 1.5.4
24 |
25 | - Added a return key trap to the input filter field so that pressing return in the field should not submit any forms the table may be within.
26 | - The `preventReturnKey` option (`true` by default) has been added to allow you to switch back to the previous behavior of allowing the return key to submit forms.
27 |
28 | ## 1.5.3
29 |
30 | - **There is a potentially significant change in functionality in this version.** While the documentation offered the `inputSelector` option, within the code it was implemented as `filterSelector`. This has been corrected to match the documentation. Note that if you were previously using the `filterSelector` option to overcome this issue, you will need to change it to `inputSelector` to use the feature with this version.
31 |
32 | ## 1.5.2
33 |
34 | - Added an `inputSelector` option, thanks to [Pratik Thakkar](https://github.com/pratikt), which specifies a selector for an existing element to use instead of creating a new filter input field. There are some caveats of which to be aware:
35 | - If the element doesn't exist, a filter input field will be created as normal.
36 | - Because of quick lists and other options, this setting will be ignored and the filter input field will be created as normal if the resolution of the `inputSelector` returns more than one element.
37 |
38 | ## 1.5.1
39 |
40 | - Added an `autofocus` option, thanks to [Robert McLeod](https://github.com/penguinpowernz), which is disabled by default. Note that autofocus is generally a bad idea for accessibility reasons, but if you do not need to be compliant or don't want to support accessibility users, it's a nice user experience option.
41 |
42 | ## 1.5
43 |
44 | - **There is a potentially significant change in functionality in this version.** The callback is now called every time the search query changes. Previously it was only called when the change was a non-empty query. That is, the callback is now called when the query is cleared too.
45 | - Additional features have been taken from [Tomas Celizna](https://github.com/tomasc)'s CoffeeScript-based fork:
46 | - The quick list items can now be something other than anchor tags. See the `quickListTag` and `quickListGroupTag` options.
47 | - The filter query field can now have a name attribute assigned to it. See the `inputName` option.
48 | - The class applied to visible rows is now user changeable. See the `visibleClass` option.
49 | - The options in the documentation have been ordered alphabetically for easier scanning.
50 | - The internal pseudo selector is now created appropriately according to the jQuery version. (Pseudo selector generation changed in jQuery 1.8)
51 |
52 | ## 1.4
53 |
54 | - Fixed a bug with filtering rarely showing rows that did not have a match with the search query.
55 | - Added example pages.
56 | - Improved inline documentation of the source code.
57 |
58 | ## 1.3.1 (in spirit)
59 |
60 | - Added minified version of the plugin (thanks [Luke Stevenson](https://github.com/lucanos)).
61 |
62 | ## 1.3
63 |
64 | - The functionality is not reapplied to tables that have already been processed. This allows you to call `$(selector).filterTable()` again for dynamically created data without it affecting previously filtered tables.
65 |
66 | ## 1.2
67 |
68 | - Changed the default container class to `filter-table` from `table-filter` to be consistent with the plugin name.
69 | - Made the cell highlighting class an option rather than hard-coded.
70 |
71 | ## 1.1
72 |
73 | - Initial public release.
74 |
--------------------------------------------------------------------------------
/examples/filtertable-sample.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | This is a very plain sample of the jQuery.FilterTable plugin.
17 || # | 22 |President | 23 |Terms | 24 |Tenure | 25 |
|---|---|---|---|
| 1 | George Washington | two | 1789-1797 |
| 2 | John Adams | one | 1797-1801 |
| 3 | Thomas Jefferson | two | 1801-1809 |
| 4 | James Madison | two | 1809-1817 |
| 5 | James Monroe | two | 1817-1825 |
| 6 | John Quincy Adams | one | 1825-1829 |
| 7 | Andrew Jackson | two | 1829-1837 |
| 8 | Martin Van Buren | one | 1837-1841 |
| 9 | William Henry Harrison | one-partial | 1841 |
| 10 | John Tyler | one-partial | 1841-1845 |
| 11 | James Knox Polk | one | 1845-1849 |
| 12 | Zachary Taylor | one-partial | 1849-1850 |
| 13 | Millard Fillmore | one-partial | 1850-1853 |
| 14 | Franklin Pierce | one | 1853-1857 |
| 15 | James Buchanan | one | 1857-1861 |
| 16 | Abraham Lincoln | two-partial | 1861-1865 |
| 17 | Andrew Johnson | one-partial | 1865-1869 |
| 18 | Ulysses S. Grant | two | 1869-1877 |
| 19 | Rutherford Birchard Hayes | one | 1877-1881 |
| 20 | James Abram Garfield | one-partial | 1881 |
| 21 | Chester Alan Arthur | one-partial | 1881-1885 |
| 22 | Grover Cleveland | one | 1885-1889 |
| 23 | Benjamin Harrison | one | 1889-1893 |
| 24 | Grover Cleveland | one-again | 1893-1897 |
| 25 | William McKinley | two-partial | 1897-1901 |
| 26 | Theodore Roosevelt | two-partial | 1901-1909 |
| 27 | William Howard Taft | one | 1909-1913 |
| 28 | Woodrow Wilson | two | 1913-1921 |
| 29 | Warren Gamaliel Harding | two-partial | 1921-1923 |
| 30 | Calvin Coolidge | two-partial | 1923-1929 |
| 31 | Herbert Clark Hoover | one | 1929-1933 |
| 32 | Franklin Delano Roosevelt | four-partial | 1933-1945 |
| 33 | Harry S. Truman | two-partial | 1945-1953 |
| 34 | Dwight David Eisenhower | two | 1953-1961 |
| 35 | John Fitzgerald Kennedy | two-partial | 1961-1963 |
| 36 | Lyndon Baines Johnson | two-partial | 1963-1969 |
| 37 | Richard Milhous Nixon | two-partial | 1969-1974 |
| 38 | Gerald Rudolph Ford | two-partial | 1974-1977 |
| 39 | James Earl Carter, Jr. | one | 1977-1981 |
| 40 | Ronald Wilson Reagan | two | 1981-1989 |
| 41 | George Herbert Walker Bush | one | 1989-1993 |
| 42 | William Jefferson Clinton | two | 1993-2001 |
| 43 | George Walker Bush | two | 2001-2009 |
| 44 | Barack Hussein Obama | one | 2009- |
Data as of October, 2012.
75 |$('table').filterTable();
77 |
78 |
79 |
84 |
85 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # jQuery Filter Table Plugin
2 |
3 | This plugin will add a search filter to tables. When typing in the filter, any rows that do not contain the filter will be hidden.
4 |
5 | One can also define clickable shortcuts for commonly used terms.
6 |
7 | See the demos at http://sunnywalker.github.com/jQuery.FilterTable
8 |
9 | ## Usage
10 |
11 | Include the dependencies:
12 |
13 | ```html
14 |
15 |
16 |
17 |
22 | ```
23 |
24 | Then apply `filterTable()` to your table(s):
25 |
26 | ```html
27 |
30 | ```
31 |
32 | ## Problems?
33 |
34 | Be sure to check the FAQ file for common issues if the plugin does not seem to be working as intended.
35 |
36 | Note that the plugin does not work as expected when using cells with `rowspan`.
37 |
38 | ## Options
39 |
40 | | Option | Type | Default | Description |
41 | | ------ | ---- | ------- | ----------- |
42 | | `autofocus` | boolean | false | Makes the filter input field autofocused _(not recommended for accessibility reasons)_ |
43 | | `callback` | function(`term`, `table`) | _null_ | Callback function after a filter is performed. Parameters: term filter term (string)table table being filtered (jQuery object)This is a sample of the jQuery.FilterTable plugin which searchs for any of the terms instead of the literal term (a OR b). The term delimiters are comma (,) and space ( ).
17 |For example:
18 || # | 27 |President | 28 |Terms | 29 |Tenure | 30 |
|---|---|---|---|
| 1 | George Washington | two | 1789-1797 |
| 2 | John Adams | one | 1797-1801 |
| 3 | Thomas Jefferson | two | 1801-1809 |
| 4 | James Madison | two | 1809-1817 |
| 5 | James Monroe | two | 1817-1825 |
| 6 | John Quincy Adams | one | 1825-1829 |
| 7 | Andrew Jackson | two | 1829-1837 |
| 8 | Martin Van Buren | one | 1837-1841 |
| 9 | William Henry Harrison | one-partial | 1841 |
| 10 | John Tyler | one-partial | 1841-1845 |
| 11 | James Knox Polk | one | 1845-1849 |
| 12 | Zachary Taylor | one-partial | 1849-1850 |
| 13 | Millard Fillmore | one-partial | 1850-1853 |
| 14 | Franklin Pierce | one | 1853-1857 |
| 15 | James Buchanan | one | 1857-1861 |
| 16 | Abraham Lincoln | two-partial | 1861-1865 |
| 17 | Andrew Johnson | one-partial | 1865-1869 |
| 18 | Ulysses S. Grant | two | 1869-1877 |
| 19 | Rutherford Birchard Hayes | one | 1877-1881 |
| 20 | James Abram Garfield | one-partial | 1881 |
| 21 | Chester Alan Arthur | one-partial | 1881-1885 |
| 22 | Grover Cleveland | one | 1885-1889 |
| 23 | Benjamin Harrison | one | 1889-1893 |
| 24 | Grover Cleveland | one-again | 1893-1897 |
| 25 | William McKinley | two-partial | 1897-1901 |
| 26 | Theodore Roosevelt | two-partial | 1901-1909 |
| 27 | William Howard Taft | one | 1909-1913 |
| 28 | Woodrow Wilson | two | 1913-1921 |
| 29 | Warren Gamaliel Harding | two-partial | 1921-1923 |
| 30 | Calvin Coolidge | two-partial | 1923-1929 |
| 31 | Herbert Clark Hoover | one | 1929-1933 |
| 32 | Franklin Delano Roosevelt | four-partial | 1933-1945 |
| 33 | Harry S. Truman | two-partial | 1945-1953 |
| 34 | Dwight David Eisenhower | two | 1953-1961 |
| 35 | John Fitzgerald Kennedy | two-partial | 1961-1963 |
| 36 | Lyndon Baines Johnson | two-partial | 1963-1969 |
| 37 | Richard Milhous Nixon | two-partial | 1969-1974 |
| 38 | Gerald Rudolph Ford | two-partial | 1974-1977 |
| 39 | James Earl Carter, Jr. | one | 1977-1981 |
| 40 | Ronald Wilson Reagan | two | 1981-1989 |
| 41 | George Herbert Walker Bush | one | 1989-1993 |
| 42 | William Jefferson Clinton | two | 1993-2001 |
| 43 | George Walker Bush | two | 2001-2009 |
| 44 | Barack Hussein Obama | one | 2009- |
Data as of October, 2012.
80 |$('table').filterTable({
82 | filterExpression: 'filterTableFindAny'
83 | });
84 |
85 |
86 |
92 |
93 |
94 |
--------------------------------------------------------------------------------
/examples/filtertable-existing-input.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | This is a sample of the jQuery.FilterTable plugin which uses an existing element to filter the table instead of creating its own.
21 || # | 26 |President | 27 |Terms | 28 |Tenure | 29 |
|---|---|---|---|
| 1 | George Washington | two | 1789-1797 |
| 2 | John Adams | one | 1797-1801 |
| 3 | Thomas Jefferson | two | 1801-1809 |
| 4 | James Madison | two | 1809-1817 |
| 5 | James Monroe | two | 1817-1825 |
| 6 | John Quincy Adams | one | 1825-1829 |
| 7 | Andrew Jackson | two | 1829-1837 |
| 8 | Martin Van Buren | one | 1837-1841 |
| 9 | William Henry Harrison | one-partial | 1841 |
| 10 | John Tyler | one-partial | 1841-1845 |
| 11 | James Knox Polk | one | 1845-1849 |
| 12 | Zachary Taylor | one-partial | 1849-1850 |
| 13 | Millard Fillmore | one-partial | 1850-1853 |
| 14 | Franklin Pierce | one | 1853-1857 |
| 15 | James Buchanan | one | 1857-1861 |
| 16 | Abraham Lincoln | two-partial | 1861-1865 |
| 17 | Andrew Johnson | one-partial | 1865-1869 |
| 18 | Ulysses S. Grant | two | 1869-1877 |
| 19 | Rutherford Birchard Hayes | one | 1877-1881 |
| 20 | James Abram Garfield | one-partial | 1881 |
| 21 | Chester Alan Arthur | one-partial | 1881-1885 |
| 22 | Grover Cleveland | one | 1885-1889 |
| 23 | Benjamin Harrison | one | 1889-1893 |
| 24 | Grover Cleveland | one-again | 1893-1897 |
| 25 | William McKinley | two-partial | 1897-1901 |
| 26 | Theodore Roosevelt | two-partial | 1901-1909 |
| 27 | William Howard Taft | one | 1909-1913 |
| 28 | Woodrow Wilson | two | 1913-1921 |
| 29 | Warren Gamaliel Harding | two-partial | 1921-1923 |
| 30 | Calvin Coolidge | two-partial | 1923-1929 |
| 31 | Herbert Clark Hoover | one | 1929-1933 |
| 32 | Franklin Delano Roosevelt | four-partial | 1933-1945 |
| 33 | Harry S. Truman | two-partial | 1945-1953 |
| 34 | Dwight David Eisenhower | two | 1953-1961 |
| 35 | John Fitzgerald Kennedy | two-partial | 1961-1963 |
| 36 | Lyndon Baines Johnson | two-partial | 1963-1969 |
| 37 | Richard Milhous Nixon | two-partial | 1969-1974 |
| 38 | Gerald Rudolph Ford | two-partial | 1974-1977 |
| 39 | James Earl Carter, Jr. | one | 1977-1981 |
| 40 | Ronald Wilson Reagan | two | 1981-1989 |
| 41 | George Herbert Walker Bush | one | 1989-1993 |
| 42 | William Jefferson Clinton | two | 1993-2001 |
| 43 | George Walker Bush | two | 2001-2009 |
| 44 | Barack Hussein Obama | one | 2009- |
Data as of October, 2012.
79 |$('table').filterTable({
81 | inputSelector: '#input-filter'
82 | });
83 |
84 |
85 |
92 |
93 |
--------------------------------------------------------------------------------
/examples/filtertable-all-terms.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | This is a sample of the jQuery.FilterTable plugin which searchs for all of the terms instead of the literal term (a AND b). The term delimiters are comma (,) and space ( ).
17 |It's important to note that the term matching is per-cell, not per-row. That is, to match, each cell must have all of the terms.
18 |For example:
19 || # | 30 |President | 31 |Terms | 32 |Tenure | 33 |
|---|---|---|---|
| 1 | George Washington | two | 1789-1797 |
| 2 | John Adams | one | 1797-1801 |
| 3 | Thomas Jefferson | two | 1801-1809 |
| 4 | James Madison | two | 1809-1817 |
| 5 | James Monroe | two | 1817-1825 |
| 6 | John Quincy Adams | one | 1825-1829 |
| 7 | Andrew Jackson | two | 1829-1837 |
| 8 | Martin Van Buren | one | 1837-1841 |
| 9 | William Henry Harrison | one-partial | 1841 |
| 10 | John Tyler | one-partial | 1841-1845 |
| 11 | James Knox Polk | one | 1845-1849 |
| 12 | Zachary Taylor | one-partial | 1849-1850 |
| 13 | Millard Fillmore | one-partial | 1850-1853 |
| 14 | Franklin Pierce | one | 1853-1857 |
| 15 | James Buchanan | one | 1857-1861 |
| 16 | Abraham Lincoln | two-partial | 1861-1865 |
| 17 | Andrew Johnson | one-partial | 1865-1869 |
| 18 | Ulysses S. Grant | two | 1869-1877 |
| 19 | Rutherford Birchard Hayes | one | 1877-1881 |
| 20 | James Abram Garfield | one-partial | 1881 |
| 21 | Chester Alan Arthur | one-partial | 1881-1885 |
| 22 | Grover Cleveland | one | 1885-1889 |
| 23 | Benjamin Harrison | one | 1889-1893 |
| 24 | Grover Cleveland | one-again | 1893-1897 |
| 25 | William McKinley | two-partial | 1897-1901 |
| 26 | Theodore Roosevelt | two-partial | 1901-1909 |
| 27 | William Howard Taft | one | 1909-1913 |
| 28 | Woodrow Wilson | two | 1913-1921 |
| 29 | Warren Gamaliel Harding | two-partial | 1921-1923 |
| 30 | Calvin Coolidge | two-partial | 1923-1929 |
| 31 | Herbert Clark Hoover | one | 1929-1933 |
| 32 | Franklin Delano Roosevelt | four-partial | 1933-1945 |
| 33 | Harry S. Truman | two-partial | 1945-1953 |
| 34 | Dwight David Eisenhower | two | 1953-1961 |
| 35 | John Fitzgerald Kennedy | two-partial | 1961-1963 |
| 36 | Lyndon Baines Johnson | two-partial | 1963-1969 |
| 37 | Richard Milhous Nixon | two-partial | 1969-1974 |
| 38 | Gerald Rudolph Ford | two-partial | 1974-1977 |
| 39 | James Earl Carter, Jr. | one | 1977-1981 |
| 40 | Ronald Wilson Reagan | two | 1981-1989 |
| 41 | George Herbert Walker Bush | one | 1989-1993 |
| 42 | William Jefferson Clinton | two | 1993-2001 |
| 43 | George Walker Bush | two | 2001-2009 |
| 44 | Barack Hussein Obama | one | 2009- |
Data as of October, 2012.
83 |$('table').filterTable({
85 | filterExpression: 'filterTableFindAll'
86 | });
87 |
88 |
89 |
95 |
96 |
97 |
--------------------------------------------------------------------------------
/examples/filtertable-min-chars.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | This is a sample of the jQuery.FilterTable plugin illustrating the minChars option. This sample sets the option to 3, meaning the term must be at least 3 characters to trigger the filtering.
Special note for usability: Your visitors may not know that there is a minumum number of characters needed for the filtering, so you will likely want to also indicate so with the label or placeholder options.
For example:
19 || # | 30 |President | 31 |Terms | 32 |Tenure | 33 |
|---|---|---|---|
| 1 | George Washington | two | 1789-1797 |
| 2 | John Adams | one | 1797-1801 |
| 3 | Thomas Jefferson | two | 1801-1809 |
| 4 | James Madison | two | 1809-1817 |
| 5 | James Monroe | two | 1817-1825 |
| 6 | John Quincy Adams | one | 1825-1829 |
| 7 | Andrew Jackson | two | 1829-1837 |
| 8 | Martin Van Buren | one | 1837-1841 |
| 9 | William Henry Harrison | one-partial | 1841 |
| 10 | John Tyler | one-partial | 1841-1845 |
| 11 | James Knox Polk | one | 1845-1849 |
| 12 | Zachary Taylor | one-partial | 1849-1850 |
| 13 | Millard Fillmore | one-partial | 1850-1853 |
| 14 | Franklin Pierce | one | 1853-1857 |
| 15 | James Buchanan | one | 1857-1861 |
| 16 | Abraham Lincoln | two-partial | 1861-1865 |
| 17 | Andrew Johnson | one-partial | 1865-1869 |
| 18 | Ulysses S. Grant | two | 1869-1877 |
| 19 | Rutherford Birchard Hayes | one | 1877-1881 |
| 20 | James Abram Garfield | one-partial | 1881 |
| 21 | Chester Alan Arthur | one-partial | 1881-1885 |
| 22 | Grover Cleveland | one | 1885-1889 |
| 23 | Benjamin Harrison | one | 1889-1893 |
| 24 | Grover Cleveland | one-again | 1893-1897 |
| 25 | William McKinley | two-partial | 1897-1901 |
| 26 | Theodore Roosevelt | two-partial | 1901-1909 |
| 27 | William Howard Taft | one | 1909-1913 |
| 28 | Woodrow Wilson | two | 1913-1921 |
| 29 | Warren Gamaliel Harding | two-partial | 1921-1923 |
| 30 | Calvin Coolidge | two-partial | 1923-1929 |
| 31 | Herbert Clark Hoover | one | 1929-1933 |
| 32 | Franklin Delano Roosevelt | four-partial | 1933-1945 |
| 33 | Harry S. Truman | two-partial | 1945-1953 |
| 34 | Dwight David Eisenhower | two | 1953-1961 |
| 35 | John Fitzgerald Kennedy | two-partial | 1961-1963 |
| 36 | Lyndon Baines Johnson | two-partial | 1963-1969 |
| 37 | Richard Milhous Nixon | two-partial | 1969-1974 |
| 38 | Gerald Rudolph Ford | two-partial | 1974-1977 |
| 39 | James Earl Carter, Jr. | one | 1977-1981 |
| 40 | Ronald Wilson Reagan | two | 1981-1989 |
| 41 | George Herbert Walker Bush | one | 1989-1993 |
| 42 | William Jefferson Clinton | two | 1993-2001 |
| 43 | George Walker Bush | two | 2001-2009 |
| 44 | Barack Hussein Obama | one | 2009- |
Data as of October, 2012.
83 |$('table').filterTable({
85 | minChars: 3,
86 | label: 'Filter (3+ characters):'
87 | });
88 |
89 |
90 |
95 |
96 |
97 |
--------------------------------------------------------------------------------
/examples/filtertable-striping.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | This is a plain row striping sample of the jQuery.FilterTable plugin. It shows the code to stripe alternate rows.
22 || # | 27 |President | 28 |Terms | 29 |Tenure | 30 |
|---|---|---|---|
| 1 | George Washington | two | 1789-1797 |
| 2 | John Adams | one | 1797-1801 |
| 3 | Thomas Jefferson | two | 1801-1809 |
| 4 | James Madison | two | 1809-1817 |
| 5 | James Monroe | two | 1817-1825 |
| 6 | John Quincy Adams | one | 1825-1829 |
| 7 | Andrew Jackson | two | 1829-1837 |
| 8 | Martin Van Buren | one | 1837-1841 |
| 9 | William Henry Harrison | one-partial | 1841 |
| 10 | John Tyler | one-partial | 1841-1845 |
| 11 | James Knox Polk | one | 1845-1849 |
| 12 | Zachary Taylor | one-partial | 1849-1850 |
| 13 | Millard Fillmore | one-partial | 1850-1853 |
| 14 | Franklin Pierce | one | 1853-1857 |
| 15 | James Buchanan | one | 1857-1861 |
| 16 | Abraham Lincoln | two-partial | 1861-1865 |
| 17 | Andrew Johnson | one-partial | 1865-1869 |
| 18 | Ulysses S. Grant | two | 1869-1877 |
| 19 | Rutherford Birchard Hayes | one | 1877-1881 |
| 20 | James Abram Garfield | one-partial | 1881 |
| 21 | Chester Alan Arthur | one-partial | 1881-1885 |
| 22 | Grover Cleveland | one | 1885-1889 |
| 23 | Benjamin Harrison | one | 1889-1893 |
| 24 | Grover Cleveland | one-again | 1893-1897 |
| 25 | William McKinley | two-partial | 1897-1901 |
| 26 | Theodore Roosevelt | two-partial | 1901-1909 |
| 27 | William Howard Taft | one | 1909-1913 |
| 28 | Woodrow Wilson | two | 1913-1921 |
| 29 | Warren Gamaliel Harding | two-partial | 1921-1923 |
| 30 | Calvin Coolidge | two-partial | 1923-1929 |
| 31 | Herbert Clark Hoover | one | 1929-1933 |
| 32 | Franklin Delano Roosevelt | four-partial | 1933-1945 |
| 33 | Harry S. Truman | two-partial | 1945-1953 |
| 34 | Dwight David Eisenhower | two | 1953-1961 |
| 35 | John Fitzgerald Kennedy | two-partial | 1961-1963 |
| 36 | Lyndon Baines Johnson | two-partial | 1963-1969 |
| 37 | Richard Milhous Nixon | two-partial | 1969-1974 |
| 38 | Gerald Rudolph Ford | two-partial | 1974-1977 |
| 39 | James Earl Carter, Jr. | one | 1977-1981 |
| 40 | Ronald Wilson Reagan | two | 1981-1989 |
| 41 | George Herbert Walker Bush | one | 1989-1993 |
| 42 | William Jefferson Clinton | two | 1993-2001 |
| 43 | George Walker Bush | two | 2001-2009 |
| 44 | Barack Hussein Obama | one | 2009- |
Data as of October, 2012.
80 |var stripeTable = function(table) { //stripe the table (jQuery selector)
82 | table.find('tr').removeClass('striped').filter(':visible:even').addClass('striped');
83 | };
84 | $('table').filterTable({
85 | callback: function(term, table) { stripeTable(table); } //call the striping after every change to the filter term
86 | });
87 | stripeTable($('table')); //stripe the table for the first time
88 |
89 |
90 |
101 |
102 |
--------------------------------------------------------------------------------
/examples/filtertable-ignore-columns.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | This is a sample of the jQuery.FilterTable plugin using the ignoreColumns option.
In this sample, columns the first and third columns (0 and 2) are ignored—not filtered. Also in this sample, the ignored columns are styled via CSS to help identify that they are ignored.
21 |For example:
22 || # | 32 |President | 33 |Terms | 34 |Tenure | 35 |
|---|---|---|---|
| 1 | George Washington | two | 1789-1797 |
| 2 | John Adams | one | 1797-1801 |
| 3 | Thomas Jefferson | two | 1801-1809 |
| 4 | James Madison | two | 1809-1817 |
| 5 | James Monroe | two | 1817-1825 |
| 6 | John Quincy Adams | one | 1825-1829 |
| 7 | Andrew Jackson | two | 1829-1837 |
| 8 | Martin Van Buren | one | 1837-1841 |
| 9 | William Henry Harrison | one-partial | 1841 |
| 10 | John Tyler | one-partial | 1841-1845 |
| 11 | James Knox Polk | one | 1845-1849 |
| 12 | Zachary Taylor | one-partial | 1849-1850 |
| 13 | Millard Fillmore | one-partial | 1850-1853 |
| 14 | Franklin Pierce | one | 1853-1857 |
| 15 | James Buchanan | one | 1857-1861 |
| 16 | Abraham Lincoln | two-partial | 1861-1865 |
| 17 | Andrew Johnson | one-partial | 1865-1869 |
| 18 | Ulysses S. Grant | two | 1869-1877 |
| 19 | Rutherford Birchard Hayes | one | 1877-1881 |
| 20 | James Abram Garfield | one-partial | 1881 |
| 21 | Chester Alan Arthur | one-partial | 1881-1885 |
| 22 | Grover Cleveland | one | 1885-1889 |
| 23 | Benjamin Harrison | one | 1889-1893 |
| 24 | Grover Cleveland | one-again | 1893-1897 |
| 25 | William McKinley | two-partial | 1897-1901 |
| 26 | Theodore Roosevelt | two-partial | 1901-1909 |
| 27 | William Howard Taft | one | 1909-1913 |
| 28 | Woodrow Wilson | two | 1913-1921 |
| 29 | Warren Gamaliel Harding | two-partial | 1921-1923 |
| 30 | Calvin Coolidge | two-partial | 1923-1929 |
| 31 | Herbert Clark Hoover | one | 1929-1933 |
| 32 | Franklin Delano Roosevelt | four-partial | 1933-1945 |
| 33 | Harry S. Truman | two-partial | 1945-1953 |
| 34 | Dwight David Eisenhower | two | 1953-1961 |
| 35 | John Fitzgerald Kennedy | two-partial | 1961-1963 |
| 36 | Lyndon Baines Johnson | two-partial | 1963-1969 |
| 37 | Richard Milhous Nixon | two-partial | 1969-1974 |
| 38 | Gerald Rudolph Ford | two-partial | 1974-1977 |
| 39 | James Earl Carter, Jr. | one | 1977-1981 |
| 40 | Ronald Wilson Reagan | two | 1981-1989 |
| 41 | George Herbert Walker Bush | one | 1989-1993 |
| 42 | William Jefferson Clinton | two | 1993-2001 |
| 43 | George Walker Bush | two | 2001-2009 |
| 44 | Barack Hussein Obama | one | 2009- |
Data as of October, 2012.
85 |$('table').filterTable({
87 | ignoreColumns: [0, 2]
88 | });
89 |
90 |
91 |
97 |
98 |
99 |
--------------------------------------------------------------------------------
/jquery.filtertable.js:
--------------------------------------------------------------------------------
1 | /**
2 | * jquery.filterTable
3 | *
4 | * This plugin will add a search filter to tables. When typing in the filter,
5 | * any rows that do not contain the filter will be hidden.
6 | *
7 | * Utilizes bindWithDelay() if available. https://github.com/bgrins/bindWithDelay
8 | *
9 | * @version v1.5.7
10 | * @author Sunny Walker, swalker@hawaii.edu
11 | * @license MIT
12 | */
13 | (function ($) {
14 | var jversion = $.fn.jquery.split('.'),
15 | jmajor = parseFloat(jversion[0]),
16 | jminor = parseFloat(jversion[1]);
17 | // build the pseudo selector for jQuery < 1.8
18 | if (jmajor < 2 && jminor < 8) {
19 | // build the case insensitive filtering functionality as a pseudo-selector expression
20 | $.expr[':'].filterTableFind = function (a, i, m) {
21 | return $(a).text().toUpperCase().indexOf(m[3].toUpperCase().replace(/"""/g, '"').replace(/"\\"/g, "\\")) >= 0;
22 | };
23 | // build the case insensitive all-words filtering functionality as a pseudo-selector expression
24 | $.expr[':'].filterTableFindAny = function (a, i, m) {
25 | // build an array of each non-falsey value passed
26 | var raw_args = m[3].split(/[\s,]/),
27 | args = [];
28 | $.each(raw_args, function (j, v) {
29 | var t = v.replace(/^\s+|\s$/g, '');
30 | if (t) {
31 | args.push(t);
32 | }
33 | });
34 | // if there aren't any non-falsey values to search for, abort
35 | if (!args.length) {
36 | return false;
37 | }
38 | return function (a) {
39 | var found = false;
40 | $.each(args, function (j, v) {
41 | if ($(a).text().toUpperCase().indexOf(v.toUpperCase().replace(/"""/g, '"').replace(/"\\"/g, "\\")) >= 0) {
42 | found = true;
43 | return false;
44 | }
45 | });
46 | return found;
47 | };
48 | };
49 | // build the case insensitive all-words filtering functionality as a pseudo-selector expression
50 | $.expr[':'].filterTableFindAll = function (a, i, m) {
51 | // build an array of each non-falsey value passed
52 | var raw_args = m[3].split(/[\s,]/),
53 | args = [];
54 | $.each(raw_args, function (j, v) {
55 | var t = v.replace(/^\s+|\s$/g, '');
56 | if (t) {
57 | args.push(t);
58 | }
59 | });
60 | // if there aren't any non-falsey values to search for, abort
61 | if (!args.length) {
62 | return false;
63 | }
64 | return function (a) {
65 | // how many terms were found?
66 | var found = 0;
67 | $.each(args, function (j, v) {
68 | if ($(a).text().toUpperCase().indexOf(v.toUpperCase().replace(/"""/g, '"').replace(/"\\"/g, "\\")) >= 0) {
69 | // found another term
70 | found++;
71 | }
72 | });
73 | return found === args.length; // did we find all of them in this cell?
74 | };
75 | };
76 | } else {
77 | // build the pseudo selector for jQuery >= 1.8
78 | $.expr[':'].filterTableFind = jQuery.expr.createPseudo(function (arg) {
79 | return function (el) {
80 | return $(el).text().toUpperCase().indexOf(arg.toUpperCase().replace(/"""/g, '"').replace(/"\\"/g, "\\")) >= 0;
81 | };
82 | });
83 | $.expr[':'].filterTableFindAny = jQuery.expr.createPseudo(function (arg) {
84 | // build an array of each non-falsey value passed
85 | var raw_args = arg.split(/[\s,]/),
86 | args = [];
87 | $.each(raw_args, function (i, v) {
88 | // trim the string
89 | var t = v.replace(/^\s+|\s$/g, '');
90 | if (t) {
91 | args.push(t);
92 | }
93 | });
94 | // if there aren't any non-falsey values to search for, abort
95 | if (!args.length) {
96 | return false;
97 | }
98 | return function (el) {
99 | var found = false;
100 | $.each(args, function (i, v) {
101 | if ($(el).text().toUpperCase().indexOf(v.toUpperCase().replace(/"""/g, '"').replace(/"\\"/g, "\\")) >= 0) {
102 | found = true;
103 | // short-circuit the searching since this cell has one of the terms
104 | return false;
105 | }
106 | });
107 | return found;
108 | };
109 | });
110 | $.expr[':'].filterTableFindAll = jQuery.expr.createPseudo(function (arg) {
111 | // build an array of each non-falsey value passed
112 | var raw_args = arg.split(/[\s,]/),
113 | args = [];
114 | $.each(raw_args, function (i, v) {
115 | // trim the string
116 | var t = v.replace(/^\s+|\s$/g, '');
117 | if (t) {
118 | args.push(t);
119 | }
120 | });
121 | // if there aren't any non-falsey values to search for, abort
122 | if (!args.length) {
123 | return false;
124 | }
125 | return function (el) {
126 | // how many terms were found?
127 | var found = 0;
128 | $.each(args, function (i, v) {
129 | if ($(el).text().toUpperCase().indexOf(v.toUpperCase().replace(/"""/g, '"').replace(/"\\"/g, "\\")) >= 0) {
130 | // found another term
131 | found++;
132 | }
133 | });
134 | // did we find all of them in this cell?
135 | return found === args.length;
136 | };
137 | });
138 | }
139 | // define the filterTable plugin
140 | $.fn.filterTable = function (options) {
141 | // start off with some default settings
142 | var defaults = {
143 | // make the filter input field autofocused (not recommended for accessibility)
144 | autofocus: false,
145 |
146 | // callback function: function (term, table){}
147 | callback: null,
148 |
149 | // class to apply to the container
150 | containerClass: 'filter-table',
151 |
152 | // tag name of the container
153 | containerTag: 'p',
154 |
155 | // jQuery expression method to use for filtering
156 | filterExpression: 'filterTableFind',
157 |
158 | // if true, the table's tfoot(s) will be hidden when the table is filtered
159 | hideTFootOnFilter: false,
160 |
161 | // class applied to cells containing the filter term
162 | highlightClass: 'alt',
163 |
164 | // don't filter the contents of cells with this class
165 | ignoreClass: '',
166 |
167 | // don't filter the contents of these columns
168 | ignoreColumns: [],
169 |
170 | // use the element with this selector for the filter input field instead of creating one
171 | inputSelector: null,
172 |
173 | // name of filter input field
174 | inputName: '',
175 |
176 | // tag name of the filter input tag
177 | inputType: 'search',
178 |
179 | // text to precede the filter input tag
180 | label: 'Filter:',
181 |
182 | // filter only when at least this number of characters are in the filter input field
183 | minChars: 1,
184 |
185 | // don't show the filter on tables with at least this number of rows
186 | minRows: 8,
187 |
188 | // HTML5 placeholder text for the filter field
189 | placeholder: 'search this table',
190 |
191 | // prevent the return key in the filter input field from trigger form submits
192 | preventReturnKey: true,
193 |
194 | // list of phrases to quick fill the search
195 | quickList: [],
196 |
197 | // class of each quick list item
198 | quickListClass: 'quick',
199 |
200 | // quick list item label to clear the filter (e.g., '× Clear filter')
201 | quickListClear: '',
202 |
203 | // tag surrounding quick list items (e.g., ul)
204 | quickListGroupTag: '',
205 |
206 | // tag type of each quick list item (e.g., a or li)
207 | quickListTag: 'a',
208 |
209 | // class applied to visible rows
210 | visibleClass: 'visible'
211 | },
212 | // mimic PHP's htmlspecialchars() function
213 | hsc = function (text) {
214 | return text.replace(/&/g, '&').replace(/"/g, '"').replace(//g, '>');
215 | },
216 | // merge the user's settings into the defaults
217 | settings = $.extend({}, defaults, options);
218 |
219 | // handle the actual table filtering
220 | var doFiltering = function (table, q) {
221 | // cache the tbody element
222 | var tbody = table.find('tbody');
223 | // if the filtering query is blank or the number of chars is less than the minChars option
224 | if (q === '' || q.length < settings.minChars) {
225 | // show all rows
226 | tbody.find('tr').show().addClass(settings.visibleClass);
227 | // remove the row highlight from all cells
228 | tbody.find('td').removeClass(settings.highlightClass);
229 | // show footer if the setting was specified
230 | if (settings.hideTFootOnFilter) {
231 | table.find('tfoot').show();
232 | }
233 | } else {
234 | // if the filter query is not blank
235 | var all_tds = tbody.find('td');
236 | // hide all rows, assuming none were found
237 | tbody.find('tr').hide().removeClass(settings.visibleClass);
238 | // remove previous highlights
239 | all_tds.removeClass(settings.highlightClass);
240 | // hide footer if the setting was specified
241 | if (settings.hideTFootOnFilter) {
242 | table.find('tfoot').hide();
243 | }
244 | if (settings.ignoreColumns.length) {
245 | var tds = [];
246 | if (settings.ignoreClass) {
247 | all_tds = all_tds.not('.' + settings.ignoreClass);
248 | }
249 | tds = all_tds.filter(':' + settings.filterExpression + '("' + q + '")');
250 | tds.each(function () {
251 | var t = $(this),
252 | col = t.parent().children().index(t);
253 | if ($.inArray(col, settings.ignoreColumns) === -1) {
254 | t.addClass(settings.highlightClass).closest('tr').show().addClass(settings.visibleClass);
255 | }
256 | });
257 | } else {
258 | if (settings.ignoreClass) {
259 | all_tds = all_tds.not('.' + settings.ignoreClass);
260 | }
261 | // highlight (class=alt) only the cells that match the query and show their rows
262 | all_tds.filter(':' + settings.filterExpression + '("' + q + '")').addClass(settings.highlightClass).closest('tr').show().addClass(settings.visibleClass);
263 | }
264 | }
265 | // call the callback function
266 | if (settings.callback) {
267 | settings.callback(q, table);
268 | }
269 | }; // doFiltering()
270 |
271 | return this.each(function () {
272 | // cache the table
273 | var t = $(this),
274 | // cache the tbody
275 | tbody = t.find('tbody'),
276 | // placeholder for the filter field container DOM node
277 | container = null,
278 | // placeholder for the quick list items
279 | quicks = null,
280 | // placeholder for the field field DOM node
281 | filter = null,
282 | // was the filter created or chosen from an existing element?
283 | created_filter = true;
284 |
285 | // only if object is a table and there's a tbody and at least minRows trs and hasn't already had a filter added
286 | if (t[0].nodeName === 'TABLE' && tbody.length > 0 && (settings.minRows === 0 || (settings.minRows > 0 && tbody.find('tr').length >= settings.minRows)) && !t.prev().hasClass(settings.containerClass)) {
287 | // use a single existing field as the filter input field
288 | if (settings.inputSelector && $(settings.inputSelector).length === 1) {
289 | filter = $(settings.inputSelector);
290 | // container to hold the quick list options
291 | container = filter.parent();
292 | created_filter = false;
293 | } else {
294 | // create the filter input field (and container)
295 | // build the container tag for the filter field
296 | container = $('<' + settings.containerTag + ' />');
297 | // add any classes that need to be added
298 | if (settings.containerClass !== '') {
299 | container.addClass(settings.containerClass);
300 | }
301 | // add the label for the filter field
302 | container.prepend(settings.label + ' ');
303 | // build the filter field
304 | filter = $('');
305 | // prevent return in the filter field from submitting any forms
306 | if (settings.preventReturnKey) {
307 | filter.on('keydown', function (ev) {
308 | if ((ev.keyCode || ev.which) === 13) {
309 | ev.preventDefault();
310 | return false;
311 | }
312 | });
313 | }
314 | }
315 |
316 | // add the autofocus attribute if requested
317 | if (settings.autofocus) {
318 | filter.attr('autofocus', true);
319 | }
320 |
321 | // does bindWithDelay() exist?
322 | if ($.fn.bindWithDelay) {
323 | // bind doFiltering() to keyup (delayed)
324 | filter.bindWithDelay('keyup', function () {
325 | doFiltering(t, $(this).val());
326 | }, 200);
327 | } else {
328 | // just bind to onKeyUp
329 | // bind doFiltering() to keyup
330 | filter.bind('keyup', function () {
331 | doFiltering(t, $(this).val());
332 | });
333 | }
334 |
335 | // bind doFiltering() to additional events
336 | filter.bind('click search input paste blur', function () {
337 | doFiltering(t, $(this).val());
338 | });
339 |
340 | // add the filter field to the container if it was created by the plugin
341 | if (created_filter) {
342 | container.append(filter);
343 | }
344 |
345 | // are there any quick list items to add?
346 | if (settings.quickList.length > 0 || settings.quickListClear) {
347 | quicks = settings.quickListGroupTag ? $('<' + settings.quickListGroupTag + ' />') : container;
348 | // for each quick list item...
349 | $.each(settings.quickList, function (index, value) {
350 | // build the quick list item link
351 | var q = $('<' + settings.quickListTag + ' class="' + settings.quickListClass + '" />');
352 | // add the item's text
353 | q.text(hsc(value));
354 | if (q[0].nodeName === 'A') {
355 | // add a (worthless) href to the item if it's an anchor tag so that it gets the browser's link treatment
356 | q.attr('href', '#');
357 | }
358 | // bind the click event to it
359 | q.bind('click', function (e) {
360 | // stop the normal anchor tag behavior from happening
361 | e.preventDefault();
362 | // send the quick list value over to the filter field and trigger the event
363 | filter.val(value).focus().trigger('click');
364 | });
365 | // add the quick list link to the quick list groups container
366 | quicks.append(q);
367 | });
368 |
369 | // add the quick list clear item if a label has been specified
370 | if (settings.quickListClear) {
371 | // build the clear item
372 | var q = $('<' + settings.quickListTag + ' class="' + settings.quickListClass + '" />');
373 | // add the label text
374 | q.html(settings.quickListClear);
375 | if (q[0].nodeName === 'A') {
376 | // add a (worthless) href to the item if it's an anchor tag so that it gets the browser's link treatment
377 | q.attr('href', '#');
378 | }
379 | // bind the click event to it
380 | q.bind('click', function (e) {
381 | e.preventDefault();
382 | // clear the quick list value and trigger the event
383 | filter.val('').focus().trigger('click');
384 | });
385 | // add the clear item to the quick list groups container
386 | quicks.append(q);
387 | }
388 |
389 | // add the quick list groups container to the DOM if it isn't already there
390 | if (quicks !== container) {
391 | container.append(quicks);
392 | }
393 | }
394 |
395 | // add the filter field and quick list container to just before the table if it was created by the plugin
396 | if (created_filter) {
397 | t.before(container);
398 | }
399 | }
400 | }); // return this.each
401 | }; // $.fn.filterTable
402 | })(jQuery);
403 |
--------------------------------------------------------------------------------