getNodeById не работает - PullRequest
       32

getNodeById не работает

0 голосов
/ 14 апреля 2010

У меня есть ext treepanel с 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 загружает следующий код 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)), он показывает, что он не определен.

У меня проблема с моим кодом?

1 Ответ

2 голосов
/ 18 октября 2010

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);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...