From ddd41f6827cfd2f9ff30d9ad9fac40a8abb98d6d Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 15 Feb 2012 06:16:34 -0500 Subject: [PATCH 1/2] testQuery now returns false if NONE of query words are present. --- jquery.quicksearch.js | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/jquery.quicksearch.js b/jquery.quicksearch.js index 543d828..dc36ad5 100644 --- a/jquery.quicksearch.js +++ b/jquery.quicksearch.js @@ -25,15 +25,37 @@ return val.toLowerCase().split(' '); }, testQuery: function (query, txt, _row) { + /* + Edit Description: + For my use case, I want to retain rows that contain at least one of multiple query words. The as-is functionality only retains rows that contain all the query words. + + Increment x when a word doesn't appear in a row. + if x == number of terms in query (query.length) ,then there are no matches, return false, else, at least one of the terms matches, so return true to keep the row in the result set + */ + + //Edit Start: setup negative match counter + var x = 0 + //end edit: + for (var i = 0; i < query.length; i += 1) { if (txt.indexOf(query[i]) === -1) { - return false; + //Edit Start: + //return false; + //if the query term doesn't match the txt, increment negative match counter + x++; + //end edit: } } - return true; + //Edit Start: + //if negative match counter equals the number of query terms, return false, else at least one term + //matched, so return true + if(x == query.length){return false;} + else{return true;} + //return true; + //end edit: } }, opt); - + this.go = function () { var i = 0, @@ -41,12 +63,14 @@ noresults = true, query = options.prepareQuery(val), val_empty = (val.replace(' ', '').length === 0); - + for (var i = 0, len = rowcache.length; i < len; i++) { if (val_empty || options.testQuery(query, cache[i], rowcache[i])) { + options.show.apply(rowcache[i]); noresults = false; numMatchedRows++; + } else { options.hide.apply(rowcache[i]); } @@ -130,6 +154,7 @@ } var t = (typeof options.selector === "string") ? jq_results.find(options.selector) : $(target).not(options.noResults); + cache = t.map(function () { return e.strip_html(this.innerHTML); }); @@ -137,7 +162,7 @@ rowcache = jq_results.map(function () { return this; }); - + /* * Modified fix for sync-ing "val". * Original fix https://github.com/michaellwest/quicksearch/commit/4ace4008d079298a01f97f885ba8fa956a9703d1 @@ -173,6 +198,7 @@ val = $(this).val(); e.trigger(); + }); }); From 3c11d0ed2a8a5d11e1de984abed4d249d8de8952 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 15 Feb 2012 06:38:43 -0500 Subject: [PATCH 2/2] created splitResults option to control results across rows --- jquery.quicksearch.js | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/jquery.quicksearch.js b/jquery.quicksearch.js index dc36ad5..f806002 100644 --- a/jquery.quicksearch.js +++ b/jquery.quicksearch.js @@ -9,6 +9,7 @@ noResults: '', matchedResultsCount: 0, bind: 'keyup', + splitResults: false, onBefore: function () { return; }, @@ -31,27 +32,37 @@ Increment x when a word doesn't appear in a row. if x == number of terms in query (query.length) ,then there are no matches, return false, else, at least one of the terms matches, so return true to keep the row in the result set + + options.splitResults set in options */ //Edit Start: setup negative match counter - var x = 0 + var negativeMatchCount = 0; //end edit: - for (var i = 0; i < query.length; i += 1) { + if (txt.indexOf(query[i]) === -1) { //Edit Start: - //return false; //if the query term doesn't match the txt, increment negative match counter - x++; + if(options.splitResults){ + negativeMatchCount++; + }else{ + //original return if splitResults option not set + return false; + } //end edit: } } - //Edit Start: - //if negative match counter equals the number of query terms, return false, else at least one term - //matched, so return true - if(x == query.length){return false;} - else{return true;} - //return true; + /* + Edit Start: + if negative match counter equals the number of query terms, return false, + else at least one term matched, so return true + */ + if(negativeMatchCount == query.length && options.splitResults){ + return false; + }else{ + return true; + } //end edit: } }, opt);