PDA

View Full Version : Problem with .reload() on a leaf tree node



Achim74
14 Apr 2011, 7:22 AM
Hello !

I have a tree with a remote datasource. I have a function where I can add child elements to a tree node. This happens on server side. After the new entry in the database is created I use this:



mytree.getNodeById(parent_id).reload();


Of course the var parent_id contains the correct node_id. This works very well on nodes that already have children. On a leaf node this does not work. Can I manually set it on "not leaf" before reloading the tree? Or is the only way to use addChildNode?

Greetz
Achim

razvanioan
14 Apr 2011, 12:52 PM
Hello Achim,

This was a though one for me as well, as I've struggled all this evening to find the best way to do it.
The ideea is that 'leaf' nodes, normally do not need a load() method (as they do not have children, when first time loaded), and I wanted to be able to first add a child to it, and then reload (only that node).

I've found two solutions, but last one fits my needs (where I'm using TreeLoader.load() method directly):

Solution 1:
After a successful add (to the server-side) I was returning the ID of the new generated node + I knew the text from the add form, so I added the node manually to the desired node (which was leaf before).



if(node.isLeaf()){
node.leaf = false;
node.setCls('folder');
}

node.appendChild(
new Ext.tree.AsyncTreeNode({
cls: 'leaf',
leaf: true,
checked: false,
id: action.result.new_id // sent from server,
text: title (from form text field or from server...)
})
)

node.expand();


Solution 2:
As I wanted to still use server-side (re)load method, and don't remember if I still had some rendering issues with second added node or to nodes added to already rendered as folder nodes, I came up with this method instead. Also with the method above you can appendChildren only to the end of child nodes of a parent which may not always be the desired sorting.....



// if leaf, add folder ui
if(node.isLeaf()){
node.leaf = false;
node.setCls('folder');
}

// if node was loaded as folder first time, use reload
if(node.reload){
node.reload();
} else {
// otherwyse, use TreeLoader.load() method, and force an expand when finished
node.getLoader().load(node,function(node){
node.expand();
});
}



Of course that here you can use even a more simple solution, but I've prefered to use reload() method for nodes which were loaded as 'folder' from start:


// if leaf, add folder ui
if(node.isLeaf()){
node.leaf = false;
node.setCls('folder');
}

node.getLoader().load(node,function(node){
node.expand();
});


It would have been nice to have an easy solution to swap (and add or remove all new functionality) node types from leaf to folder and vice-versa.

Hope it helps,
Razvan