开发者

getNodeById does not work

I have a ext treepanel with json.

var tree = new Ext.tree.TreePanel({
renderTo:'tree-container',
title: 'Category',
height: 300,
width: 400,
useArrows:true,
autoScroll:true,
animate:true,
enableDD:true,
containerScroll: true,
rootVisible: false,
frame: true,
root: {
    text: 'Category',
    draggable: false,
开发者_开发知识库    id: '0'
},

// auto create TreeLoader
dataUrl: $("#web").val() + "/category/index/get-nodes",

listeners: {
    'checkchange': function(node, checked){
        if(checked){
                categoryManager.add(node.id);
            //node.getUI().addClass('complete');
        }else{
                categoryManager.remove(node.id);
           // node.getUI().removeClass('complete');
        }
    }
}

});

dataUrl loads the following json code

[{"text":"Code Snippet","id":"1","cls":"folder","checked":false,"children":[{"text":"PHP","id":"3","cls":"file","checked":false,"children":[]},{"text":"Javascript","id":"4","cls":"file","checked":false,"children":[]}]}]

when I try to find a node by console.log( tree.getNodeByid(3) ), it shows that it is undefined.

Do I have a problem with my code?


tree.getNodeById(...) find only node that expanded

you can find the node path as follow, and than expand the paths and get the node

var MyTree = Ext.extend(Ext.tree.TreePanel, {
getPath : function(id){
        var node = this.getNodeById(id);
        if(node){
            return node.getPath();
        }
        var paths = this.root.getPath();
        forEach = function(list, fun, sope){
            if(!list || list.length == 0){
                return;
            }
            for(var i = 0, length = list.length; i < length; i++){
                var node = list[i];
                var args = [];
                args.push(node);
                if(arguments.length > 3){
                    for(var ii = 3; ii < arguments.length; ii++){
                        args.push(arguments[ii]);
                    }
                }
                var result = fun.apply(sope, args);
                if(result){
                    return result;
                }
            }
        };

        getChildNodes = function(parent){
            var children = parent.children || parent.childNodes;
            if(children && children.length == 0 && parent.attributes){
                children = parent.attributes.children;
            }
            return children;
        };

        getPath = function(item, paths){
            if(item.id == id){
                return paths + "/" + item.id;
            }
            return forEach(getChildNodes(item), getPath, this, paths + "/" + item.id);
        };
        return forEach(getChildNodes(this.root), getPath, this, paths);
    }
});

var tree = new MyTree(....);
var paths = tree.getPath(id);
if(paths){
  this.expandPath(paths);
  var node = tree.getNodeById(id);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