Solved

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

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

770 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