diff --git a/README.md b/README.md index 4c6a237..755e343 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ One can also define clickable shortcuts for commonly used terms. See the demos at http://sunnywalker.github.com/jQuery.FilterTable +The main difference between this repo and the main repo at https://github.com/sunnywalker/jQuery.FilterTable is the addition of the data-filterTableKeywords attribute which allows you to add arbitrary values to filter. This has been submitted as a PR https://github.com/sunnywalker/jQuery.FilterTable/pull/43 but has not be merged. + ## Usage Include the dependencies: diff --git a/examples/filtertable-all-terms.html b/examples/filtertable-all-terms.html index 62ef9fe..aafd407 100644 --- a/examples/filtertable-all-terms.html +++ b/examples/filtertable-all-terms.html @@ -94,4 +94,4 @@

Code

- \ No newline at end of file + diff --git a/examples/filtertable-any-term.html b/examples/filtertable-any-term.html index e24fcd7..04ef88f 100644 --- a/examples/filtertable-any-term.html +++ b/examples/filtertable-any-term.html @@ -91,4 +91,4 @@

Code

- \ No newline at end of file + diff --git a/examples/filtertable-datatag.html b/examples/filtertable-datatag.html new file mode 100644 index 0000000..1104779 --- /dev/null +++ b/examples/filtertable-datatag.html @@ -0,0 +1,88 @@ + + + + + + jQuery.FilterTable Sample + + + + +

jQuery.FilterTable Sample

+

← More samples

+

This is a sample of the jQuery.FilterTable plugin. The filter searches both the text of the table and any additional keywords that are in the data-filterTableKeywords attribute.

+

Presidents of the United States of America and their party allegience

+
Affiliation information taken from wikipedia + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
#PresidentTermsTenure
1George Washingtontwo1789-1797
2John Adamsone1797-1801
3Thomas Jeffersontwo1801-1809
4James Madisontwo1809-1817
5James Monroetwo1817-1825
6John Quincy Adamsone1825-1829
7Andrew Jacksontwo1829-1837
8Martin Van Burenone1837-1841
9William Henry Harrisonone-partial1841
10John Tylerone-partial1841-1845
11James Knox Polkone1845-1849
12Zachary Taylorone-partial1849-1850
13Millard Fillmoreone-partial1850-1853
14Franklin Pierceone1853-1857
15James Buchananone1857-1861
16Abraham Lincolntwo-partial1861-1865
17Andrew Johnsonone-partial1865-1869
18Ulysses S. Granttwo1869-1877
19Rutherford Birchard Hayesone1877-1881
20James Abram Garfieldone-partial1881
21Chester Alan Arthurone-partial1881-1885
22Grover Clevelandone1885-1889
23Benjamin Harrisonone1889-1893
24Grover Clevelandone-again1893-1897
25William McKinleytwo-partial1897-1901
26Theodore Roosevelttwo-partial1901-1909
27William Howard Taftone1909-1913
28Woodrow Wilsontwo1913-1921
29Warren Gamaliel Hardingtwo-partial1921-1923
30Calvin Coolidgetwo-partial1923-1929
31Herbert Clark Hooverone1929-1933
32Franklin Delano Rooseveltfour-partial1933-1945
33Harry S. Trumantwo-partial1945-1953
34Dwight David Eisenhowertwo1953-1961
35John Fitzgerald Kennedytwo-partial1961-1963
36Lyndon Baines Johnsontwo-partial1963-1969
37Richard Milhous Nixontwo-partial1969-1974
38Gerald Rudolph Fordtwo-partial1974-1977
39James Earl Carter, Jr.one1977-1981
40Ronald Wilson Reagantwo1981-1989
41George Herbert Walker Bushone1989-1993
42William Jefferson Clintontwo1993-2001
43George Walker Bushtwo2001-2009
44Barack Hussein Obamaone2009-
+

Data as of October, 2012.

+

Code

