6 Replies Latest reply on Sep 29, 2008 3:10 PM by miglara

    Basic Checkbox Issue

    miglara
      Hi,

      I'm new a Flex and it seems it's more complicated that it should be... but hopefully I'm wrong.
      I'm trying to create a list of 7 fruits, that include a picture, a checkbox and its label in English.
      There is a Submit button that when it's clicked, the label of the selected checkboxes changes to Spanish.
      I have this partially working but my problem is to loop through all 7 elements using a for loop to avoid long lines of code.
      I attempted doing it like I would do it in Javascript:

      for (i=1; i<8; i++){
      if ( "fruit"+i+".selected") {
      }
      }
      }

      But it's not working. I spent 2 hours surfing in the web but the solutions are related to including a datagrid or a list but it seems too difficult and they don't mention anything about handling pictures. My code is quite simple, something like the following 7 times, just changing the image source and the fruit index.

      <mx:Image x="479" y="21" source="images/banana.gif" width="50" height="50"/>
      <mx:CheckBox x="554" y="49" label="Banana" id="fruit1" textAlign="left"/>

      Your help will be greatly appreciate it!

      -Miguel
        • 1. Re: Basic Checkbox Issue
          Vanden_B Level 1
          You have to change some things, and ofcourse you can't use JavaScript like this, you have to use ActionScript.

          First, loop like this: "for (var i:int = 1; i<8; i++) {". And change the label after the if-condition by: "fruit"+i.label = "Spanish text"; (Maybe a better idea is to get this fromout a ArrayCollection...?)
          And use the debug-functionality and trace what results you get by, trace("in loop, i: "+i);
          • 2. Re: Basic Checkbox Issue
            miglara Level 1
            Thanks for your response, but it doesn't seem to be working. It seems that it doesn't like the combination of the string plus the integer variable. I have:
            var i:int;
            i=1;
            for (i=1; i<8; i++){
            if ( "fruit"+i+".selected")

            And it's showing a compilation error in the condition. I also tried:

            if ("fruit"+i.selected) but it doesn't do it either :-(

            Thanks,

            -Miguel

            • 3. Re: Basic Checkbox Issue
              Vanden_B Level 1
              the if-condition won't work indeed.

              I can't test it out right now, but i think you have to use:
              var b:Boolean = Boolean();
              var fruitId:String = String();

              in loop ... {
              fruitId = "fruit"+i;
              b = fruitId.selected;

              if (b) {
              doYourThing
              }

              Use the trace function which will lead you through this.
              • 4. Re: Basic Checkbox Issue
                istrasci
                There are several ways to do this.

                1) Create an array of CheckBoxes and access with if (fruit[n].selected)

                2) Use a repeater to create N checkboxes and access through the repeater. I'm still a beginner with repeaters, so I think it would look something like:

                <mx:Repeater id="rep" dataSource={some_array_with_N_elements}>
                <mx:CheckBox .../>
                </mx:Repeater>

                Then you'd access with something like: if (rep[n].CheckBox.selected) Actually, I'm positive that's not totally correct, so you'd have to check documentation on how to access with repeaters.

                3) Use the getChildByName() method in conjunction with building the string in the Actionscript code like you are doing. But then you'd have to set the "name" property of each checkbox in such a way that they are similar enough to each be constructed using the loop index. Something like this:

                <mx:CheckBox name="fruit1" .../>
                <mx:CheckBox name="fruit2" .../>
                ...

                Then in the Actionscript code, you'd access with: if (getChildByName("fruit"+n).selected)


                If you know how to use repeaters, then option 2 is probably the best and most concise. If not, it might be more trouble than it's worth (at least at this point) to learn how to use them, and go with option 1 or 3..
                • 5. Re: Basic Checkbox Issue
                  Level 7

                  "miglara" <webforumsuser@macromedia.com> wrote in message
                  news:gbr2u5$r00$1@forums.macromedia.com...
                  > Hi,
                  >
                  > I'm new a Flex and it seems it's more complicated that it should be... but
                  > hopefully I'm wrong.
                  > I'm trying to create a list of 7 fruits, that include a picture, a
                  > checkbox
                  > and its label in English.
                  > There is a Submit button that when it's clicked, the label of the selected
                  > checkboxes changes to Spanish.
                  > I have this partially working but my problem is to loop through all 7
                  > elements
                  > using a for loop to avoid long lines of code.
                  > I attempted doing it like I would do it in Javascript:
                  >
                  > for (i=1; i<8; i++){
                  > if ( "fruit"+i+".selected") {
                  > }
                  > }
                  > }
                  >
                  > But it's not working. I spent 2 hours surfing in the web but the solutions
                  > are
                  > related to including a datagrid or a list but it seems too difficult and
                  > they
                  > don't mention anything about handling pictures. My code is quite simple,
                  > something like the following 7 times, just changing the image source and
                  > the
                  > fruit index.
                  >
                  > <mx:Image x="479" y="21" source="images/banana.gif" width="50"
                  > height="50"/>
                  > <mx:CheckBox x="554" y="49" label="Banana" id="fruit1" textAlign="left"/>
                  >
                  > Your help will be greatly appreciate it!

                  Flex 3 Training From the Source has an example of exactly this. Actually, I
                  think they have several.


                  • 6. Re: Basic Checkbox Issue
                    miglara Level 1
                    Thanks a lot, I wasn't familiar with repeaters but it seems that that's the best way to go.

                    Thanks!

                    Miguel