9

I would like to know how can i check the sibling nodes of a tree while clicking on a particular node in ExtJs.

I had given id's for each node and i can access the id of a clicked node. then how can i proceed to checking the child nodes automatically ??

somebody please help me..

Stefan Gehrig
  • 82,642
  • 24
  • 155
  • 189
tismon
  • 101
  • 1
  • 1
  • 3

7 Answers7

7
// or any other way of getting hands on the node you want to work with
var node = treePanel.getNodeById('your-id');
node.eachChild(function(n) {
    n.getUI().toggleCheck(true);
});

If you want this to work on the whole subtree of the current node, you'll have to do some recursion.

A little more integrated:

treePanel.on('checkchange', function(node, checked) {
    node.eachChild(function(n) {
        n.getUI().toggleCheck(checked);
    });
});
Stefan Gehrig
  • 82,642
  • 24
  • 155
  • 189
  • 3
    does not work in newer API versions. succeeded this http://stackoverflow.com/questions/6579769/automatically-check-uncheck-all-subtree-nodes-in-extjs-tree-when-certain-node-ge – pahan Oct 25 '11 at 07:19
  • That's correct. The code above is for Ext JS 3, Ext JS 4 changed the tree API quite heavily. – Stefan Gehrig Oct 25 '11 at 07:22
  • 2
    So if you don't expand the child nodes they will not load. So when you call checkchange it will only check child nodes that have been loaded. What I ended up doing was adding node.expand(true); before node.eachChild, therefore it expands all the nodes and will then check them all. – Grammin Aug 19 '13 at 15:42
  • I've the following error: "Maximum call stack size exceeded" – Cesar Miguel Oct 27 '14 at 20:04
5
function nodeCheck(node) {
    node.eachChild(function(n) {
        if(n.hasChildNodes())
            nodeCheck(n)
        n.getUI().toggleCheck(false);
    });
}
var node = (tree.getSelectionModel().getSelectedNode()) ? tree.getSelectionModel().getSelectedNode() : tree.root;
if(node) nodeCheck(node);

It works well for me ;)

slammer
  • 51
  • 1
  • 1
2

listeners:{

checkchange : function(node, checked) {
    node.parentNode.cascadeBy(function(n){n.set('checked', checked);});
}

}

damo
  • 849
  • 12
  • 16
1
function checkChange(node, checked, Object) {
    node.cascadeBy(function(n) {
        n.set('checked', checked);
    });
}
boooch
  • 11
  • 1
1

The answer of Mr C works fine(ExtJS 4.2), but a bug will occur when the childnodes of parentnode has 1 child. Here is my a little improvement. Someone can improve further

function (node, checked) {

    if (node.isLeaf()) {
        node = node.parentNode;
        var siblingStateEqual = true;
        if (node.childNodes.length == 1) {
            siblingStateEqual = checked;
        } else {
            node.cascadeBy(function (n) {
                if (n != node) {
                    if (n.get('checked') != checked) {
                        siblingStateEqual = false;
                    }
                }

            });
        }

        if (siblingStateEqual == checked) {
            node.set('checked', checked);
        }

    }
    else {
        node.cascadeBy(function (n) {
            n.set('checked', checked);
        });
    }
}
Natan Streppel
  • 5,759
  • 6
  • 35
  • 43
daniel
  • 11
  • 2
0

The JSON or XML will need the "checked" property set to true or false when you populate the nodes. I am assuming that you are using an AsyncTreeNode to do this for you. If the tree nodes are created without this checked property present, ExtJS will not render it with the checkbox.

It Grunt
  • 3,300
  • 3
  • 21
  • 35
-1

Or, if like me, you need to automatically check/uncheck the parent node when all child leaf nodes are checked/unchecked you can try this:

function (node, checked)
{

    if (node.get('leaf'))
    {
        node = node.parentNode;
        var siblingStateEqual = true;
        node.cascadeBy(function (n)
        {
            if (n != node) {
                if (n.get('checked') != checked) {
                    siblingStateEqual = false;
                }
            }

        });

        if (siblingStateEqual == checked)
        {
            node.set('checked', checked);
        }

    }
    else
    {
        node.cascadeBy(function (n) { n.set('checked', checked); });
    }


}
Fred Dump
  • 1
  • 2