11 Replies Latest reply on May 18, 2009 1:06 PM by rcanulla

    Controlling properties on items added to the stage in a loop

    rcanulla Level 1

      Hi there,

       

      I have a loop that adds xml elements to the stage (10). Once added, I need to control the items (checkbox = do something).

       

      Here is where I add them to the stage.

                private function createLayout():void {
                     container = new VBox();
                     
                     for(var i:int=0; i<10; i++) {
                          vBox = new VBox();
                          hBox = new HBox();
                          titleText = new LinkButton();
                          itemInfo = new Text();
                          abstract = new Text();
                          archive = new CheckBox();
                          rateItem = new ComboBox();
                          category = new ComboBox();
                          var categoryLabels:Array = new Array("Food & Ag","News","Health","People","General","What's Coming","Biofuel","Environment");
                          var rateLabels:Array = new Array("Excellent","Good","Bad","Neutral","Controversial");
      
                          titleText.label = listCollection.getItemAt(i).title;
                          titleText.width = 400;
                          itemInfo.text = listCollection.getItemAt(i).source + " | " + listCollection.getItemAt(i).date;
                          abstract.text = listCollection.getItemAt(i).abstract;
                          abstract.width = 400;
                          archive.label = "Archive";
                          
                          category.prompt = "Category";
                          category.dataProvider = categoryLabels;
                          category.rowCount = categoryLabels.length;
      
                          rateItem.prompt = "Rate";
                          rateItem.dataProvider = rateLabels;
                          
                          vBox.addChild(titleText);
                          vBox.addChild(itemInfo);
                          vBox.addChild(abstract);
                          vBox.addChild(hBox);
                          
                          hBox.addChild(archive);     
                          hBox.percentWidth = 80;
                          hBox.percentHeight = 80;
                          archive.addEventListener(Event.CHANGE, toggleArchive);                    
                          
                          container.addChild(vBox);
                     }
                     
                     addChild(container);
      
                }
      

       

      I listen for a checkbox (change event) and when clicked, call a function that adds two comboboxes to the stage.

       

      archive.addEventListener(Event.CHANGE, toggleArchive);     

       

      I then call the function that will handle the event.

       

                public function toggleArchive(e:Event):void {
                     if(archive.selected == true){
                          hBox.addChild(category);
                          hBox.addChild(rateItem);                    
                     }
                     else {
                          hBox.removeChild(category);
                          hBox.removeChild(rateItem);
                     }
                }
      

       

       

       

      When I have one item it works fine.

      for(var i:int=0; i<1; i++) {

       

      When I add 10 nothing happens. I feel like i should be doing something to identify what item I'm referring to. Any thoughts/reccomendations would br great!

        • 1. Re: Controlling properties on items added to the stage in a loop
          Gregory Lafrance Level 6

          Give your category and rateItem objects names, like category.name = "category";

           

          then don't add remove based on ComboBox, but set these properties:

           

          var cb:ComboBox = myTopVBox.getChildAt(i).getChildByName(category);

          cb.visible = false;

          cb.includeInLayout = false;

          1 person found this helpful
          • 2. Re: Controlling properties on items added to the stage in a loop
            rcanulla Level 1

            My apologies, but I am somewhat confused. Thanks for taking the time to help me (first off)!

             

            I changed the createLayout function to add name, visible, and includeInLayout propertiesfor each comboBox. I then changes the scope of "i" so that I can reference it from the function called upon the Event.CHANGE event.

             

                      private var i:Number;
            

             

                      private function createLayout():void {
                           container = new VBox();
                           
                           for(var i=0; i<listCollection.length; i++) {
                                vBox = new VBox();
                                hBox = new HBox();
                                titleText = new LinkButton();
                                itemInfo = new Text();
                                abstract = new Text();
                                archive = new CheckBox();
                                rateItem = new ComboBox();
                                category = new ComboBox();
                                var categoryLabels:Array = new Array("Food & Ag","News","Health","People","General","What's Coming","Biofuel","Environment");
                                var rateLabels:Array = new Array("Excellent","Good","Bad","Neutral","Controversial");
            
                                titleText.label = listCollection.getItemAt(i).title;
                                titleText.width = 400;
                                itemInfo.text = listCollection.getItemAt(i).source + " | " + listCollection.getItemAt(i).date;
                                abstract.text = listCollection.getItemAt(i).abstract;
                                abstract.width = 400;
                                archive.label = "Archive";
                                
                                category.prompt = "Category";
                                category.name = "category";
                                category.dataProvider = categoryLabels;
                                category.rowCount = categoryLabels.length;
                                category.visible = false;
                                category.includeInLayout = false;
            
                                rateItem.prompt = "Rate";
                                rateItem.name = "rateItem";
                                rateItem.dataProvider = rateLabels;
                                rateItem.visible = false;
                                rateItem.includeInLayout = false;
                                
                                vBox.addChild(titleText);
                                vBox.addChild(itemInfo);
                                vBox.addChild(abstract);
                                vBox.addChild(hBox);
                                
                                hBox.addChild(archive);     
                                hBox.addChild(category);
                                hBox.addChild(rateItem);
                                
                                hBox.percentWidth = 80;
                                hBox.percentHeight = 80;
                                archive.addEventListener(Event.CHANGE, toggleArchive);                    
                                
                                container.addChild(vBox);
                           }
                           
                           addChild(container);
            
                      }
            

             

            now I have to modify these items by name from the toggleArchive method.

                      public function toggleArchive(e:Event):void {
                           if(archive.selected == true){
                                var cb:ComboBox = hBox.getChildAt(i).getChildByName(category);
                                cb.visible = true;
                                cb.includeInLayout = true;
                           }
                           else {
            
            
                           }
            

            Two things, 1) i get an error saying...

             

             

            Severity and Description    Path    Resource    Location    Creation Time    Id
            1061: Call to a possibly undefined method getChildByName through a reference with static type flash.display:DisplayObject.    mediaTrap/src/components    listItems.mxml    line 132    1242667335601    643

             

            2) shouldnt it be something like this

                      public function toggleArchive(e:Event):void {
                           if(hBox.getChildAt(i).archive.selected == true){
                                var cb:ComboBox = hBox.getChildAt(i).getChildByName(category);
                                cb.visible = true;
                                cb.includeInLayout = true;
                           }
                           else {
            
            
                           
            
            • 3. Re: Controlling properties on items added to the stage in a loop
              Gregory Lafrance Level 6

              Yeah. I was generalizing, but seems you figured it out in your app.

              • 4. Re: Controlling properties on items added to the stage in a loop
                rcanulla Level 1

                do you have any thoughs regarding the error? I actually have no idea about this. Sorry!

                 

                error

                Severity and Description    Path    Resource    Location    Creation Time    Id
                1119: Access of possibly undefined property selected through a reference with static type flash.display:DisplayObject.    mediaTrap/src/components    listItems.mxml    line 133    1242669734737    661

                 

                Severity and Description    Path    Resource    Location    Creation Time    Id
                1119: Access of possibly undefined property includeInLayout through a reference with static type flash.display:DisplayObject.    mediaTrap/src/components    listItems.mxml    line 135    1242669873712    665

                Why do you use getChildAt & getChildByName? Why can't I just do something like this?

                          public function toggleArchive(e:Event):void {
                               if(archive.getChildAt(i).selected == true){
                                    rateItem.getChildAt(i).visible = true;
                                    rateItem.getChildAt(i).includeInLayout = true;
                               }
                               else {
                
                
                               }
                          }
                
                
                • 5. Re: Controlling properties on items added to the stage in a loop
                  Gregory Lafrance Level 6
                  ComboBox(archive).getChildAt(i).selected == true
                  • 6. Re: Controlling properties on items added to the stage in a loop
                    rcanulla Level 1

                    Can you please be more descriptive? I have no idea what that means.

                     

                    I'm sorry because I feel annoying. Let me know if I am, and I'll stop bugging you.

                    • 7. Re: Controlling properties on items added to the stage in a loop
                      Gregory Lafrance Level 6

                      You're not annoying, just curious.

                       

                      It looks like Flex in this case is treating archive as a generic DisplayObject, so you need to cast it back to its real datatype (ComboBox).

                       

                      So:

                       

                      public function toggleArchive(e:Event):void {
                          if(((ComboBox)archive.getChildAt(i)).selected == true){
                              rateItem.getChildAt(i).visible = true;
                              rateItem.getChildAt(i).includeInLayout = true;
                          }
                      }
                      
                      • 8. Re: Controlling properties on items added to the stage in a loop
                        rcanulla Level 1

                        Phew. I am curious, very curious.

                         

                        I did as stated before, and understand the need for casting the object so that flash knows how to handle it, but am getting differint errors now.

                         

                                  public function toggleArchive(e:Event):void {
                                      if(((ComboBox)archive.getChildAt(i)).selected == true){
                                          rateItem.getChildAt(i).visible = true;
                                          rateItem.getChildAt(i).includeInLayout = true;
                                      }
                                  
                                  //     else {
                                  //          trace("test");
                                  //     }
                                  }
                        

                        getting these errors. all on the line below.

                        if(((ComboBox)archive.getChildAt(i)).selected == true){

                         

                        error.

                        Severity and Description    Path    Resource    Location    Creation Time    Id
                        1008: Attribute is invalid.    mediaTrap/src/components    listItems.mxml    line 133    1242672962051    701

                         

                        Severity and Description    Path    Resource    Location    Creation Time    Id
                        1084: Syntax error: expecting identifier before dot.    mediaTrap/src/components    listItems.mxml    line 133    1242672962050    700

                         

                        Severity and Description    Path    Resource    Location    Creation Time    Id
                        1084: Syntax error: expecting rightparen before archive.    mediaTrap/src/components    listItems.mxml    line 133    1242672962050    699

                        • 9. Re: Controlling properties on items added to the stage in a loop
                          Gregory Lafrance Level 6

                          You misplaced your parenthesis:

                           

                          if(((ComboBox)archive.getChildAt(i)).selected == true){
                          • 10. Re: Controlling properties on items added to the stage in a loop
                            rcanulla Level 1

                            i feel like I'm getting it! Still not working, but I think I'm understanding...

                             

                            So the issue is that flash doesn't see the CheckBox() archive as a CheckBox and rateItem as a ComboBox. I'm still getting errors that flash doesn't recognize the property includeInLayout & selected. I DO NOT get and error for visible? Thoughts?

                             

                                      public function toggleArchive(e:Event):void {
                            line 133-->         if((CheckBox(archive).getChildAt(i)).selected == true){
                                              (ComboBox(rateItem).getChildAt(i)).visible = true;
                            line 135-->             (ComboBox(rateItem).getChildAt(i)).includeInLayout = true;
                                          }
                                      
                                           else {
                                                trace("test");
                                           }
                                      }
                            

                             

                             

                            Severity and Description    Path    Resource    Location    Creation Time    Id
                            1119: Access of possibly undefined property includeInLayout through a reference with static type flash.display:DisplayObject.    mediaTrap/src/components    listItems.mxml    line 135    1242674897089    725

                             

                            Severity and Description    Path    Resource    Location    Creation Time    Id
                            1119: Access of possibly undefined property selected through a reference with static type flash.display:DisplayObject.    mediaTrap/src/components    listItems.mxml    line 133    1242674897088    724

                            • 11. Re: Controlling properties on items added to the stage in a loop
                              rcanulla Level 1

                              also, I should note, it looks like i am getting the problem when using getChildAt() method. I can access the item fine when only instantiating once.

                               


                              Should I listen for which one is clicked and pass that info?