Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript getNodeById不工作_Javascript - Fatal编程技术网

Javascript getNodeById不工作

Javascript getNodeById不工作,javascript,Javascript,我有一个带有json的ext treepanel 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,

我有一个带有json的ext treepanel

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加载以下json代码

[{"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":[]}]}]
当我试图通过console.log(tree.getNodeByid(3))查找节点时,它显示该节点未定义

我的代码有问题吗?

tree.getNodeById(…)只查找展开的节点

您可以按如下所示找到节点路径,然后展开路径并获得节点

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);
}
var MyTree=Ext.extend(Ext.tree.TreePanel{
getPath:函数(id){
var node=this.getNodeById(id);
如果(节点){
返回node.getPath();
}
var path=this.root.getPath();
forEach=函数(列表、乐趣、sope){
如果(!list | | list.length==0){
返回;
}
for(var i=0,length=list.length;i3){
for(var ii=3;ii