PDA

View Full Version : Tree with non-unique IDs and getNodeById()?



digeomel
24 Aug 2007, 1:54 AM
Hi,

I have a situation where the user may append what appear to be the same nodes at different locations in the same TreePanel. I use the term "appear to be" because of course, the node ids can be different, but the problem is that ideally the IDs should be the same, because they are IDs taken from the database.

So, first of all, can this be done? Is it ok to have nodes with the same attributes.id values in the same TreePanel? And what will getNodeById() return then? Does it return an array of all the nodes found?

Or should I just make sure that my IDs are unique anyhow?

Thanks in advance.

Animal
24 Aug 2007, 2:28 AM
Make sure they're unique.

To generate an id: http://extjs.com/deploy/ext/docs/output/Ext.html#id

digeomel
24 Aug 2007, 3:50 AM
Ok, so then I can add a custom attribute to the node attributes object to reflect my database id, but then the getNodeById() function will be useless, I will have to create my own function for getting node(s) with a specific attribute value, right?

Animal
24 Aug 2007, 4:30 AM
Yes. It might return an Array if there was >1 node with that attribute value.

digeomel
24 Aug 2007, 5:45 AM
Yes. It might return an Array if there was >1 node with that attribute value.

Thanks Animal.
I will run some tests and see what happens before I go on to make it even more complex than it already is.

digeomel
27 Aug 2007, 4:46 AM
Ok, I have replaced the id attribute by my own attribute, db_id. And of course, in the process I needed a function similar to getNodeById(). My solution is this:



Ext.apply(Ext.data.Tree.prototype, {
getNodeByAttribute: function(attrib, value)
{
var root = this.getRootNode();
var nodeFound = null;
root.cascade(function fnCheckAttrib() {
if(this.attributes[attrib] && this.attributes[attrib] == value)
{
nodeFound = this;
return false;
}
});
return nodeFound;
}
});


This will allow you to get a node based on any attribute (as long as attributes are simple values, not objects). Thought it might help someone else as well.

evant
27 Aug 2007, 5:23 AM
Just FYI:

http://extjs.com/deploy/ext/docs/output/Ext.tree.TreeNode.html#findChild

digeomel
27 Aug 2007, 5:54 AM
Just FYI:

http://extjs.com/deploy/ext/docs/output/Ext.tree.TreeNode.html#findChild

Hahaha :)) I must be blind! :-/

Maybe it's because I expected to find this in the TreePanel and not the TreeNode.

Thanks!

digeomel
27 Aug 2007, 5:59 AM
On second thought, and after inspecting the code for findChild, it appears not to do what I want.

findChild only examines the immediate children of a node, while I want to search on the whole TreePanel.

So, I think my code is still valid. ;)