jsTree search query for child nodes only

╄→尐↘猪︶ㄣ 提交于 2021-01-27 17:51:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!