7

我想开始在我的页面上使用 Dynatree,但是我可能需要按名称搜索我的树。你知道怎么做吗?

4

4 回答 4

15

我不仅需要匹配节点,还需要到这些节点的整个路径。我写了这个功能,它对我有用。

库的修改:

var clear = true;

DynaTreeNode.prototype.search = function(pattern){

    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;
        });

        for (var i = 0; i < this.childList.length; i++){
            var hide = {hide: false};
            this.childList[i]._searchNode(pattern, hide);
        }
    } 
},

DynaTreeNode.prototype._searchNode = function(pattern, hide){

    if (this.childList){
        // parent node

        var hideNode = true;
        for(var i = 0; i < this.childList.length; i++){
            var hideChild = {hide: false};
            this.childList[i]._searchNode(pattern, hideChild);
            hideNode = hideNode && hideChild.hide;
        }
        if(hideNode && !this._isRightWithPattern(pattern)){
            this._hideNode();
            hide.hide = true;
        } else {
            hide.hide = false;
        }

    } else {
        // leaf
        if (!this._isRightWithPattern(pattern)){
            this._hideNode();
            hide.hide = true;
        } else {
            hide.hide = false;
        }
    }
},

DynaTreeNode.prototype._isRightWithPattern = function(pattern){
    if((this.data.title.toLowerCase()).indexOf(pattern.toLowerCase()) >= 0){
        return true;
    }
    return false;
},

DynaTreeNode.prototype._hideNode = function(){
    if(this.li) {
      this.li.hidden = true;
    }
}

利用:

$("tree").dynatree("getRoot").search(pattern);
于 2012-10-02T09:19:48.017 回答
1

目前没有搜索功能,但你可以使用这样的东西(未测试)

var match = null;
tree.visit(function(node){
    if(node.data.title === "foo"){
        match = node;
        return false; // stop traversal (if we are only interested in first match)
    }
});
alert("Found " + match);
于 2012-09-05T11:55:52.023 回答
0

I've done it this way

<style>
span.occurance a.dynatree-title{background-color:#3AFF22;}
</style>


DynaTreeNode.prototype.find = function (needle) {
    needle = (needle || '');
    if (needle.length >= 1) {
        var occurs = [];
        this.visit(function (node) {
            $(node.span).removeClass('occurance'); //remove pervious findings
            if (node.data.title.indexOf(needle) != -1) {
                occurs.push(node);
                node._expandPath();
            }
        });
        for (indx in occurs) { // mark findings
            $(occurs[indx].span).addClass('occurance');
        }
    } else {
        $('span.dynatree-node.occurance').removeClass('occurance');
    }
},
DynaTreeNode.prototype._expandPath = function () {
    var path = [],
        node = this;
    while (node = node.getParent()) {
        path.push(node);
    }
    for (indx in path) {
        path[indx].expand(true)
    }
}

usage:

[your selector].dynatree("getRoot").find('needle');
于 2013-05-04T05:45:05.457 回答
0

感谢@mar10,我做了一个小而简单的函数来搜索带有标题的节点:

// If searchFrom is null, root is used
function seachFolderNodeWithName(name, searchFrom) {
    if (name == null) {
        return undefined;
    }

    if (searchFrom == null) {
        searchFrom = jQuery('#tree').dynatree("getRoot");
    }

    var match = undefined;

    searchFrom.visit(function (node) {
        if (node.data.title === name) {
            match = node;
            return false; // Break if found
        }
    });
    return match;
};
于 2013-05-13T14:12:54.270 回答