zTree模糊查询,匹配则显示父子节点,否则隐藏

var hiddenNodes = []; //用于存储被隐藏的结点
    var nodeList;
    //过滤ztree显示数据
    function filter() {
        显示上次搜索后背隐藏的结点
        //zTreeObj.showNodes(hiddenNodes);

        查找不符合条件的叶子节点
        //function filterFunc(node) {
        //    var keywords = $("#keyword").val();
        //    if (node.isParent || node.Name.indexOf(keywords) != -1) return false;
        //    return true;
        //};

        获取不符合条件的叶子结点
        //hiddenNodes = zTreeObj.getNodesByFilter(filterFunc);

        隐藏不符合条件的叶子结点
        //zTreeObj.hideNodes(hiddenNodes);

        //zTreeObj.expandAll(true);

        var allNode = zTreeObj.transformToArray(zTreeObj.getNodes());
        zTreeObj.hideNodes(allNode);

        function filterFunc(node) {
            var keywords = $("#keyword").val();
            if ( node.Name.indexOf(keywords) != -1) return true;
            return false;
        };

        var keywords = $("#keyword").val();
        if (keywords == "") {
            zTreeObj.showNodes(allNode);
            zTreeObj.expandAll(false);
        } else {
            //var nodeList = zTreeObj.getNodesByParamFuzzy("text", keywords, null);
            nodeList = zTreeObj.getNodesByFilter(filterFunc);
            nodeList = zTreeObj.transformToArray(nodeList);
            for (var n in nodeList) {
                if (nodeList.hasOwnProperty(n)) {
                    findParent(zTreeObj, nodeList[n]);
                }
            }
            zTreeObj.showNodes(nodeList);
        }
    }

    function findParent(treeObj, node) {
        treeObj.expandNode(node, true, false, false);
        var pNode = node.getParentNode();
        if (pNode != null) {
            nodeList.push(pNode);
            findParent(treeObj, pNode);
        }
    };

你可能感兴趣的:(js)