javascript - uncheck parent node when children node is not all checked - Stack Overflow

I am newbie extJs user. I have using tree panel with checkbox and i want to uncheck the parent node whe

I am newbie extJs user. I have using tree panel with checkbox and i want to uncheck the parent node when the children node is not all checked. below is my code, hope you can help me guys, i dont know what to do here.

the structure of my treepanel is something like this:

  • parentnode1
    • sub-parentNode1.1
      • child1.1
      • child1.2
      • child1.3
    • subparentNode1.2
      • child2.1
      • child2.2
  • parentnode2
    • subparentNode2.1
      • child2.1.1


var treeCheck = new Ext.tree.TreePanel({
    //some code here
});

//event here

treeCheck.on('checkchange', function(node, checked) {
    if(node.hasChildNodes()==true) {

        node.eachChild(function(n) {
            n.getUI().toggleCheck(checked);
        });

    } else {

        if(!checked) {
            var _parentNode = node.parentNode;
            //i dont know what to do here...
            //specifically, i want to uncheck the parent node and subparent node
            //when the children/child node is unchecked
        }
    }
});

I am newbie extJs user. I have using tree panel with checkbox and i want to uncheck the parent node when the children node is not all checked. below is my code, hope you can help me guys, i dont know what to do here.

the structure of my treepanel is something like this:

  • parentnode1
    • sub-parentNode1.1
      • child1.1
      • child1.2
      • child1.3
    • subparentNode1.2
      • child2.1
      • child2.2
  • parentnode2
    • subparentNode2.1
      • child2.1.1


var treeCheck = new Ext.tree.TreePanel({
    //some code here
});

//event here

treeCheck.on('checkchange', function(node, checked) {
    if(node.hasChildNodes()==true) {

        node.eachChild(function(n) {
            n.getUI().toggleCheck(checked);
        });

    } else {

        if(!checked) {
            var _parentNode = node.parentNode;
            //i dont know what to do here...
            //specifically, i want to uncheck the parent node and subparent node
            //when the children/child node is unchecked
        }
    }
});
Share Improve this question edited Apr 9, 2013 at 19:14 Rob 4,94712 gold badges52 silver badges56 bronze badges asked Jul 24, 2012 at 5:50 Noel BalabaNoel Balaba 1812 silver badges12 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

Had the same issue. Fixed it by adding this event handler:

treePanel.on('checkchange', function(node, isChecked) {

    // Propagate change downwards (for all children of current node).
    var setChildrenCheckedStatus = function (current) {
        if (current.parentNode) {
            var parent = current.parentNode;
            current.set('checked', parent.get('checked'));
        }

        if (current.hasChildNodes()) {
            current.eachChild(arguments.callee);
        }
    };
    if (node.hasChildNodes()) {
        node.eachChild(setChildrenCheckedStatus);
    }

    // Propagate change upwards (if all siblings are the same, update parent).
    var updateParentCheckedStatus = function (current) {
        if (current.parentNode) {
            var parent = current.parentNode;

            var checkedCount = 0;
            parent.eachChild(function(n) {
                checkedCount += (n.get('checked') ? 1 : 0);
            });

            // Children have same value if all of them are checked or none is checked.
            var sameValue = (checkedCount == parent.childNodes.length) || (checkedCount == 0);

            if (sameValue) {
                var checkedValue = (checkedCount == parent.childNodes.length);
                parent.set('checked', checkedValue);
            } else {
                // Not all of the children are checked, so uncheck the parent.
                parent.set('checked', false);
            }

            updateParentCheckedStatus(parent);
        }
    }
    updateParentCheckedStatus(node);
});

Works both ways, downwards (check all children of a node) and upwards (uncheck parent nodes if a node is unchecked) in a recursive manner.

if(!checked)
{
    //To uncheck the child nodes (I think you didn't ask this)
    /*for(var i in node.childNodes)
        node.childNodes[i].set('checked', false);
    */
    //To unchek the parent node
    node.parentNode.set('checked', false);
    node.parentNode.parentNode.set('checked', false);
}

Note: I tested it and found that it is not recursive.

From node.parentNode.set('checked', false) only it should have unchecked the grand-parent.

Just for pleteness, for this kind of recursion, ExtJS 3.4 has respectively Ext.tree.Node methods cascade and bubble.

        listeners: {
        'checkchange': function(node, checked){

                var checkChange = function(currentNode, flag) {
                    try{
                        currentNode.attributes.checked  = flag;
                        currentNode.ui.checkbox.checked = flag;
                    }
                    catch(e){
                        // just avoiding error when node has no such attribute
                    }
                };

                /**
                 * Cascades down the tree from this node propagating the 'checkChange' event.
                 */
                 node.cascade(function() {
                    if(this.expanded === false){
                        this.expand(true);
                    }

                    checkChange(this, checked);

                    return true;
                });

                /**
                 * Bubbles up the tree from this node changing parent's state depending on children.
                 */
                node.bubble(function() {
                    if(this.hasChildNodes()){

                        var checkedCount = 0;

                        this.eachChild(function(child) {
                            checkedCount += (child.attributes.checked ? 1 : 0);
                        });

                        var checkedValue = (checkedCount == this.childNodes.length) && checkedCount !== 0;

                        checkChange(this, checkedValue);
                    }
                });
         }
     },

A pletely functional fiddle can be found here.

Obs: logic for bubble was borrowed from ag0rex

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744889501a4599317.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信