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

How to select the parent node when child is selected?

1 Answer 556 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
William
Top achievements
Rank 1
William asked on 27 Jan 2009, 02:57 PM
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!!

1 Answer, 1 is accepted

Sort by
0
Yana
Telerik team
answered on 28 Jan 2009, 08:00 AM
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.
Tags
TreeView
Asked by
William
Top achievements
Rank 1
Answers by
Yana
Telerik team
Share this question
or