问题
I am using jsTree with the search plugin. Now I want the search to only match child nodes and not parents nodes.
This is what I've implemented:
"search": {
case_insensitive: true,
show_only_matches: true,
search_leaves_only: true,
}
Here's an example of my tree:
▼ Parent1
└ child 1
ParentABC
▼ Parentxyz
└ child abc
ParentABC
has no child node and if I search for "abc", both ParentABC
and child abc
is shown but the expected result should have been child abc
only.
search_leaves_only: true
works great on the child nodes because they have no child nodes themselves. jsTree considers a node as leaf when it has no child node.
So in what way can I make my search query for child nodes only?
EDIT: SOLUTION
I just had to make use of search_callback
with a custom function. Here it is:
"search": {
show_only_matches: true,
search_callback: function (searchString, node) {
if (node.parent != "#" && node.text.toUpperCase().includes(searchString.toUpperCase()) == true) {
return node;
}
}
}
The way I'm populating my tree is with JSON data and all my parent nodes have the attribute parent
as "#"
. case_insensitive: true
does not work anymore, that's why I use toUpperCase()
来源:https://stackoverflow.com/questions/50856732/jstree-search-query-for-child-nodes-only