search - JQuery Dynatree - 按名称搜索节点

标签 search dynatree jquery-dynatree

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

最佳答案

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

库的修改:

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

关于search - JQuery Dynatree - 按名称搜索节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12277797/

相关文章:

search - 过滤一个新的自定义字段 Phreeze

ios - 快速传递参数?

javascript - 使用不带节点图标的 dynaTree

DynaTree 在左键单击后右键单击时有两个突出显示的节点

javascript - 具有真实复选框的 Dynatree

php - 如何重新排序搜索结果?

language-agnostic - 如何在文本中搜索人名? (启发式)

javascript - 如何在 dynatree 中以编程方式选择子节点?

javascript - 如何在空动态树上删除节点

javascript - 来自 <ul> 结构的 js dynatree 插件 - 如果其根节点禁用单选按钮选择