Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to split results between rows #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
47 changes: 42 additions & 5 deletions jquery.quicksearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
noResults: '',
matchedResultsCount: 0,
bind: 'keyup',
splitResults: false,
onBefore: function () {
return;
},
Expand All @@ -25,28 +26,62 @@
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,
numMatchedRows = 0,
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]);
}
Expand Down Expand Up @@ -130,14 +165,15 @@
}

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);
});

rowcache = jq_results.map(function () {
return this;
});

/*
* Modified fix for sync-ing "val".
* Original fix https://github.com/michaellwest/quicksearch/commit/4ace4008d079298a01f97f885ba8fa956a9703d1
Expand Down Expand Up @@ -173,6 +209,7 @@

val = $(this).val();
e.trigger();

});
});

Expand Down