Solved

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

Posted on 2007-11-15
5
1,551 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

821 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