PDA

View Full Version : Select First Node In A Tree after Render



TampaBay55
25 Mar 2010, 11:16 AM
Greetings:

I want to select the first node (if it exists) in a tree after the tree has been rendered. This seems simple but I can't wrap my brain around it:



var tr = new Ext.tree.AsyncTreeNode({
text: 'Root',
listeners:{
'load': function(tl,node,resp){
if(tl.childNodes.length==0){
//do something here
} else {
mytreePanel.getSelectionModel().select(tl.item(0));
//do more stuff here
}
}
}
});


This doesn't seem to work and I am sure there is a logical reason why. Can anyone help? Thanks

mpawlowski
25 Mar 2010, 1:24 PM
I had a similar problem where I couldn't access the children of a tree. The solution I found to this was by setting the loader:


loader: new Ext.tree.TreeLoader({
preloadChildren: true
})

Not sure if thats exactly what your problem is, but it might help.

TampaBay55
28 Mar 2010, 5:15 AM
Thanks for the response. Unfortunately it didn't help.

steffenk
28 Mar 2010, 1:09 PM
this should work in listener of TreePanel:


afterrender: function(pnl) {
if (pnl.getRootNode().hasChildNodes()) {
pnl.getSelectionModel().select(pnl.getRootNode());
} else {
console.log('no childs');
}
}

TampaBay55
29 Mar 2010, 11:44 AM
Steffenk:

I am not acutally trying to select the root node itself but the first child node. Your post did help to show me I was using the wrong event though...thanks.

mschwartz
29 Mar 2010, 11:52 AM
treePanel.root.firstChild && treePanel.root.firstChild.select();

TampaBay55
29 Mar 2010, 12:01 PM
Perfect. Exactly what I needed. Thanks.
Thanks to everyone for the help.