From 9be2518ee53fcf17f7b46e46d4e7b04e3220ced5 Mon Sep 17 00:00:00 2001 From: "Kazemi, Darius" Date: Mon, 18 Aug 2014 10:37:59 -0400 Subject: [PATCH 1/4] Adding minimum character threshold Adds a new `minChars` setting where the filter is only applied to a table when at least `minChars` characters are entered. --- README.md | 1 + jquery.filtertable.js | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9832340..eca0aec 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ $('table').filterTable(); //if this code appears after your tables; otherwise, i | `inputName` | string | filter-table | Name attribute of the filter input field | | `inputType` | string | search | Tag name of the filter input itself | | `label` | string | Filter: | Text to precede the filter input | +| `minChars` | integer | 1 | Filter is only applied to a table when at least this many characters are entered | | `minRows` | integer | 8 | Only show the filter on tables with this number of rows or more | | `placeholder` | string | search this table | HTML5 placeholder text for the filter input | | `quickList` | array | [] | List of clickable phrases to quick fill the search | diff --git a/jquery.filtertable.js b/jquery.filtertable.js index 6422d6f..d64ab43 100644 --- a/jquery.filtertable.js +++ b/jquery.filtertable.js @@ -34,6 +34,7 @@ inputName: '', // name of filter input field inputType: 'search', // tag name of the filter input tag label: 'Filter:', // text to precede the filter input tag + minChars: 1, // don't start filtering until this many characters are entered minRows: 8, // don't show the filter on tables with less than this number of rows placeholder: 'search this table', // HTML5 placeholder text for the filter field quickList: [], // list of phrases to quick fill the search @@ -49,7 +50,7 @@ var doFiltering = function(table, q) { // handle the actual table filtering var tbody=table.find('tbody'); // cache the tbody element - if (q==='') { // if the filtering query is blank + if (q==='' || q.length < settings.minChars) { // if the filtering query is blank or the number of chars entered is less than minChars tbody.find('tr').show().addClass(settings.visibleClass); // show all rows tbody.find('td').removeClass(settings.highlightClass); // remove the row highlight from all cells if (settings.hideTFootOnFilter) { // show footer if the setting was specified From 462aa517c8b87b532c5ec383e6a36c7bf5cbedc9 Mon Sep 17 00:00:00 2001 From: "Kazemi, Darius" Date: Mon, 18 Aug 2014 10:46:32 -0400 Subject: [PATCH 2/4] Adding minified lib --- jquery.filtertable.min.js | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/jquery.filtertable.min.js b/jquery.filtertable.min.js index 8487edc..e279992 100644 --- a/jquery.filtertable.min.js +++ b/jquery.filtertable.min.js @@ -1,12 +1 @@ -/** - * jquery.filterTable - * - * 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. - * - * Utilizes bindWithDelay() if available. https://github.com/bgrins/bindWithDelay - * - * @version v1.5.3 - * @author Sunny Walker, swalker@hawaii.edu - */ -!function(e){var t=e.fn.jquery.split("."),i=parseFloat(t[0]),a=parseFloat(t[1]);e.expr[":"].filterTableFind=2>i&&8>a?function(t,i,a){return e(t).text().toUpperCase().indexOf(a[3].toUpperCase())>=0}:jQuery.expr.createPseudo(function(t){return function(i){return e(i).text().toUpperCase().indexOf(t.toUpperCase())>=0}}),e.fn.filterTable=function(t){var i={autofocus:!1,callback:null,containerClass:"filter-table",containerTag:"p",hideTFootOnFilter:!1,highlightClass:"alt",inputSelector:null,inputName:"",inputType:"search",label:"Filter:",minRows:8,placeholder:"search this table",quickList:[],quickListClass:"quick",quickListGroupTag:"",quickListTag:"a",visibleClass:"visible"},a=function(e){return e.replace(/&/g,"&").replace(/"/g,""").replace(//g,">")},l=e.extend({},i,t),n=function(e,t){var i=e.find("tbody");""===t?(i.find("tr").show().addClass(l.visibleClass),i.find("td").removeClass(l.highlightClass),l.hideTFootOnFilter&&e.find("tfoot").show()):(i.find("tr").hide().removeClass(l.visibleClass),l.hideTFootOnFilter&&e.find("tfoot").hide(),i.find("td").removeClass(l.highlightClass).filter(':filterTableFind("'+t.replace(/(['"])/g,"\\$1")+'")').addClass(l.highlightClass).closest("tr").show().addClass(l.visibleClass)),l.callback&&l.callback(t,e)};return this.each(function(){var t=e(this),i=t.find("tbody"),s=null,r=null,o=null,c=!0;"TABLE"===t[0].nodeName&&i.length>0&&(0===l.minRows||l.minRows>0&&i.find("tr").length>l.minRows)&&!t.prev().hasClass(l.containerClass)&&(l.inputSelector&&1===e(l.inputSelector).length?(o=e(l.inputSelector),s=o.parent(),c=!1):(s=e("<"+l.containerTag+" />"),""!==l.containerClass&&s.addClass(l.containerClass),s.prepend(l.label+" "),o=e('')),l.autofocus&&o.attr("autofocus",!0),e.fn.bindWithDelay?o.bindWithDelay("keyup",function(){n(t,e(this).val())},200):o.bind("keyup",function(){n(t,e(this).val())}),o.bind("click search",function(){n(t,e(this).val())}),c&&s.append(o),l.quickList.length>0&&(r=l.quickListGroupTag?e("<"+l.quickListGroupTag+" />"):s,e.each(l.quickList,function(t,i){var n=e("<"+l.quickListTag+' class="'+l.quickListClass+'" />');n.text(a(i)),"A"===n[0].nodeName&&n.attr("href","#"),n.bind("click",function(e){e.preventDefault(),o.val(i).focus().trigger("click")}),r.append(n)}),r!==s&&s.append(r)),c&&t.before(s))})}}(jQuery); \ No newline at end of file +!function(e){var t=e.fn.jquery.split("."),i=parseFloat(t[0]),a=parseFloat(t[1]);e.expr[":"].filterTableFind=2>i&&8>a?function(t,i,a){return e(t).text().toUpperCase().indexOf(a[3].toUpperCase())>=0}:jQuery.expr.createPseudo(function(t){return function(i){return e(i).text().toUpperCase().indexOf(t.toUpperCase())>=0}}),e.fn.filterTable=function(t){var i={autofocus:!1,callback:null,containerClass:"filter-table",containerTag:"p",hideTFootOnFilter:!1,highlightClass:"alt",inputSelector:null,inputName:"",inputType:"search",label:"Filter:",minChars:1,minRows:8,placeholder:"search this table",quickList:[],quickListClass:"quick",quickListGroupTag:"",quickListTag:"a",visibleClass:"visible"},a=function(e){return e.replace(/&/g,"&").replace(/"/g,""").replace(//g,">")},l=e.extend({},i,t),n=function(e,t){var i=e.find("tbody");""===t||t.length0&&(0===l.minRows||l.minRows>0&&i.find("tr").length>l.minRows)&&!t.prev().hasClass(l.containerClass)&&(l.inputSelector&&1===e(l.inputSelector).length?(o=e(l.inputSelector),s=o.parent(),c=!1):(s=e("<"+l.containerTag+" />"),""!==l.containerClass&&s.addClass(l.containerClass),s.prepend(l.label+" "),o=e('')),l.autofocus&&o.attr("autofocus",!0),e.fn.bindWithDelay?o.bindWithDelay("keyup",function(){n(t,e(this).val())},200):o.bind("keyup",function(){n(t,e(this).val())}),o.bind("click search",function(){n(t,e(this).val())}),c&&s.append(o),l.quickList.length>0&&(r=l.quickListGroupTag?e("<"+l.quickListGroupTag+" />"):s,e.each(l.quickList,function(t,i){var n=e("<"+l.quickListTag+' class="'+l.quickListClass+'" />');n.text(a(i)),"A"===n[0].nodeName&&n.attr("href","#"),n.bind("click",function(e){e.preventDefault(),o.val(i).focus().trigger("click")}),r.append(n)}),r!==s&&s.append(r)),c&&t.before(s))})}}(jQuery); From 88bd389c3f0c5583b52fc4237080ebc04862f00b Mon Sep 17 00:00:00 2001 From: "Kazemi, Darius" Date: Mon, 18 Aug 2014 10:51:41 -0400 Subject: [PATCH 3/4] Updating header in minified js --- jquery.filtertable.min.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/jquery.filtertable.min.js b/jquery.filtertable.min.js index e279992..971096c 100644 --- a/jquery.filtertable.min.js +++ b/jquery.filtertable.min.js @@ -1 +1,12 @@ +/** + * jquery.filterTable + * + * 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. + * + * Utilizes bindWithDelay() if available. https://github.com/bgrins/bindWithDelay + * + * @version v1.5.3 + * @author Sunny Walker, swalker@hawaii.edu + */ !function(e){var t=e.fn.jquery.split("."),i=parseFloat(t[0]),a=parseFloat(t[1]);e.expr[":"].filterTableFind=2>i&&8>a?function(t,i,a){return e(t).text().toUpperCase().indexOf(a[3].toUpperCase())>=0}:jQuery.expr.createPseudo(function(t){return function(i){return e(i).text().toUpperCase().indexOf(t.toUpperCase())>=0}}),e.fn.filterTable=function(t){var i={autofocus:!1,callback:null,containerClass:"filter-table",containerTag:"p",hideTFootOnFilter:!1,highlightClass:"alt",inputSelector:null,inputName:"",inputType:"search",label:"Filter:",minChars:1,minRows:8,placeholder:"search this table",quickList:[],quickListClass:"quick",quickListGroupTag:"",quickListTag:"a",visibleClass:"visible"},a=function(e){return e.replace(/&/g,"&").replace(/"/g,""").replace(//g,">")},l=e.extend({},i,t),n=function(e,t){var i=e.find("tbody");""===t||t.length0&&(0===l.minRows||l.minRows>0&&i.find("tr").length>l.minRows)&&!t.prev().hasClass(l.containerClass)&&(l.inputSelector&&1===e(l.inputSelector).length?(o=e(l.inputSelector),s=o.parent(),c=!1):(s=e("<"+l.containerTag+" />"),""!==l.containerClass&&s.addClass(l.containerClass),s.prepend(l.label+" "),o=e('')),l.autofocus&&o.attr("autofocus",!0),e.fn.bindWithDelay?o.bindWithDelay("keyup",function(){n(t,e(this).val())},200):o.bind("keyup",function(){n(t,e(this).val())}),o.bind("click search",function(){n(t,e(this).val())}),c&&s.append(o),l.quickList.length>0&&(r=l.quickListGroupTag?e("<"+l.quickListGroupTag+" />"):s,e.each(l.quickList,function(t,i){var n=e("<"+l.quickListTag+' class="'+l.quickListClass+'" />');n.text(a(i)),"A"===n[0].nodeName&&n.attr("href","#"),n.bind("click",function(e){e.preventDefault(),o.val(i).focus().trigger("click")}),r.append(n)}),r!==s&&s.append(r)),c&&t.before(s))})}}(jQuery); From 3468da2e0240e09d98d224fef13006ddb850a2f9 Mon Sep 17 00:00:00 2001 From: "Kazemi, Darius" Date: Mon, 18 Aug 2014 10:53:55 -0400 Subject: [PATCH 4/4] Increment version number and add changelog entry --- README.md | 4 ++++ jquery.filtertable.js | 2 +- jquery.filtertable.min.js | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index eca0aec..097b1da 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,10 @@ Other than jQuery, the plugin will take advantage of Brian Grinstead's [bindWith ## Change Log +### 1.5.4 + +- Added a `minChars` option, thanks to [Darius Kazemi](https://github.com/dariusk), which specifies the minimum number of characters a user must enter into a filter before filtering occurs. Default is 1, meaning the moment the user begins to type, filtering will occur. + ### 1.5.3 - **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. diff --git a/jquery.filtertable.js b/jquery.filtertable.js index d64ab43..2785b0f 100644 --- a/jquery.filtertable.js +++ b/jquery.filtertable.js @@ -6,7 +6,7 @@ * * Utilizes bindWithDelay() if available. https://github.com/bgrins/bindWithDelay * - * @version v1.5.3 + * @version v1.5.4 * @author Sunny Walker, swalker@hawaii.edu */ (function($) { diff --git a/jquery.filtertable.min.js b/jquery.filtertable.min.js index 971096c..d080fd4 100644 --- a/jquery.filtertable.min.js +++ b/jquery.filtertable.min.js @@ -6,7 +6,7 @@ * * Utilizes bindWithDelay() if available. https://github.com/bgrins/bindWithDelay * - * @version v1.5.3 + * @version v1.5.4 * @author Sunny Walker, swalker@hawaii.edu */ !function(e){var t=e.fn.jquery.split("."),i=parseFloat(t[0]),a=parseFloat(t[1]);e.expr[":"].filterTableFind=2>i&&8>a?function(t,i,a){return e(t).text().toUpperCase().indexOf(a[3].toUpperCase())>=0}:jQuery.expr.createPseudo(function(t){return function(i){return e(i).text().toUpperCase().indexOf(t.toUpperCase())>=0}}),e.fn.filterTable=function(t){var i={autofocus:!1,callback:null,containerClass:"filter-table",containerTag:"p",hideTFootOnFilter:!1,highlightClass:"alt",inputSelector:null,inputName:"",inputType:"search",label:"Filter:",minChars:1,minRows:8,placeholder:"search this table",quickList:[],quickListClass:"quick",quickListGroupTag:"",quickListTag:"a",visibleClass:"visible"},a=function(e){return e.replace(/&/g,"&").replace(/"/g,""").replace(//g,">")},l=e.extend({},i,t),n=function(e,t){var i=e.find("tbody");""===t||t.length0&&(0===l.minRows||l.minRows>0&&i.find("tr").length>l.minRows)&&!t.prev().hasClass(l.containerClass)&&(l.inputSelector&&1===e(l.inputSelector).length?(o=e(l.inputSelector),s=o.parent(),c=!1):(s=e("<"+l.containerTag+" />"),""!==l.containerClass&&s.addClass(l.containerClass),s.prepend(l.label+" "),o=e('')),l.autofocus&&o.attr("autofocus",!0),e.fn.bindWithDelay?o.bindWithDelay("keyup",function(){n(t,e(this).val())},200):o.bind("keyup",function(){n(t,e(this).val())}),o.bind("click search",function(){n(t,e(this).val())}),c&&s.append(o),l.quickList.length>0&&(r=l.quickListGroupTag?e("<"+l.quickListGroupTag+" />"):s,e.each(l.quickList,function(t,i){var n=e("<"+l.quickListTag+' class="'+l.quickListClass+'" />');n.text(a(i)),"A"===n[0].nodeName&&n.attr("href","#"),n.bind("click",function(e){e.preventDefault(),o.val(i).focus().trigger("click")}),r.append(n)}),r!==s&&s.append(r)),c&&t.before(s))})}}(jQuery);