Check all child nodes of a Ext.tree.AsyncTreeNode

1.4k views Asked by At

I am trying to check all child nodes of a node if this is checked.

Added a listener

   tree.addListener('expandnode', this.onTreeNodeExpand, this);

Function

onTreeNodeExpand: function (node) {
       if (node.hasChildNodes()) {
            node.eachChild(function (n) {
                    var checked = n.parentNode.ui.isChecked();
                    if (checked != undefined && checked)
                    {
                            n.getUI().toggleCheck(checked);
                    }
            });
        }
    }

Since I have Async tree node, all child records will be loaded on demand and first time when the node is expanded I get false for this statement

if (node.hasChildNodes()) // returns false

It think "expandnode" event is called same time while Async is in progress.

How can I capture a event where after async data is loaded, check if this node is checked and set all child nodes to checked?

Environment: EXTJS 3.4

2

There are 2 answers

0
HaBo On BEST ANSWER

Since, I didn't get much help on EXTJS doc for this scenario, I have handled it as below.

All my Async calls for child records will go through a function, in that ajax success function once the data is loaded, I called another function to set the check box based on parent node.

 ajaxForLoading: function (node, deep, anim, isDirectList, childrenConfig) {
       Ext.Ajax.request({
                    url: url,
                    params: param,
                    method: 'POST',
                    success: function (response) {
                       this.loadRelationNodeChildren(node, json, deep, anim, childrenConfig);
                    }

 },
loadRelationNodeChildren: function (node, json, deep, anim, childrenConfig){
    // My other logic
    expandChildNodes(node, this);
 },

expandChildNodes: function(node, scope){
 var checked = node.ui.isChecked();
            node.eachChild(function (n) {
                if (checked != undefined) {
                    var tree = n.ownerTree;
                    if (n.isLeaf())
                        tree.suspendEvents();
                        n.getUI().toggleCheck(checked);
                    if (n.isLeaf())
                        tree.resumeEvents();
                }
            });

}
0
无我仙人 On

I use 'beforechildrenrendered', 'expand', 'checkchange' events to toggle check node with a async tree.

root: {nodeType: 'async',text: 'root', id:'-1'},
dataUrl: url,
listeners: {
    scope: this,
    'beforechildrenrendered': function(node) {
        node.eachChild(function(childNode) {
            childNode.on('checkchange', function(node, checked) {
                childNode.suspendEvents();//need suspend events first
                childNode.cascade(function(n) {
                    n.attributes.checked = checked;
                    n.getUI().toggleCheck(checked);
                });
                childNode.resumeEvents();//resume events

                if(!checked) {
                    return;
                }

                if(!node.isLeaf()) {
                    node.expand();
                }
            });
        },this);

        node.on('expand', function(node) {
            if(node.id == '-1') {//root
                return;
            }

            if(node.parentNode && node.parentNode.attributes.checked) {
                node.attributes.checked = true;
                node.getUI().toggleCheck(true);
            }

            if(node.attributes.checked) {
                for(var i = 0; i < node.childNodes.length; i++) {
                    node.childNodes[i].expand();
                }
            }
        });
    }
}