2 Replies Latest reply on Dec 13, 2008 9:01 PM by ntsiii

    Getting children of checkbox

    Matt Gifford Adobe Community Professional
      Hi all.

      I've created a display list of checkboxes (chapter titles) with child select boxes underneath (slides)
      All works well.

      I have now written an onclick function for the chapter check boxes (that sends that two variables required to build the string matching the id of any slide (child) checkboxes underneath it.

      I cant seem to actually find the child checkboxes.

      Basically, I want to loop through the children and set them to either true or false, depending on the value of the chapter checkbox.

      How can I get to them by their IDs? FLEX / AS doesnt have a getElementById function, which is a damn shame. :(

      I'm building the display list using a VBox, adding HBox children that contain the checkboxes I need to update.
      This is done by looping over two arrays.

      Here's the code building the display list:

      public function createCheckBoxDisplay():void {
      var masterCount:int;
      for(masterCount=0;masterCount<masterSlideArray.length;masterCount++) {
      var chapterNumber:int = masterCount+1;
      var chapterTitle:String = masterSlideArray[masterCount][0].toString();

      // get the items and create the output for the chapter slide selection edit
      var slideCLVert:VBox = new VBox();
      slideCLVert.name = 'vertBox' + masterCount;
      var chapterTitleCheckBox:CheckBox = new CheckBox();
      chapterTitleCheckBox.id = masterCount.toString();
      chapterTitleCheckBox.addEventListener(MouseEvent.CLICK, chapterCheckBoxValue);

      // check to see if we can pre-populate the master checkbox based on the slide selections
      var slideInt:int;
      var slideCheckNumber:int = 0;
      for(slideInt=0;slideInt<masterSlideArray[masterCount][1].length;slideInt++) {

      if(masterSlideArray[masterCount][1][slideInt].showSlide == 'true') {
      slideCheckNumber = slideCheckNumber+1;
      }

      }
      // if the number of 'child' check boxes selected equals the total number of children
      // for this chapter, select the main chapter checkbox
      if (masterSlideArray[masterCount][1].length == slideCheckNumber) {
      chapterTitleCheckBox.selected = true;
      }

      var chapterTitleLabel:Label = new Label();
      var titleHBox:HBox = new HBox();

      chapterTitleLabel.text = 'Chapter ' + chapterNumber + ': ' + chapterTitle;

      titleHBox.addChild(chapterTitleCheckBox);
      titleHBox.addChild(chapterTitleLabel);

      // add the titleHBox to the VBox
      slideCLVert.addChild(titleHBox);

      // get the children (slides) from the array
      for(slideInt=0;slideInt<masterSlideArray[masterCount][1].length;slideInt++) {
      var slideNumber:int = slideInt+1;
      var slideHBox:HBox = new HBox();
      slideHBox.name = 'slideHBox' + masterCount + '_' + slideInt;

      var clCheckBox:CheckBox = new CheckBox();
      clCheckBox.id = masterCount + '_' + slideInt;
      clCheckBox.name = 'slideCheckBox' + masterCount + '_' + slideInt;
      clCheckBox.addEventListener(MouseEvent.CLICK, changeCheckBoxValue);

      if(masterSlideArray[masterCount][1][slideInt].showSlide == 'true') {
      clCheckBox.selected = true;
      }

      var clSlideName:Label = new Label();
      clSlideName.text = 'Slide ' + slideNumber;

      slideHBox.addChild(clCheckBox);
      slideHBox.addChild(clSlideName);
      slideCLVert.addChild(slideHBox);
      }
      selectionBox.addChild(slideCLVert);
      }

      var commitButton:Button = new Button();
      commitButton.label = 'Save Changes';
      commitButton.addEventListener(MouseEvent.CLICK, saveChanges);
      selectionBox.addChild(commitButton);

      }


      Here's the onclick function to try and set the values of the child check boxes:

      private function chapterCheckBoxValue(e:MouseEvent):void {
      var chapterID:String = e.currentTarget.id;
      var selectChildren:Boolean;

      if(!e.currentTarget.selected){
      //Alert.show('set as false');
      selectChildren = false;
      } else {
      //Alert.show('set as true');
      selectChildren = true;
      }

      var slideInt:int;
      for(slideInt=0;slideInt<masterSlideArray[chapterID][1].length;slideInt++) {
      // this is where I want to be able to loop through and
      //set the selected value of the child check boxes
      }
      }


      Any help would be greatly appreciated. Please. :)
        • 1. Re: Getting children of checkbox
          Level 7

          "Mattg2379" <webforumsuser@macromedia.com> wrote in message
          news:ghu3c9$16i$1@forums.macromedia.com...
          > Hi all.
          >
          > I've created a display list of checkboxes (chapter titles) with child
          > select
          > boxes underneath (slides)
          > All works well.
          >
          > I have now written an onclick function for the chapter check boxes (that
          > sends
          > that two variables required to build the string matching the id of any
          > slide
          > (child) checkboxes underneath it.
          >
          > I cant seem to actually find the child checkboxes.
          >
          > Basically, I want to loop through the children and set them to either true
          > or
          > false, depending on the value of the chapter checkbox.
          >
          > How can I get to them by their IDs? FLEX / AS doesnt have a getElementById
          > function, which is a damn shame. :(

          The id doesn't actually buy you much unless you create the component in
          mxml. Just use an array or a dictionary to store references to your
          components as you create them.

          HTH;

          Amy


          • 2. Re: Getting children of checkbox
            ntsiii Level 3
            "The id doesn't actually buy you much unless you create the component in mxml"

            This works in mxml because the mxml compiler creates instance variables with the name specified in the id attribute, containing a reference to the component instance.

            As Amy says, when instantiating component yourself, you need to keep those references yourself.

            Note, Repeater does that for you.

            Tracy