Solved

de-select parents when all children in tree are de-selected

Posted on 2007-11-15
5
1,529 Views
Last Modified: 2008-02-01
Hi,
I am mainly an ASP.NET developer but have to use some JavaScript for performance reasons. It would be nice if some JavaScript guru could help me with my little problem.
I have a 3rd partyTreeView:

<radT:RadTreeView
        ID="RadTree1"
        runat="server"
        OnNodeExpand=RadTree1_NodeExpand CheckBoxes=true AfterClientCheck="AfterCheck"
        >
    </radT:RadTreeView>

When the uses (de-)selects a parent node the children are (de-)selected accordingly using this JavaScript code:
<script language=javascript>

        function UpdateAllChildren(nodes, checked)
        {

            var i;
            for (i=0; i<nodes.length; i++)
            {
                 if (checked)
                 {
                    nodes[i].Check();
                 }
                 else
                 {
                    nodes[i].UnCheck();
                 }
                 
                 if (nodes[i].Nodes.length > 0)
                 {
                    UpdateAllChildren(nodes[i].Nodes, checked);
                 }
            }
       
        }
       
        function AfterCheck(node)
        {
            UpdateAllChildren(node.Nodes, node.Checked);
        }
    </script>

Would it be possible to de-select all parent nodes when all its children are de-selected?

BTW each node has a unique value.

Many thanks in advance.

BW

Chris
0
Comment
Question by:csetzkorn
  • 2
  • 2
5 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 20288953
Well, may be...   You could find out how many "checked" child nodes exist for a specific routine like:

function checkedKids( node ) {
  var result = 0;
  for ( var i = 0; i < nodes.length; i++ ) {
    result += checkedKids( node[ i ] );
  }
  return result;
}

... then, to use it, you could use something like...

if ( checkedKids( node ) == 0 ) {
  node.Uncheck();
}
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 20288961
Be careful though.  If you check the number of kids for a node which has no children, the result will be 0, and this would cause the leaf node to be unchecked.  This could propogate up the tree, if you aren't careful.
0
 
LVL 16

Expert Comment

by:gnoon
ID: 20288983
Try this

function AfterCheck(node)
{
    UpdateAllChildren(node.Nodes, node.Checked);
    if(node.Parent != null)
    {
        var anyChecked= false;
        for(var i=0; i<node.Parent.Nodes.length; i++)
        {
            if(node.Parent.Nodes[i].Checked)
            {
                anyChecked= false;
                break;
            }
        }
        node.Parent.Checked = anyChecked;
    }
}
0
 
LVL 16

Accepted Solution

by:
gnoon earned 500 total points
ID: 20289000
>node.Parent.Checked = anyChecked;

that should be

if(anyChecked) node.Parent.Check();

;-)
0
 

Author Comment

by:csetzkorn
ID: 20289492
Hi,

Thanks. Sorry but it did not work but this does:

function AfterCheck(node)
{
    UpdateAllChildren(node.Nodes, node.Checked);
   
    var hasCheckedNodes = false;
            var i;
            if(node.Parent!=null)
            {
                for(i=0;i<node.Parent.Nodes.length;i++)
                {
                    if(node.Parent.Nodes[i].Checked)
                        hasCheckedNodes = true;    
                }
                if(hasCheckedNodes == false)
                    node.Parent.UnCheck();
            }      
}
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now