如果搜索字符串不匹配任何节点,jstree 将显示所有节点

jstree showing all nodes if search string didn't match any node

我正在使用以下配置渲染 jstree

$('#deliverables').jstree({
    'core': {
        'data': data
    },
    'search': {
        'case_insensitive': true,
        'show_only_matches' : true
    },
    'plugins': ['search']
});

$('#deliverable_search').keyup(function(){
    $('#deliverables').jstree('search', $(this).val());
});

使用此配置,jstree 仅在搜索文本找到至少一个节点时才显示匹配的节点。但是如果搜索文本与任何节点不匹配,jstree 会显示所有节点。我觉得这有点奇怪。我在这里遗漏了什么吗?

https://jsfiddle.net/t9fe58rt/1/ link供大家参考。

这是一种有意的行为,参见:https://github.com/vakata/jstree/issues/1192#issuecomment-128042329

但是您可以将处理程序附加到 search event and if the result is empty act accordingly, eg. you can hide the all the tree nodes using hide_all 方法。

代码:

.on('search.jstree', function (nodes, str, res) {
    if (str.nodes.length===0) {
        $('#deliverables').jstree(true).hide_all();
    }
})

但不要忘记在触发新搜索之前将它们全部显示出来:

$('#deliverable_search').keyup(function(){
    $('#deliverables').jstree(true).show_all();
    $('#deliverables').jstree('search', $(this).val());
});

演示:https://jsfiddle.net/xfn8aa19/

对我来说,Irvin Dominin 的回答还不够

 $('#deliverable_search').keyup(function () {
        $('#deliverables').jstree(true).show_all();
        $('.jstree-node').show();
        $('#deliverables').jstree('search', $(this).val());
        $('.jstree-hidden').hide();
        $('a.jstree-search').parent('li').find('.jstree-hidden').show();
    });