Thursday, 29 August 2013

Enable exact multi word search for node titles with the jstree search plugin

Enable exact multi word search for node titles with the jstree search plugin

I use the jstree search plugin (documentation) to search for multiple IDs
in the title fields of my HTML tree.
I took the original jstree code (line 3398) and changed it as suggested
here to enable multi word searching in the title fields of the tree.
It works fine for "title contains" queries (e.g. title ID:40857332 of node
x and title ID:408 of node y contain the search word ID:408) but I'm at
loss how to change the code in order to find only exact matches (e.g.
title ID:408 of node y matches the search word ID:408 exactly).
The function gets called like this:
$("#my_html_tree").jstree("search", search_words);
with the following configurations:
"search" : {"case_insensitive" : true, "search_method":
"jstree_title_contains_multi"}
The variable "search_words" is a string containing several IDs:
var search_words = "ID:4 ID:7 ID:3188";
The format of the HTML tree nodes:
<li id="3188"> <a title="ID:3188">Tree node 3188</a></li>
This is my changed code:
$.expr[':'].jstree_title_contains_multi = function(a,i,m){
var word, words = [];
var searchFor =
m[3].toLowerCase().replace(/^\s+/g,'').replace(/\s+$/g,'');
if(searchFor.indexOf(' ') >= 0) {
words = searchFor.split(' ');
}
else {
words = [searchFor];
}
for (var i=0; i < words.length; i++) {
word = words[i];
if((a.getAttribute("title") || "").toLowerCase().indexOf(word) >=
0) {
return true;
}
}
return false;
};
How must I change the code in order to enable searching only for exact
matches?
Any help would be greatly appreciated.

No comments:

Post a Comment