View Full Version : How do I effectively set a new root node in a tree

3 Aug 2007, 9:43 AM
using version 1.0.1a

I've got a fairly large tree structure I'm displaying to the user. There are cases where the hierarchy that drives the data display gets modified/reset and I need to completely redraw/reorder my tree.

The fastest way to do this is to simply start over with the new structure model and re-query the server for the top-level nodes. However I first have to clear out the tree.

Since the root node can have several hundred or even thousands of children the following can cause the browser to become unresponsive for a few minutes.

var treeRoot = treeManager.myTree.getRootNode();
var children = treeRoot.childNodes;
for (var i=children.length - 1; i>=0; i--) {

Another option is to just blow away the children

treeRoot.childNodes = [];

I haven't tried this but it looks like it could cause a lot of memory leaks etc. After all the node has other members like firstChild, lastChild etc.

I've also tried making a new root node like this

var newRoot =
new Ext.tree.TreeNode();

However, this doesn't delete the old root it simply makes a tree with two roots in it. All my old nodes stay and the operations I have defined for the tree (expand all, collapse all etc) only opperate on the new half of the tree. Looking at the code in ext-all-debug.js it seems that the setRootNode() method doesn't do any sort of cleanup of the original root and simply leaves it registered.

My last attempt was to take the original root and pass it to a cleanup function that wouldn't be greedy with the CPI. This doesn't even walk the whole tree but just removes all the children of the root node.

var oldRoot = myTree.getRootNode();

var newRoot = new Ext.tree.TreeNode();


yieldingTreeDelete: function(node){
for(var i = node.childNodes.length -1; i >= 0; i--){
node.removeChild.defer(5,null, [node.childNodes[i] );

This gives the following error

this.ownerTree has no properties. ext-all-debug.js(line 17616)

It's clear that there is a dependency between a node and its tree and the node can't be removed from its parent unless the node still belongs to a tree but I don't see why the nodes being removed didn't have a valid ownerTree member.

What's the best way to do this?