2 Replies Latest reply on Dec 13, 2006 6:08 AM by Ukie_239

    Populating a form with data from Array

    Ukie_239 Level 1
      I have results from a query getting passed back as an array object. Is there an easy to assigned to values of the array to objects within a form container. Currently, i am doing mytext_txt.text = $myArray[0]["text1"]

      If there are like 40 objects in a form, its seem kinds of repetitive

      Any ideas would be awesome. How would using a model help in this case? Can you bing results to a model?

      Thanks
        • 1. Re: Populating a form with data from Array
          FlightGuy Level 1
          I use XML and receive it into an Object - pretty convenient because you get proper properties rather than just positions.

          I'm not sure I exactly understand your example - do you have 'records' as elements in the array, and each 'record' is an object with named properties? or do you have an array in which each position relates to a single property.

          Assuming the latter, a possible way to do this is to create a .as class file with all the properties you want in the same order as you have them in the array. Define a constructor that accepts your array as input. Then loop through the properties and assign them from your array:

          function MyModelClass(myArray:Array){
          var i:uint = 0;
          for (var property:String in this)
          this[property] = myArray[i++];
          }

          I haven't tried this, but should work.

          If the array is an array of objects, just do this:
          [Bindable]
          var myObject:Object;

          myObject = $myArray[0];

          Then in your mxml,
          <mx:Text id="mytext_txt" text="{myObject.text1}"/>

          Another option is to write some script that follows the container hierarchy, and for each control in the hierarchy, inspect the id (or name if you prefer), and check
          if ($myArray[0].hasOwnProperty(whatever.id))
          (whatever as Text).text = $myArray[0][whatever.id];

          hope something there makes sense.
          • 2. Re: Populating a form with data from Array
            Ukie_239 Level 1
            AWESOME!

            [Bindable]
            var myObject:Object;

            myObject = $myArray[0];

            Then in your mxml,
            <mx:Text id="mytext_txt" text="{myObject.text1}"/>

            Works like a charm. Thanks for the input