diff --git a/jquery.quicksearch.js b/jquery.quicksearch.js index 543d828..f806002 100644 --- a/jquery.quicksearch.js +++ b/jquery.quicksearch.js @@ -9,6 +9,7 @@ noResults: '', matchedResultsCount: 0, bind: 'keyup', + splitResults: false, onBefore: function () { return; }, @@ -25,15 +26,47 @@ 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 + + options.splitResults set in options + */ + + //Edit Start: setup negative match counter + var negativeMatchCount = 0; + //end edit: for (var i = 0; i < query.length; i += 1) { + if (txt.indexOf(query[i]) === -1) { - return false; + //Edit Start: + //if the query term doesn't match the txt, increment negative match counter + if(options.splitResults){ + negativeMatchCount++; + }else{ + //original return if splitResults option not set + return false; + } + //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(negativeMatchCount == query.length && options.splitResults){ + return false; + }else{ + return true; + } + //end edit: } }, opt); - + this.go = function () { var i = 0, @@ -41,12 +74,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 +165,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 +173,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 +209,7 @@ val = $(this).val(); e.trigger(); + }); });