tree menu, checking / uncheching all child items, client side, possible?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kigoobe
    New Member
    • May 2007
    • 67

    #16
    OK, so as it stands now, here is how i'm calling this -

    [PHP]echo '<input type="checkbox" name="check_men uid[]" value="'.$row['id'].'" onclick="getChi ldren('.$row['id'].',menuArray)" />'.str_repeat(' &nbsp;ยป&nbsp ;', $level-1).$row['menu_name'];[/PHP]

    then, the function get children is (I need to pass the array as well, right?) -

    [CODE=javascript]function getChildren(id, menu) {
    var children = [];
    for (var i=0;i < menu.length; i++) {
    var parentid = menu[i];
    if (parentid == id) {
    children.push(i );
    children.concat (getChildren(i) );
    }
    }
    alert(dump(chil dren));
    return children;
    }[/CODE]
    In FF error console, I'm getting "menu has no properties" when a menu contains other level/s below, and an empty alert box when it's a menu with no child ... hmmm ...

    Comment

    • kigoobe
      New Member
      • May 2007
      • 67

      #17
      Wel, I'm on this now ...

      [CODE=javascript] function getChildren(id, menu) {
      var children = [];
      for (var i=0;i < menu.length; i++) {
      var parentid = menu[i];
      if (parentid == id) {
      /* alert (id);*/
      children.push(i );
      var obj=document.ge tElementById("v eh_"+id);
      obj.checked = (obj.checked == true) ? false : true;
      children.concat (getChildren(i, menu));
      }
      }
      /*alert(dump(chi ldren));*/
      return children;
      }[/CODE]

      this is checking / unchecking one or two checkboxes only ... hmmm ... but this is certainly working in the good direction ... :)

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #18
        If menuArray is a global array, then there's no need to pass it as an argument to the function.

        The function, as it is now, assumes that ids start from 0 and continues 1 by 1 and the largest id is equal to array length minus 1. If that isn't the case, you'll need to change how the array is processed.

        Comment

        • kigoobe
          New Member
          • May 2007
          • 67

          #19
          Thanks acoder. The id actually starts from 1. I've changed accordingly. Here is the code -

          [CODE=javascript]<script type="text/javascript">
          <!--
          var menuArray = [];

          function getChildren(id, menu) {
          var children = [];
          for (var i=1;i <= menu.length; i++) {
          var parentid = menu[i];
          if (parentid == id) {
          /* alert (id);*/
          children.push(i );
          var obj=document.ge tElementById("v eh_"+id);
          children.concat (getChildren(i, menu));
          obj.checked = (obj.checked == true) ? false : true;
          }
          }
          /*alert(dump(chi ldren));*/
          return children;
          }

          -->
          </script>[/CODE]

          Now, does it make the array global? This is still not solving the problem, I'm still not getting all the check boxes checked ... :(

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #20
            Originally posted by kigoobe
            Now, does it make the array global?
            It doesn't make the array global, but I would've thought that the array created using PHP had been declared globally.

            Comment

            • kigoobe
              New Member
              • May 2007
              • 67

              #21
              thanks acoder. yeah, you are right, I've messed up a bit here for the global declaration.

              But apart from that, it seems that I'm doing another big thing here, that I'm not taking into consideration all child menus.

              I've uploaded the page as plain html, as it's seen in the actual site, and has uploaded it in this address, so that you can see what I am getting exactly. When I click a parent element, it checks / unchecks a few of the children, at times none. Behaving bizzarely.

              Do you see what's going wrong here?
              Thanks :)

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #22
                What I suggest is call getChildren and set it to an array, then loop over that array and set each checkbox to the parent checkbox. Don't set it within getChildren.

                Comment

                • kigoobe
                  New Member
                  • May 2007
                  • 67

                  #23
                  Thanks acoder ... but I'm not getting any info by doing alert for children ... :(

                  the other code that I was having was -

                  [CODE=javascript]function getChildren(id) {
                  for (var loop=id + 1; loop<menuArray. length; loop++) {
                  if (menuArray[loop] == id) {
                  var parentObj = document.getEle mentById('veh_' + id);
                  var obj = document.getEle mentById('veh_' + loop);
                  obj.checked = parentObj.check ed;
                  }
                  }
                  } [/CODE]

                  this one works for the first step, but for multilevel menus, doesn't go beyond one deep. And in one instance, even it's not going one deep ... seems it's a pretty tough situation I'm into ... :(

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #24
                    Scrap the previous suggestion. Try something like:
                    [code=javascript]function getChildren(id) {
                    var obj = document.getEle mentById("veh_" +id);
                    for (var i=0; i < menuArray.lengt h; i++) {
                    var parentid = menuArray[i];
                    if (parentid == id) {
                    document.getEle mentById("veh_" +i).checked = obj.checked;
                    getChildren(i);
                    }
                    }
                    }[/code]

                    Comment

                    Working...