+
$('table').filterTable();
+ + + + + \ No newline at end of file diff --git a/examples/index.html b/examples/index.html index 9a22ead..f924411 100644 --- a/examples/index.html +++ b/examples/index.html @@ -13,6 +13,7 @@

jQuery.FilterTable Samples

  • Basic Sample
  • Alternate Row Striping
  • Quick List Items
  • +
  • Search Data tag value
  • Using an Existing Input for Filtering
  • Filter on Any Term
  • Filter on All Terms
  • diff --git a/jquery.filtertable.js b/jquery.filtertable.js index e65af7f..40e0ae7 100644 --- a/jquery.filtertable.js +++ b/jquery.filtertable.js @@ -37,8 +37,8 @@ } return function (a) { var found = false; - $.each(args, function (j, v) { - if ($(a).text().toUpperCase().indexOf(v.toUpperCase().replace(/"""/g, '"').replace(/"\\"/g, "\\")) >= 0) { + $.each(args, function(j, v) { + if (isMatch(a,v)){ found = true; return false; } @@ -61,23 +61,20 @@ if (!args.length) { return false; } - return function (a) { - // how many terms were found? - var found = 0; - $.each(args, function (j, v) { - if ($(a).text().toUpperCase().indexOf(v.toUpperCase().replace(/"""/g, '"').replace(/"\\"/g, "\\")) >= 0) { - // found another term - found++; + return function(a) { + var found = 0; // how many terms were found? + $.each(args, function(j, v) { + if (isMatch(a,v)) { + found++; // found another term } }); return found === args.length; // did we find all of them in this cell? }; }; - } else { - // build the pseudo selector for jQuery >= 1.8 - $.expr[':'].filterTableFind = jQuery.expr.createPseudo(function (arg) { - return function (el) { - return $(el).text().toUpperCase().indexOf(arg.toUpperCase().replace(/"""/g, '"').replace(/"\\"/g, "\\")) >= 0; + } else { // build the pseudo selector for jQuery >= 1.8 + $.expr[':'].filterTableFind = jQuery.expr.createPseudo(function(arg) { + return function(el) { + return isMatch(el,arg); }; }); $.expr[':'].filterTableFindAny = jQuery.expr.createPseudo(function (arg) { @@ -97,16 +94,17 @@ } return function (el) { var found = false; - $.each(args, function (i, v) { - if ($(el).text().toUpperCase().indexOf(v.toUpperCase().replace(/"""/g, '"').replace(/"\\"/g, "\\")) >= 0) { + $.each(args, function(i, v) { + if(isMatch(el, v)){ found = true; - // short-circuit the searching since this cell has one of the terms - return false; - } + return false; // short-circuit the searching since this cell has one of the terms + } }); return found; }; }); + + $.expr[':'].filterTableFindAll = jQuery.expr.createPseudo(function (arg) { // build an array of each non-falsey value passed var raw_args = arg.split(/[\s,]/), @@ -122,13 +120,11 @@ if (!args.length) { return false; } - return function (el) { - // how many terms were found? - var found = 0; - $.each(args, function (i, v) { - if ($(el).text().toUpperCase().indexOf(v.toUpperCase().replace(/"""/g, '"').replace(/"\\"/g, "\\")) >= 0) { - // found another term - found++; + return function(el) { + var found = 0; // how many terms were found? + $.each(args, function(i, v) { + if(isMatch(el,v)){ + found++; // found another term } }); // did we find all of them in this cell? @@ -136,6 +132,16 @@ }; }); } + + var isMatch = function(element, searchString){ + var result = $(element).text().toUpperCase().indexOf(searchString.toUpperCase()) >= 0; + if (result == false){ + if ($(element).data("filtertablekeywords") != undefined){ + result = $(element).data("filtertablekeywords").toUpperCase().indexOf(searchString.toUpperCase()) >= 0; + } + } + return result; + } // define the filterTable plugin $.fn.filterTable = function (options) { // start off with some default settings