5 Replies Latest reply on Apr 1, 2009 9:47 AM by Newsgroup_User

    dynamic binding expression to get data from array

    lunacafu
      I am retrieveing from an object to label my button as follows:

      quote:

      <mx:Button label = "{wordlist.word.LABEL[0][index]}"


      I now want the binding expression to be dynamic, meaning that LABEL has to be a variable. How would I do this?

      quote:

      <mx:Button label = "{wordlist.word.????[LABEL]????[0][index]}"
        • 1. Re: dynamic binding expression to get data from array
          Peter Hahmann Level 1
          I'm not sure if I understood what you need but I use the following logic to have dynamic labels:

          private var labelArray:ArrayCollection;

          private function getLabel(id:int):String{
          var label:String = "";
          for each(var obj:Object in labelArray)
          {
          if(obj.id = id)
          {
          label = obj.label;
          }
          }
          return label;
          }



          <mx:Button label="{getLabel(45)}"/>


          Regards,
          Peter
          • 2. Re: dynamic binding expression to get data from array
            Level 7

            "lunacafu" <webforumsuser@macromedia.com> wrote in message
            news:gqte13$1g4$1@forums.macromedia.com...
            >I am retrieveing from an object to label my button as follows:
            >
            >
            quote:

            <mx:Button label = "{wordlist.word.LABEL[0][index]}"

            >
            > I now want the binding expression to be dynamic, meaning that LABEL has to
            > be
            > a variable. How would I do this?
            >
            >
            quote:

            <mx:Button label = "{wordlist.word.????[LABEL]????[0][index]}"


            First, if wordlist.word.LABEL is an XMLListCollection or ArrayCollection,
            you need to use getItemAt instead of bracket notation. Once you get to that
            point, you need to cast it to whatever data type the object at 0 is.

            HTH;

            Amy


            • 3. Re: dynamic binding expression to get data from array
              archemedia Level 4
              I don't know if I undertstand your question but I think you want to use an element of an array as the source for data binding. If this is so, you should use the getItemAt method of the array class instead of using square brackets.
              Say you want to bind the first element of an array to the label of a button:

              WRONG:
              <mx:Button label = "{myArray[0]}"

              CORRECT:
              <mx:Button label = "{myArray.getItemAt(0)}"

              HTH
              • 4. dynamic binding expression to get data from array
                archemedia Level 4
                I misinterpreted your message. You want to use a variable as a propname of an object right?
                See the code for a small example

                Although this example will work, it is against the rule to use square brackets in binding expressions so Flex Builder will issue a warning.

                As a solution, you could write a class which implements a method 'getPropertyByName' to retrieve its value.

                HTH
                • 5. Re: dynamic binding expression to get data from array
                  Level 7

                  "archemedia" <webforumsuser@macromedia.com> wrote in message
                  news:gr020h$am5$1@forums.macromedia.com...
                  >I don't know if I undertstand your question but I think you want to use an
                  > element of an array as the source for data binding. If this is so, you
                  > should
                  > use the getItemAt method of the array class instead of using square
                  > brackets.
                  > Say you want to bind the first element of an array to the label of a
                  > button:
                  >
                  > WRONG:
                  > <mx:Button label = "{myArray[0]}"
                  >
                  > CORRECT:
                  > <mx:Button label = "{myArray.getItemAt(0)}"

                  Array does not have a getItemAt method.