This is a migrated thread and some comments may be shown as answers.

Un check parent after all child nodes are unchecked

2 Answers 191 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
loai taha
Top achievements
Rank 1
loai taha asked on 13 Feb 2010, 05:10 PM
Hi all,

I'm trying to solve this problem where un-checking all child nodes should un-check the parent node using JavaScript.

My tree view is populated through Linq data source, with CheckBoxes property set to true. If any of the child nodes is checked, the parent node will be checked too, however, I can't solve the problem where un-checking all child nodes lead to un-checking the parent node.

I tried several solutions, yet, the best I got was un-checking any of the checked child nodes will un-check the parent node.

Appreciate your help and assistant.

Regards,

2 Answers, 1 is accepted

Sort by
0
Yana
Telerik team
answered on 17 Feb 2010, 12:27 PM
Hello,

Please find below a simple code demonstrating how you can achieve the needed functionality with RadTreeView:

<telerik:RadTreeView ID="tree1" runat="server" CheckBoxes="true" CheckChildNodes="true"
OnClientNodeChecked="nodeChecked">
    <Nodes>
        <telerik:RadTreeNode Text="root">
            <Nodes>
                <telerik:RadTreeNode Text="node 1" />
                <telerik:RadTreeNode Text="node 2" />
                <telerik:RadTreeNode Text="node 3" />
                <telerik:RadTreeNode Text="node 4" />
                <telerik:RadTreeNode Text="node 5" />
            </Nodes>
        </telerik:RadTreeNode>
    </Nodes>
</telerik:RadTreeView>
</div>
</form>
<script type="text/javascript">
 
    function nodeChecked(sender, args) {
        var node = args.get_node();
        var shouldUncheck = true;
        if (node.get_checked() == false) {
            var parent = node.get_parent();
            for (var i = 0; i < parent.get_nodes().get_count(); i++) {
                var childNode = parent.get_nodes().getNode(i);
                if (childNode.get_checked() == true) {
                    shouldUncheck = false;
                    break;
                }
            }
            if (shouldUncheck) {
                parent.uncheck();
            }
        }
    }
</script>


Regards,
Yana
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
loai taha
Top achievements
Rank 1
answered on 18 Feb 2010, 12:42 PM
Thank you Yana, it works perfectly :-)
Tags
TreeView
Asked by
loai taha
Top achievements
Rank 1
Answers by
Yana
Telerik team
loai taha
Top achievements
Rank 1
Share this question
or