Un check parent after all child nodes are unchecked

3 posts, 0 answers
  1. loai taha
    loai taha avatar
    44 posts
    Member since:
    Apr 2007

    Posted 13 Feb 2010 Link to this post

    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. Yana
    Admin
    Yana avatar
    4554 posts

    Posted 17 Feb 2010 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. loai taha
    loai taha avatar
    44 posts
    Member since:
    Apr 2007

    Posted 18 Feb 2010 Link to this post

    Thank you Yana, it works perfectly :-)
Back to Top