Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion examples/filtertable-all-terms.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,4 @@ <h2>Code</h2>
</script>
<script src="samples-common.js"></script><!-- this is only needed for the sample page and not for your projects -->
</body>
</html>
</html>
2 changes: 1 addition & 1 deletion examples/filtertable-any-term.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,4 @@ <h2>Code</h2>
</script>
<script src="samples-common.js"></script><!-- this is only needed for the sample page and not for your projects -->
</body>
</html>
</html>
88 changes: 88 additions & 0 deletions examples/filtertable-datatag.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery.FilterTable Sample</title>
<link rel="stylesheet" href="samples-styles.css">
<style>
/* filter table specific styling */
td.alt { background-color: #ffc; background-color: rgba(255, 255, 0, 0.2); }
.filter-table .quick { margin-left: 1em; font-size: 0.8em; text-decoration: none; }
.fitler-table .quick:hover { text-decoration: underline; }
</style>
</head>
<body>
<h1>jQuery.FilterTable Sample</h1>
<p><a href="index.html">&larr; More samples</a></p>
<p>This is a sample of the <a href="http://sunnywalker.github.com/jQuery.FilterTable">jQuery.FilterTable plugin</a>. The filter searches both the text of the table and any additional keywords that are in the <code> data-filterTableKeywords </code> attribute.</p>
<h2>Presidents of the United States of America and their party allegience</h2>
<br>Affiliation information taken from <a href="https://en.wikipedia.org/wiki/List_of_Presidents_of_the_United_States_by_political_affiliation">wikipedia</a>
<table>
<thead>
<tr>
<th scope="col" title="President Number">#</th>
<th scope="col">President</th>
<th scope="col">Terms</th>
<th scope="col">Tenure</th>
</tr>
</thead>
<tbody>
<tr><td>1</td><td data-filterTableKeywords="First Non-affiliated">George Washington</td><td>two</td><td>1789-1797</td></tr>
<tr><td>2</td><td data-filterTableKeywords="Second Federalist">John Adams</td><td>one</td><td>1797-1801</td></tr>
<tr><td>3</td><td data-filterTableKeywords="Third Democrat-Republican">Thomas Jefferson</td><td>two</td><td>1801-1809</td></tr>
<tr><td>4</td><td data-filterTableKeywords="Democrat-Republican">James Madison</td><td>two</td><td>1809-1817</td></tr>
<tr><td>5</td><td data-filterTableKeywords="Democrat-Republican">James Monroe</td><td>two</td><td>1817-1825</td></tr>
<tr><td>6</td><td data-filterTableKeywords="Democrat-Republican">John Quincy Adams</td><td>one</td><td>1825-1829</td></tr>
<tr><td>7</td><td data-filterTableKeywords="Democrat">Andrew Jackson</td><td>two</td><td>1829-1837</td></tr>
<tr><td>8</td><td data-filterTableKeywords="Democrat">Martin Van Buren</td><td>one</td><td>1837-1841</td></tr>
<tr><td>9</td><td data-filterTableKeywords="Whig">William Henry Harrison</td><td>one-partial</td><td>1841</td></tr>
<tr><td>10</td><td data-filterTableKeywords="Whig">John Tyler</td><td>one-partial</td><td>1841-1845</td></tr>
<tr><td>11</td><td data-filterTableKeywords="Democrat">James Knox Polk</td><td>one</td><td>1845-1849</td></tr>
<tr><td>12</td><td data-filterTableKeywords="Whig">Zachary Taylor</td><td>one-partial</td><td>1849-1850</td></tr>
<tr><td>13</td><td data-filterTableKeywords="Whig">Millard Fillmore</td><td>one-partial</td><td>1850-1853</td></tr>
<tr><td>14</td><td data-filterTableKeywords="Democrat">Franklin Pierce</td><td>one</td><td>1853-1857</td></tr>
<tr><td>15</td><td data-filterTableKeywords="Democrat">James Buchanan</td><td>one</td><td>1857-1861</td></tr>
<tr><td>16</td><td data-filterTableKeywords="Republican">Abraham Lincoln</td><td>two-partial</td><td>1861-1865</td></tr>
<tr><td>17</td><td data-filterTableKeywords="Democrat">Andrew Johnson</td><td>one-partial</td><td>1865-1869</td></tr>
<tr><td>18</td><td data-filterTableKeywords="Republican">Ulysses S. Grant</td><td>two</td><td>1869-1877</td></tr>
<tr><td>19</td><td data-filterTableKeywords="Republican">Rutherford Birchard Hayes</td><td>one</td><td>1877-1881</td></tr>
<tr><td>20</td><td data-filterTableKeywords="Republican">James Abram Garfield</td><td>one-partial</td><td>1881</td></tr>
<tr><td>21</td><td data-filterTableKeywords="Republican">Chester Alan Arthur</td><td>one-partial</td><td>1881-1885</td></tr>
<tr><td>22</td><td data-filterTableKeywords="Democrat">Grover Cleveland</td><td>one</td><td>1885-1889</td></tr>
<tr><td>23</td><td data-filterTableKeywords="Republican">Benjamin Harrison</td><td>one</td><td>1889-1893</td></tr>
<tr><td>24</td><td data-filterTableKeywords="Democrat">Grover Cleveland</td><td>one-again</td><td>1893-1897</td></tr>
<tr><td>25</td><td data-filterTableKeywords="Republican">William McKinley</td><td>two-partial</td><td>1897-1901</td></tr>
<tr><td>26</td><td data-filterTableKeywords="Republican">Theodore Roosevelt</td><td>two-partial</td><td>1901-1909</td></tr>
<tr><td>27</td><td data-filterTableKeywords="Republican">William Howard Taft</td><td>one</td><td>1909-1913</td></tr>
<tr><td>28</td><td data-filterTableKeywords="Democrat">Woodrow Wilson</td><td>two</td><td>1913-1921</td></tr>
<tr><td>29</td><td data-filterTableKeywords="Republican">Warren Gamaliel Harding</td><td>two-partial</td><td>1921-1923</td></tr>
<tr><td>30</td><td data-filterTableKeywords="Republican">Calvin Coolidge</td><td>two-partial</td><td>1923-1929</td></tr>
<tr><td>31</td><td data-filterTableKeywords="Republican">Herbert Clark Hoover</td><td>one</td><td>1929-1933</td></tr>
<tr><td>32</td><td data-filterTableKeywords="Democrat">Franklin Delano Roosevelt</td><td>four-partial</td><td>1933-1945</td></tr>
<tr><td>33</td><td data-filterTableKeywords="Democrat">Harry S. Truman</td><td>two-partial</td><td>1945-1953</td></tr>
<tr><td>34</td><td data-filterTableKeywords="Republican">Dwight David Eisenhower</td><td>two</td><td>1953-1961</td></tr>
<tr><td>35</td><td data-filterTableKeywords="Democrat">John Fitzgerald Kennedy</td><td>two-partial</td><td>1961-1963</td></tr>
<tr><td>36</td><td data-filterTableKeywords="Democrat">Lyndon Baines Johnson</td><td>two-partial</td><td>1963-1969</td></tr>
<tr><td>37</td><td data-filterTableKeywords="Republican">Richard Milhous Nixon</td><td>two-partial</td><td>1969-1974</td></tr>
<tr><td>38</td><td data-filterTableKeywords="Republican">Gerald Rudolph Ford</td><td>two-partial</td><td>1974-1977</td></tr>
<tr><td>39</td><td data-filterTableKeywords="Democrat">James Earl Carter, Jr.</td><td>one</td><td>1977-1981</td></tr>
<tr><td>40</td><td data-filterTableKeywords="Republican">Ronald Wilson Reagan</td><td>two</td><td>1981-1989</td></tr>
<tr><td>41</td><td data-filterTableKeywords="Republican">George Herbert Walker Bush</td><td>one</td><td>1989-1993</td></tr>
<tr><td>42</td><td data-filterTableKeywords="Democrat">William Jefferson Clinton</td><td>two</td><td>1993-2001</td></tr>
<tr><td>43</td><td data-filterTableKeywords="Republican">George Walker Bush</td><td>two</td><td>2001-2009</td></tr>
<tr><td>44</td><td data-filterTableKeywords="Democrat">Barack Hussein Obama</td><td>one</td><td>2009-</td></tr>
</tbody>
</table>
<p><em>Data as of October, 2012.</em></p>
<h2>Code</h2>
<pre>$('table').filterTable();</pre>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="../jquery.filtertable.js"></script>
<script>
$(document).ready(function() {
$('table').filterTable({ quickList: ['Democrat','Republican','Democrat-Republican','Federalist','Whig','Non-affiliated','First']}) // add some shortcut searches); // apply filterTable to all tables on this page
});
</script>
</body>
</html>
1 change: 1 addition & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ <h1>jQuery.FilterTable Samples</h1>
<li><a href="filtertable-sample.html">Basic Sample</a></li>
<li><a href="filtertable-striping.html">Alternate Row Striping</a></li>
<li><a href="filtertable-quick.html">Quick List Items</a></li>
<li><a href="filtertable-datatag.html">Search Data tag value</a></li>
<li><a href="filtertable-existing-input.html">Using an Existing Input for Filtering</a></li>
<li><a href="filtertable-any-term.html">Filter on Any Term</a></li>
<li><a href="filtertable-all-terms.html">Filter on All Terms</a></li>
Expand Down
58 changes: 32 additions & 26 deletions jquery.filtertable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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) {
Expand All @@ -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,]/),
Expand All @@ -122,20 +120,28 @@
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?
return found === args.length;
};
});
}

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
Expand Down