check child nodes of a tree when a parent is clicked [ExtJS]

我的未来我决定 提交于 2019-12-21 10:04:35

问题


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..


回答1:


// 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);
    });
});



回答2:


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 ;)




回答3:


listeners:{

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

}




回答4:


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



回答5:


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



回答6:


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.




回答7:


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


}


来源:https://stackoverflow.com/questions/2271177/check-child-nodes-of-a-tree-when-a-parent-is-clicked-extjs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!