【问题标题】:JQuery Dynatree - How to search node with level delimiter?JQuery Dynatree - 如何使用级别分隔符搜索节点?
【发布时间】:2013-12-20 06:52:47
【问题描述】:

我需要为我的 dynatree 使用搜索功能,所以我找到了这个解决方法:JQuery Dynatree - search node by name

但是,我只需要让它搜索到我的扩展节点分隔符。 (我正在使用 jQuery ui-slider 来动态设置扩展分隔符)。 最初,我需要它搜索直到我的 minExpandedLevel。如果我移动滑块,dynatree 应该仅显示匹配结果,展开级别相当于滑块值。

尝试重置 minExpandLevel 并重新加载 dynatree 是行不通的,因为它会返回所有(甚至不匹配的)节点作为结果。

所以我想在搜索功能中添加一个限制参数,例如:

$(selector).dynatree("getRoot").search(pattern, limit);

有人知道怎么做吗?

这是我的代码:

动力树:

$.ui.dynatree.nodedatadefaults["icon"] = false;

$("#resultTree").dynatree({
    minExpandLevel: 4,
    persist: false,
    classNames: {
        vline: "no-bg",
        connector: "",
        expander: "ui-helper-hidden"
    },
    children: myJsonData
});

滑块:

timeout = false;
searchTerm = $("#searchText").val();
$("#treeslider").slider({
    min: minTick,
    max: maxTick,
    range: "min",
    slide: function (event, ui) {
        if (timeout) {
            clearTimeout(timeout);
        }
        timeout = setTimeout(function () {
            $("#resultTree").dynatree("getRoot").search(searchTerm, ui.value);
        }, 500);

    }
});

【问题讨论】:

    标签: jquery search dynatree jquery-dynatree


    【解决方案1】:

    这是一段从根开始并访问每个节点但不处理级别 3 或更低级别的节点的代码:

    $("#tree").dynatree("getRoot").visit(function(node){
    
        if( node.getLevel() > 2) {
            return 'skip';
        }
    
        console.log('processing node "' + node.data.title + '" at level ' + node.getLevel());
    
    });
    

    如果返回字符串'skip',访问函数将停止处理分支。

    【讨论】:

    • 哦,谢谢,这适用于自上而下的搜索,但我需要一种自下而上的搜索方法。这就是为什么我需要使用_searchNode 方法。现在我如何搜索直到最后一个节点并在它大于我的级别分隔符时隐藏它们?
    • 这样,即使只有最后一个节点与搜索词匹配,它也会显示为
    【解决方案2】:

    好的,我想我找到了答案:

    我修改了_searchNode 函数,因此它会隐藏大于级别分隔符的匹配节点,但只要术语在其子项中匹配就会显示父节点(甚至不匹配)。

    var clear = true;
    DynaTreeNode.prototype.search = function (pattern,limit) {
    if (typeof limit == "undefined") {
        limit = 0;
    }
    
    if (pattern.length < 1 && !clear) {
        clear = true;
        this.visit(function (node) {
            node.expand(true);
            node.li.hidden = false;
            node.expand(false);
        });
    } else if (pattern.length >= 1) {
        clear = false;
        this.visit(function (node) {
            node.expand(true);
            node.li.hidden = false;
        });
        var searchDepth = 1;
        for (var i = 0; i < this.childList.length; i++) {
            var hide = { hide: false };
            this.childList[i]._searchNode(pattern, hide, searchDepth, limit);
        }
    }
    },
    
    // bottom-up node searching function
    DynaTreeNode.prototype._searchNode = function (pattern, hide, searchDepth, limit) {
        var level = searchDepth;
        if (this.childList) {
            // parent node
            var hideNode = true;
            var searchDepth = level+1;
            for (var i = 0; i < this.childList.length; i++) {
                var hideChild = { hide: false };
                this.childList[i]._searchNode(pattern, hideChild, searchDepth, limit);
                hideNode = hideNode && hideChild.hide;
            }
    
            if (hideNode && !this._isRightWithPattern(pattern)) {
                this._hideNode();
                hide.hide = true;
            } else {
                if (limit && level > limit) {
                    this._hideNode();
                }
                hide.hide = false;
            }
    
        } else {
            // leaf        
            if (!this._isRightWithPattern(pattern)) {
                this._hideNode();
                hide.hide = true;
            } else {
                if (limit && level > limit) {
                    this._hideNode();
                }
                hide.hide = false;
            }
        }
    }
    

    【讨论】:

    • 嗨,这似乎正是我所追求的,但我完全不知道把它放在哪里!您能否简要介绍一下哪个文件(我认为是 jquery.dynatree.js)和下落。
    • 不要认为这是一个“答案”,但最终设法找到了放置此代码的位置。对于其他任何人(还有一些),查找“var DynaTree = Class.create();”行然后粘贴上面的代码。它确实 100% 有效:)
    猜你喜欢
    • 1970-01-01
    • 2018-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    • 2016-08-30
    相关资源
    最近更新 更多