?
Solved

Javascript Recursive Tree Parse?

Posted on 2003-03-28
5
Medium Priority
?
2,679 Views
Last Modified: 2008-03-10
I have a tree like form that has layers of check boxes.  The top layer for example is Week, the next layers is weekdays and weekends, the next layers is Monday - Friday and Saturday and Sunday. Listed below.  If you Check off Week everything under it should be checked off if you uncheck Monday Weekdays and Week should be unchecked.  I get an error when evalall runs but it works correctly.  checkall gets down to Monday and generates an error.  This is all part of a dynamic tree structure so I want to make it as generic as possible.  I know that I could just hardcode but that wouldn't help me much in the grand scheme.

I am using name and id for each checkbox as follows:

+ Week (name=week id=subweek onclick="checkall(subweek);evalall(week);")
| + Weekdays (name=subweek id=weekdays onclick="checkall(weekdays);evalall(subweek);")
| | Monday (name=weekdays id=monday onclick="evalall(weekdays)")
| | Tuesday (name=weekdays id=tuesday onclick="evalall(weekdays)")
| | Wednesday (name=weekdays id=wednesday onclick="evalall(weekdays)")
| | Thursday (name=weekdays id=thursday onclick="evalall(weekdays)")
| | Friday (name=weekdays id=friday onclick="evalall(weekdays)")
| + Weekend (name=subweek id=weekend onclick="checkall(weekend);evalall(subweek);")
| | Saturday (name=weekend id=saturday onclick="evalall(weekend)")
| | Sunday (name=weekend id=sunday onclick="evalall(weekend)")

function checkAll(GroupName) {
  if (eval("document.ft."+GroupName+"==null"))
    return false;
  var checkVal=eval("document.ft."+GroupName+"[0].checked");
  for (i=1;i<eval("document.ft."+GroupName+".length");i++) {
    eval("document.ft."+GroupName+"["+i+"].checked="+checkVal);
    alert(eval("document.ft."+GroupName+"["+i+"].getAttribute('id');"));
    checkAll(eval("document.ft."+GroupName+"["+i+"].getAttribute('id');"));
  }
}

function evalAll(GroupName) {
  var GroupVal=true;
  if (eval("document.ft."+GroupName+"[0].checked")) {
    for (i=1;i<eval("document.ft."+GroupName+".length");i++) {
      if (eval("document.ft."+GroupName+"["+i+"].checked==false"))
        GroupVal=false;
    }
  } else {
    for (i=1;i<eval("document.ft."+GroupName+".length");i++) {
      if (eval("document.ft."+GroupName+"["+i+"].checked!=true"))
        GroupVal&=false;
      else
        GroupVal&=true;
    }
  }
  eval("document.ft."+GroupName+"[0].checked=GroupVal");
  evalAll(eval("document.ft."+GroupName+"[0].getAttribute('name');"));
}


