PDA

View Full Version : TreeNode Problem. (id: this.title doesn't work)



Developer1729
5 Oct 2009, 4:49 AM
So, here is code and it doesn't work.


root: new Ext.tree.AsyncTreeNode({
expanded: true,
children: [{
text: 'folder1',
leaf: false,
children: [{
text: 'leaf1',
id: this.parentNode.text,
leaf: true,
}, {
text: 'leaf2',
leaf: true
}, {
text: 'leaf3',
leaf: true
}]
}, {
text: 'Menu Option 2',
leaf: true
}, {
text: 'Menu Option 3',
leaf: true
}]
}),Actually, what i need is building an id of the node in the way like this:
id = this.getPath()
it means the id must be equal to the path to the node.

Any help will be highly appreciated.

aconran
5 Oct 2009, 4:57 AM
What is the value of this when you are defining this new TreeNode?

I would suggest using individual id's and then programmatically calling getPath() when you need to determine the full path. This will provide you the flexibility of being able to retrieve by id or path.

If you want to go the other router, the easiest way to auto-tag the id's with the path would be to implement a custom TreeLoader and override createNode.

Condor
5 Oct 2009, 4:58 AM
'this' is executed when you create the root node, so it will be whatever the scope was at that time ('window'?).

ps. An id can never be the same as the result from getPath(), because getPath() uses the ids to build the path!

Developer1729
5 Oct 2009, 5:17 AM
Yes, Condor, you're right. Here is what i need:
id = this.parentNode.getPath() + this.text

So, if this at the root node creating time is something like 'window' or 'Treepanel' how can i use node intern data for building id or at least parent nodes intern data?

Condor
5 Oct 2009, 5:43 AM
Do you really want that?

{
id: 'a',
children: [{
id: 'a/b',
children: [{
id: 'a/b/c'
}
}
}
The path of the inner node would be '/a/a/b/a/b/c'!

And to answer your question:
The id gets assigned to the node before it is added to it's parent, so it can't be calculated by the node itself.
You will have to calculate ids yourself before the nodes are created.

Developer1729
5 Oct 2009, 5:50 AM
Yeah, something like this or better like this:


{
id: 'a',
children: [{
id: 'ab',
children: [{
id: 'abc'
}
}
}

with the final path like a/ab/abc

but i want to assign it automatically.

Condor
5 Oct 2009, 6:39 AM
You could override the doPreload method of the loader, e.g.

loader: new Ext.tree.TreeLoader({
...
doPreload : function(node){
var cs = node.attributes.children;
if(Ext.isArray(cs)){
for(var i = 0, len = cs.length; i < len; i++){
cs[i].id = node.id + '-' + cs[i].text;
}
}
this.constructor.prototype.doPreload.call(this, node);
}
}),
(the root node would still need an id)