Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2007-11-15
5
Medium Priority
?
1,604 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1500 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…

604 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