PDA

View Full Version : TreePanel Add Node Dynamically



kamalhg
11 Oct 2010, 5:51 AM
Hello,

I am using TreePanel and I'd like to add tree nodes dynamically to the tree. but after adding a new node it does not appear in the treepanel and when expanding the its parent node it shows the loading icon and nothing happen.
you can see the example here (http://www.cartologic.com/tree-example.html)
this is my code:



Ext.onReady(function(){
var root = new Ext.tree.AsyncTreeNode({
expanded: true,
children:[{
id: 'node-1',
text: 'Node 1',
leaf: true,
checked: false
},
{
id: 'node-2',
text: 'Node 2',
leaf: false,
checked: true
}]
});
var i = 1;
var tree = new Ext.tree.TreePanel({
width:400,
height:500,
id: 'tree-panel',
margins: '2 2 0 2',
autoScroll: true,
rootVisible: false,
root: root,
// Our custom TreeLoader:
loader: new Ext.tree.TreeLoader(),
fbar:[{
text:'Add Dynamic Node',
handler:function(){
var node = tree.getNodeById('node-2');
var n = node.appendChild({
text:'New Node ' + i++,
leaf: true,
checked: true
});
//console.debug(node);
}
}]
});
tree.render(Ext.getBody());

});

Thanks in advance.

Condor
11 Oct 2010, 6:15 AM
An AsyncTreeNode will load it's children when expanded. You want a normal TreeNode instead (specify nodeType:'node' in the node attributes).

kamalhg
11 Oct 2010, 6:23 AM
Thanks Condor.
It works. :D