How to select the parent node when child is selected?

2 posts, 0 answers
  1. William
    William avatar
    3 posts
    Member since:
    Oct 2008

    Posted 27 Jan 2009 Link to this post

    In my treeview, I have CheckCildNodes="true", which is working correctly.  When a parent node is checked, it checks all child nodes.  What I also need, is the ability for checking the parent node when a single child node is checked.  As an example, say there are 20 child nodes under a parent.  If the user only wants to select a subset of the children, I also need the parent of those children to be checked, but I don't want all of the children to be checked.  Having the user check the parent and then uncheck all children that they don't want isn't really an option.

    Is there an "out-of-the-box" setting for this?  If not, and it has to be done via JavaScript, would someone please post some example code?

    Thanks for advance for you help!!  I appreciate it!!
  2. Yana
    Admin
    Yana avatar
    4795 posts

    Posted 28 Jan 2009 Link to this post

    Hi William,

    I already answered in the support ticket, but for your convenience I'm pasting the reply:

    I suggest you subscribe to OnClientNodeChecked event and use the following code in its handler:

    function nodeChecked(sender, args)  
    {  
        var node = args.get_node();  
        var parent = node.get_parent();  
        if(parent != sender)  
        {  
            if(node.get_checked() == true)  
            {  
                sender._checkChildNodes = false;  
                parent.set_checked(true);  
                sender._checkChildNodes = true;  
            }  
            else 
            {  
                var isChildChecked = false;  
                for(var i=0;i<parent.get_nodes().get_count();i++)  
                {  
                    if(parent.get_nodes().getNode(i).get_checked())  
                        isChildChecked = true;  
                }  
                  
                if(!isChildChecked)  
                    parent.set_checked(false);  
            }  
        }  

    Note that CheckChildNodes property should stay set to "true".

    Best regards,
    Yana
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Back to Top