Thank you,
Brian
0
Comment
Question by:bkirk007
[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
  • 4
5 Comments
 

Author Comment

by:bkirk007
ID: 8226263
The actual days of the week and weekend should have evalall not checkall function calls..

Everything else was stated correctly.
0
 

Author Comment

by:bkirk007
ID: 8226308
I went ahead and corrected the original question. What I need to find out is how to figure out if an element in the form exists before I process it.  IE. if(document.ft.monday!=exist) then stop recursion


Thank you,
Brian
0
 
LVL 7

Accepted Solution

by:
cubrovic earned 330 total points
ID: 8226510
I'm not quite sure that i have understood your code but i think it what you need is something like

eval("var pom = document.ft."+GroupName)
if (!pom) return ("Group/Object dont exist")
//Further processing of object
0
 

Author Comment

by:bkirk007
ID: 8228368
Below is the new code I came up with.  Netscape 4.08 doesn't like it but IE does. Maybe there is a better way to do this.  I create the tree form with javascript so it is very simple to build, but I just want the checkmarks to act accordingly.

<script>
function checkAll(GroupName) {
     if(eval("document.ft."+GroupName+"[0]")) {
          var checkVal=eval("document.ft."+GroupName+"[0].checked");
          for (var i=1;i<eval("document.ft."+GroupName+".length");i++) {
               eval("document.ft."+GroupName+"["+i+"].checked="+checkVal);
               checkAll(eval("document.ft."+GroupName+"["+i+"].getAttribute('id');"));
          }
     }

}

function evalAll(GroupName) {
     if (eval("document.ft."+GroupName+".length!=undefined")) {
          var GroupVal=true;
          if (eval("document.ft."+GroupName+"[0].checked")) {
               for (i=1;i<eval("document.ft."+GroupName+".length");i++) {
                    if (eval("document.ft."+GroupName+"["+i+"].checked==false"))
                         GroupVal=false;
               }
          } else {
               for (i=1;i<eval("document.ft."+GroupName+".length");i++) {
                    if (eval("document.ft."+GroupName+"["+i+"].checked!=true"))
                         GroupVal&=false;
                    else
                         GroupVal&=true;
               }
          }

          eval("document.ft."+GroupName+"[0].checked=GroupVal");
          evalAll(eval("document.ft."+GroupName+"[0].getAttribute('name');"));
     }
}
</script>

<form name=ft>
<table>
<tr><td><image src="ftv2mnode.gif"><td valign=middle><input type=checkbox name=week id=subweek onClick="checkAll('subweek');evalAll('week');"><td>Week
<tr><td><image src="ftv2blank.gif"><td><image src="ftv2mnode.gif"><td valign=middle><input type=checkbox name=subweek id=weekday onClick="checkAll('weekday');evalAll('subweek');"><td>Weekdays
<tr><td><image src="ftv2blank.gif"><td><image src="ftv2vertline.gif"><td><image src="ftv2node.gif"><td valign=middle><input type=checkbox name=weekday id=monday onClick="evalAll('weekday');"><td>Monday
<tr><td><image src="ftv2blank.gif"><td><image src="ftv2vertline.gif"><td><image src="ftv2node.gif"><td valign=middle><input type=checkbox name=weekday id=tuesday onClick="evalAll('weekday');"><td>Tuesday
<tr><td><image src="ftv2blank.gif"><td><image src="ftv2vertline.gif"><td><image src="ftv2node.gif"><td valign=middle><input type=checkbox name=weekday id=wednesday onClick="evalAll('weekday');"><td>Wednesday
<tr><td><image src="ftv2blank.gif"><td><image src="ftv2vertline.gif"><td><image src="ftv2node.gif"><td valign=middle><input type=checkbox name=weekday id=thursday onClick="evalAll('weekday');"><td>Thursday
<tr><td><image src="ftv2blank.gif"><td><image src="ftv2vertline.gif"><td><image src="ftv2node.gif"><td valign=middle><input type=checkbox name=weekday id=friday onClick="evalAll('weekday');"><td>Friday
<tr><td><image src="ftv2blank.gif"><td><image src="ftv2mnode.gif"><td valign=middle><input type=checkbox name=subweek id=weekend onClick="checkAll('weekend');evalAll('subweek');"><td>Weekend
<tr><td><image src="ftv2blank.gif"><td><image src="ftv2blank.gif"><td><image src="ftv2node.gif"><td valign=middle><input type=checkbox name=weekend id=saturday onClick="evalAll('weekend');"><td>Saturday
<tr><td><image src="ftv2blank.gif"><td><image src="ftv2blank.gif"><td><image src="ftv2node.gif"><td valign=middle><input type=checkbox name=weekend id=sunday onClick="evalAll('weekend');"><td>Sunday
</table>
</form>
0
 

Author Comment

by:bkirk007
ID: 8241817
Below is what I ended up with.  Since cubrovic had the only input I would like to thank him and award him the points.

<script type="text/javascript"> <!--
function getElementbyValue(FormName,FormValue) {
     for(var i=0; i<FormName.length; i++) {
          if(FormName[i].value == FormValue)
               return FormName[i];
     }
     alert("Value not found in form: "+FormValue+"-/->"+FormName);
     return null;
}

function checkAll(GroupName) {

     if(navigator.org=="netscape")
          cntStart=0;
     else
          cntStart=1;

     rootItem=getElementbyValue(document.ft, GroupName);
     var checkVal=rootItem.checked;
     if (eval("document.ft."+GroupName+"!=undefined")) {
          for (var i=cntStart;i<eval("document.ft."+GroupName+".length");i++) {
               eval("document.ft."+GroupName+"["+i+"].checked="+checkVal);
               checkAll(eval("document.ft."+GroupName+"["+i+"].value;"));
          }
     }
}

function evalAll(GroupName) {

     if(navigator.org=="netscape")
          cntStart=0;
     else
          cntStart=1;

     if (eval("document.ft."+GroupName+".length!=undefined")) {
          var GroupVal=true;
          if (eval("document.ft."+GroupName+"[0].checked")) {
               for (i=cntStart;i<eval("document.ft."+GroupName+".length");i++) {
                    if (eval("document.ft."+GroupName+"["+i+"].checked==false"))
                         GroupVal=false;
               }
          } else {
               for (i=cntStart;i<eval("document.ft."+GroupName+".length");i++) {
                    if (eval("document.ft."+GroupName+"["+i+"].checked!=true"))
                         GroupVal&=false;
                    else
                         GroupVal&=true;
               }
          }
          rootItem=getElementbyValue(document.ft, GroupName);
          rootItem.checked=GroupVal;
          evalAll(rootItem.name);
     }
} -->
</script>
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…
Suggested Courses

777 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