PDA

View Full Version : Check/Uncheck parent and children nodes



springrider
21 Oct 2010, 5:15 AM
Good morning

I have a treenode with a node (A) with 3 children(1,2,3)
A
|_1
|_2
|_3

I would like when i check/uncheck the parent A that all the children are checked/unchecked.
If all the nodes are unchecked initially, I would like to check A when I check at least one of the children.
I wrote the folowing code:

,listeners: {checkchange: {fn: function(node, checked){
node.eachChild(function(n) {
n.getUI().toggleCheck(checked);
});
node.parentNode.getUI().toggleCheck(checked);

Each behaviour is working fine individually but not when they are both combined.
In fact,now, if all my nodes were unchecked (both parent and children) and I check one of the children, the current code checks the parent and all the children....

Hope it is clear enough.
Anyone having an idea.

Thanks in advance

Sylvain

Animal
21 Oct 2010, 5:22 AM
Looks like you are checking the parent node. If that has the same listener configured in, that will cascade down won't it?

If that's the case, and the parent node has a similar listener which is being triggered, then maybe it should be this:



,listeners: {checkchange: {fn: function(node, checked){
node.eachChild(function(n) {
n.getUI().toggleCheck(checked);
});
p = node.parentNode;
p.suspendEvents(); // Parent node now just changes state without firing an event.
p.getUI().toggleCheck(checked);
p.resumeEvents();

springrider
21 Oct 2010, 2:14 PM
Thanks Condor

The behaviour you describe is effectively what is happening, mixing cascading and bubbling.
I tried your code but i still have some recursion issues and moreover, i can not suspend the event which should be fired for doing other process (in red):
listeners:

{checkchange: {fn: function(node, checked){
node.attributes.kmlObj.setVisibility(checked);

node.bubble(function(n){ // If a child node is checked, check all the ancestors
if (node.getUI().isChecked()) {
if (!node.parentNode.getUI().isChecked()) {
node.parentNode.getUI().toggleCheck();
}
}
})
}}}

I tried this new code which works nicely when i click a child, the parent is checked and its visibility is set to true but i am still stuck with checking/unchecking children when parent is checked/unchecked.

Is there a node.isClicked properties? It would help me determining which node has been clicked and adapt the event accordingly.

Thanks for your help

Sylvain