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

[Solved] Set Parent to checked when all children are checked

4 Answers 877 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Derrick King
Top achievements
Rank 1
Derrick King asked on 08 Mar 2010, 06:12 PM
Hi team,
I'm on version 2009.2.701.35. I need a way to set the parent node to (checked) when all children are (checked). 

 

telerik:RadTreeView Skin="Web20" runat="server" ID="radTVBranches" CheckBoxes="true"   
   
 
 
 
   
 
   
 
OnClientNodeChecked="AfterCheck">  
my ActerCheck function is written as this:   
Format Code Block(  
 
   
function  
 
   
 
AfterCheck(sender, eventArgs) (   
   
 
   
 
 
 
   
 
var childNodes = eventArgs.get_node().get_nodes(); //get all the child nodes   
   
 
 
 
   
 
   
 
var isChecked = eventArgs.get_node().get_checked(); //get all nodes that are checked   
   
 
 
 
   
 
   
 
var node = eventArgs.get_node(); // get the parent node   
   
 
 
 
   
 
   
 
   
 
// uncheck the parent if any child is unchecked.   
   
 
 
 
   
 
   
 
if (!node.get_checked()) {   
   
 
   
 
 
 
   
 
while (node.get_parent().set_checked != null) {   
   
 
node.get_parent().set_checked(  
 
 
 
   
 
false);   
   
 
nodenode = node.get_parent();  
 
}  
 
}  
 
UpdateAll(childNodes, isChecked, node);   
 
 
 
   
 
//update all nodes after check   
   
 
 
 
   
 
}  
 
)  
 

What am I missing ?
Thanks,
 DK

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 09 Mar 2010, 08:00 AM

Hello Derrik,

One suggestion would be using TriStateCheckBoxes for RadTreeView which has an Indeterminate state if it has both Checked and Unchecked CheckBoxes of child Nodes.

ASPX:

 <telerik:RadTreeView ID="RadTreeView1" runat="server" TriStateCheckBoxes="true" CheckChildNodes="true" CheckBoxes="True">  
 . . . 

Hope this will suitable for your scenario.

Shinu.

0
Derrick King
Top achievements
Rank 1
answered on 09 Mar 2010, 05:26 PM
Thanks for the quick reply Shinu. In our scenario, we are checking to see if "All" (the parent node) is checked and if so store that value only, else get all the nodes that are checked. The issue with the tristate feature is that reports the indeterminate state as "checked". I need a way to iterate through the child nodes and find out if they are all checked and if so check the parent. That is working in a previous version with the code below. What is the eqivalent for the for version 2009.2.701.35.?

function AfterCheck(node) {  
     
    if (!node.Checked && node.Parent != null)  
        node.Parent.UnCheck();  
 
    var siblingCollection = (node.Parent != null) ? node.Parent.Nodes : node.TreeView.Nodes;  
    
    var allChecked = true;  
    for (var i = 0; i < siblingCollection.length; i++) {  
        if (!siblingCollection(i).Checked) {  
            allChecked = false;  
            break;  
        }  
    }  
    if (allChecked && node.Parent != null) {  
        node.Parent.Check();  
    } 
Thanks Again,
DK   
0
Accepted
Yana
Telerik team
answered on 11 Mar 2010, 01:29 PM
Hello Derrick King,

Please subscribe to OnClientNodeChecked event and modify the code in its handler like this:

<script type="text/javascript">
    function nodeChecked(sender, args) {
        var node = args.get_node();
        var parent = node.get_parent();
 
        if (!node.get_checked() && parent != sender)
            parent.set_checked(false);
 
        var siblingCollection = parent.get_nodes();
 
        var allChecked = true;
        for (var i = 0; i < siblingCollection.get_count(); i++) {
            if (!siblingCollection.getNode(i).get_checked()) {
                allChecked = false;
                break;
            }
        }
        if (allChecked && parent != sender) {
            parent.set_checked(true);
        }    
    }
</script>


Greetings,
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
Derrick King
Top achievements
Rank 1
answered on 11 Mar 2010, 05:32 PM

Thank You Yana! That is working as I expected.
DK
Code, with comments, for those who may have a similar issue:

function AfterCheck(sender, args)   
{  
    var node = args.get_node();//get all nodes  
    var parent = node.get_parent();//get the parent node  
    var childNodes = args.get_node().get_nodes(); //get all the child nodes  
    var isChecked = args.get_node().get_checked(); //get all nodes that are checked  
 
    if (!node.get_checked() && parent != sender)  
        parent.set_checked(false);  
          
    // child node collection  
    var siblingCollection = parent.get_nodes();  
 
    var allChecked = true;  
    // get all child nodes that are checked  
    for (var i = 0; i < siblingCollection.get_count(); i++) {  
        if (!siblingCollection.getNode(i).get_checked()) {  
            //if all children are not checked allChecked is false  
            allChecked = false;  
            break;  
        }  
    }  
    // check to see if the parent was not check and all children are checked  
    // if so check the parent node   
    if (allChecked && parent != sender) {  
        parent.set_checked(true);  
    }  
    UpdateAll(childNodes, isChecked); //update all nodes after check  
}  
 
 
function UpdateAll(childNodes, isChecked) {  
    for (var i = 0; i < childNodes.get_count(); i++) {  
        if (isChecked) {  
            //if the parent node is checked then  
            childNodes.getNode(i).check();  
        }  
        else {  
            //clear the child node at position (i) that was unchecked  
            childNodes.getNode(i).set_checked(false);  
        }  
        //if the current node has children  
        if (childNodes.getNode(i).get_nodes().get_count() > 0) {  
            //look for any nodes that have changed and update them  
            UpdateAll(nodes.getNode(i).get_nodes(), isChecked);  
        }  
    }  
Tags
TreeView
Asked by
Derrick King
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Derrick King
Top achievements
Rank 1
Yana
Telerik team
Share this question
or