View Full Version : When can I call getNodeById()

21 Oct 2010, 6:58 PM
So I have TreePanel. I get JSON data all at once, does that mean that the tree is fully loaded?
Suppose there is node A, and is has child node B. when I view A in firebug without expanding it, I can see that it has children. But, tree.getNodeById('B') still returns undefined, why is that? it returns B only after A is expanded.

But I don't want to show A as expanded. I was thinking to expand all nodes and then collapse them, cause I need to access those inner nodes without showing the tree as expanded. Any ideas?
doing tree.expand(true); tree.collapse(true); doesn't work I guess because i call collapse while the nodes are expanding. So in this case the question is when should I call collapse(true)?


21 Oct 2010, 8:05 PM
You should check the differences between the AsyncTreeNode (http://dev.sencha.com/deploy/dev/docs/?class=Ext.tree.AsyncTreeNode) and the TreeNode (http://dev.sencha.com/deploy/dev/docs/?class=Ext.tree.TreeNode). That describes some of what you're describing.

We can't actually help you when you don't give us a code snippet, but are you attempting to call getById on a tree before it loads, and then seeing that it's loaded in firebug, after the fact? Generally, you'd add a listener for the event you want to use to trigger your function. You can use the tree loader's load event, I think, in this case, to call getById, which should then respond properly. Again, without seeing your code, it's difficult to provide correct assistance.

And finally, the json nodes coming back can defined "expanded: false" i believe, check the docs. Also the use of the leaf property is important to defining what is expandable.

22 Oct 2010, 3:10 AM
So your server is returning all nodes AND all child nodes (as 'children') in one single request?

Did you configure your TreeLoader with preloadChildren:true?

Even after doing this, you still have to wait for the load (or expand) event on the root node before you can access child nodes.

22 Oct 2010, 7:12 AM
First, thanks for replies. here is the code:

var tree = new Ext.tree.TreePanel({
title: 'The Tree',
height: 300,
width: 400,
rootVisible: false,
root: { },
loader : new Ext.tree.TreeLoader({
dataUrl: 'check-nodes.json',
preloadChildren : true


and check-nodes.json has

text: 'To Do',
cls: 'folder',
id: 'todo',
children: [{
text: 'Go jogging',
leaf: true,
checked: false,
text: 'Take a nap',
leaf: true,
checked: false,
id: 'nap'
}, ....

why doesn't it load everything at once? I did add preloadChildren = true on the loader?
the button is there just for testing, i click on it after everything is loaded and firebug shows that 'todo' node is loaded = false

24 Oct 2010, 3:06 AM
yes, i met the same problom, waiting for help

25 Oct 2010, 2:47 PM
after looking into the code i discovered that calling load function will load with loadChildren = true will load all the way down, but not if that node is the root, so i call load on every child of the root node, which will load the whole tree.
add listener to 'load':

if(node.id == 'src'){
for(i=0; i < node.childNodes.length; i++){
var n = node.childNodes[i];

it seems to work but if anybody finds a better solution, please post)

25 Oct 2010, 10:39 PM
This is weird. That is exactly what preloadChildren should be doing...