views:

47

answers:

2

Hello StackOverFlow,

I am using the Ext JS library for creating my application. I have a tree panel that has tree nodes and children tree nodes. These tree nodes have leafs that I would like to be able to access. I've been searching for a while, but cannot find a function of property to access the leafs :(

Some code is here:

var i;
var j = 0;
var selectedLayers = new Array();
                                while(layerRoot.lastChild.hasChildNodes()){
                                    alert(layerRoot.lastChild.firstChild);
for(i = 0; i < layerRoot.lastChild.firstChild.childNodes.length; i++){
                                        if(layerRoot.lastChild.firstChild.childNodes[i].isSelected()){
                                            selectedLayers[j] = layerRoot.lastChild.firstChild.childNodes[i].attributes.text;
                                            alert(selectedLayers[j]);
j++;
} 
}

                                    layerRoot.lastChild.removeChild(layerRoot.lastChild.firstChild);
}
layerRoot.removeChild(layerRoot.lastChild);

I've tried layerRoot.lastChild.firstChild.childNodes, but this doesn't work since the children of layerRoot.lastChild.firstChild are leafs :(. Your time and feedback is very appreciated.

elshae

+1  A: 

I just started playing around with TreePanel too, I think you have to write your function to traverse the tree and test if the nodes are leaves using the leaf property. If you have a particular function you want to execute on the leaves, you can use cascade() to automatically traverse the tree for you, testing for the leaf property along the way and executing your code.

Kenny Peng
I tired using the cascade function and it is either not what I want or maybe I just don't get it. Here is what I did: while(layerRoot.lastChild.hasChildNodes()){ layerRoot.lastChild.firstChild.cascade(function(){ //if(this.ui.isChecked()){ alert(this.attributes.text + "is checked"); //} }); The problem is that this.attributes.text returns the parent and this.attributes.geoserver.text returns null
elshae
The default form of `cascade` takes the node as the argument. Also, cascade automatically traverses down the tree for you (if I understand it correctly), so you don't have to loop. Your code would look something like: ` root_node.cascade(function(node) {` ` if (node.isLeaf() ` ` }` ` }`
Kenny Peng
I forgot to add that the return value of the function that you pass into `cascade` determines whether it keeps traversing down the tree.
Kenny Peng
:) Thank you! It is definitely going through each leaf now, however one little thing :-/{ alert(node.attributes.text + " is checked"); }is returning undefined, why is this?
elshae
Oh, I just kept `node.attributes.text` because that's what you had before and I thought it was a user-configured value. If you want the text in the node, you can do `node.text`.
Kenny Peng
Wow! Thank you so much Kenny! I've been at this over a day and would have never gotten it if it weren't for you. Everything is working now :)
elshae
:-/ now I have a new issue. I need to use the cascade on leafs in both cases of the leafs already checked and when they are not. I also tried eachChild, same behavior. Is there any way to force these methods to traverse the node when the leafs have not been pre-checked? Thanks!
elshae
Not sure I understand the problem here; the only reason the bit of code only executes on checked leaves is because of the `node.ui.isChecked()` requirement in the conditional statement, right?
Kenny Peng
:-/ I wish that were true but I put an alert statement before that condition and was unsuccessful.
elshae
I would think you should be able to handle all the logic in the function you specify to cascade. Just break out the `node.ui.isChecked()` out of the condition, and check only for `isLeaf()` and have the body do the checked/not-checked logic. Am I misunderstanding what you're trying to do?
Kenny Peng
layerRoot.getOwnerTree().getNodeById('tibet').firstChild.cascade(function(node) { alert(node.text); if (node.isLeaf() prints the text of firstChild.childNodes, when at least one leaf is checked. If no leafs are checked, it doesn't. You certainly understand what I am trying to do :), it's just that cascade seems to only "notice" checked leafs :-/
elshae
btw I apologize, I'm pretty new here and do not know how to highlight the code, as you have done :)
elshae
How come cascade is being called on a child as opposed to the root node? This code seems like it would only traverse down the tree from the first child of the "tibet" node. (To highlight the code, use surround the text with backticks: `)
Kenny Peng
elshae
Again all your help has been greatly appreciated ^.^
elshae
Interesting, I would assume there would be some better way to prime the tree for traversal. Glad I could help.
Kenny Peng
A: 

You should check out TreePanel, I think TreeGrid might be deprecated in the latest ext version (3.3). It has a ton of get/set/traversal functions, check it out in the API here: http://dev.sencha.com/deploy/dev/docs/?class=Ext.tree.TreePanel

bryan.taylor
I am using TreePanel ;)
elshae