Search code examples
javascriptextjsttreenodes

Check all child nodes of a Ext.tree.AsyncTreeNode


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


Solution

  • 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();
                    }
                });
    
    }