PDA

View Full Version : Change a Tree Node's Actual Id



cornyflake
8 Jan 2007, 2:20 PM
I know how to change a tree node's id in its attributes, but how do I change it's actual id?

Could I do:

nodeCopy = node.parent.childNodes[node.id];
node.parent.childNodes[node.id] = null;
node.parent.childNodes['newId'] = nodeCopy;

Would something like this screw up something? Is there an better/easier way?

jack.slocum
8 Jan 2007, 5:33 PM
Parents don't keep a reference to child nodes by id. childNodes (like DOM) is an array. The collection you are looking to update in on the tree itself. I don't know what would happen if you ran that code.

Why do you need to change the id? What are you trying to do?

cornyflake
9 Jan 2007, 10:16 AM
I'm creating a cms that uses your tree for the managed website's "menu items". When I generate the tree, the ids of the nodes are the ids of the menu items. This makes it easy to update nodes when the menu items are updated.

When a user wants to create a new menu item, a node is added to the tree (just like your image manager example in your last blog post). Of course, the id of the node does not correspond to that of the menu item because I haven't inserted it into the database yet.

Once I insert it into the database and find out the id, I would like to update the node's id to that of the menu item. I can easily update the "attributes" id, but methods like "getNodeById" depend on the actual id (key) found in "childNodes". I was wondering if I changed the key in childNodes, if that would screw up anything that is depending on that.

A hypothetical situation would be if, when a node is clicked, the attribute "this.currentlySelectedNode" was assigned to the key of that node. Then "isSelected" would compare the given id to that of "this.currentlySelectedNode". If I were to change the actual id of a node after it was selected, "isSelected" would return false because "this.currentlySelectedNode" has the _old_ id.

I hope this makes sense.
I don't know if you have any variables that get assigned to a node's key.

gbulfon
4 May 2009, 1:18 AM
Hi, I'm having the same problem.

My tree reflects an Imap tree dynamically.
Every node has its ID = full-path-name of the imap folder.
Now I need to let the user rename an imap folder, everything goes fine server side, and the ajax call returning need to refelcts this change on the client, without having it to reload folders from the imap servlet. I do this way in the ajax callback ('o' is the object holding servlet javascript data, that is oldid=oldfullpath,newid?newfullpath and newname=shortname):

var t=this.imapTree;
var n=t.getNodeById(o.oldid);
t.unregisterNode(n);
n.id=o.newid;
n.setText(o.newname);
t.registerNode(n);

This seems to run fine, but then I find some problem on events handling: just hovering the new node causes javascript errors in the Ext.tree.TreeEventModel (look the comment at the end of the code):

delegateOver : function(e, t) {
if(!this.beforeEvent(e)){
return;
}
if(this.lastEcOver){ // prevent hung highlight
this.onIconOut(e, this.lastEcOver);
delete this.lastEcOver;
}
if(e.getTarget('.x-tree-ec-icon', 1)){
this.lastEcOver = this.getNode(e);
this.onIconOver(e, this.lastEcOver);
}
if(t = this.getNodeTarget(e)){
//INSIDE THIS CALL I GET THE ERROR
//BECAUSE this.getNode(e) RETURNS A NULL NODE
this.onNodeOver(e, this.getNode(e));
}
},


Should I do something else to change internal event ids? Maybe the DOM ids are still old?
Thx for any help.
Gabriele.