5

i have an object which represents a database table. I want to iterate through this object and print printing each value. What can i use to do this?

i want to do this inside my mxml not actionscript

for each object attribute i want to create an imput field

Tyler
  • 21,762
  • 11
  • 61
  • 90
cdugga
  • 3,849
  • 17
  • 81
  • 127

4 Answers4

12

Look up the documentation on Flex 3 looping. If you do, you'll find this:

for..in

The for..in loop iterates through the properties of an object, or the elements of an array. For example, you can use a for..in loop to iterate through the properties of a generic object (object properties are not kept in any particular order, so properties may appear in a seemingly random order):

var myObj:Object = {x:20, y:30};
for (var i:String in myObj)
{
    trace(i + ": " + myObj[i]);
}
// output:
// x: 20
// y: 30

Instead of trying to create an input field for each object, I'd suggest you take a look at DataGrid and custom ItemEditors.

Community
  • 1
  • 1
dirkgently
  • 108,024
  • 16
  • 131
  • 187
  • 2
    As Kemenaran pointed below, if you want to iterate over class properties, the canonical solution above does not work. Granted, iterating over unknown class properties would be mostly useful when debugging. – Sint Feb 04 '10 at 10:26
4

I agree that this answer isn't useful. It only works with generic objects, not user declared objects.

However, here's some code that should/could work using the describeType as suggested above. (And I don't really think it's too complex). Be aware that only public properties/methods, etc. are exposed:

var ct:CustomObject = new CustomObject(); 
var xml:XML = describeType(ct);
for each(var accessor in xml..accessor) {
  var name:String = accessor.@name;
  var type.String = accessor.@type;
  trace(ct[name]);
}
taudep
  • 2,871
  • 5
  • 27
  • 36
  • Note that this example only includes *accessors* -- attributes that are actually defined using getters and setters. One would need to modify it slightly if one were interested in variables, constants or methods. See [the documentation of describeType](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/package.html#describeType\(\)) for more specific information. – Jeremy Jun 10 '13 at 16:13
2

The problem with "for...in" is that it iterates only on dynamic properties. That is, if your object is defined as a Class (and not dynamically), "for..in" won't give anything.

The ActionScript documentation suggest to use describeType() for fixed properties, but it looks over-complicated for this simple task…

Kemenaran
  • 1,421
  • 12
  • 11
1

You can write it like actionscript but include it inside the mxml file with the script tag:

<mx:Script>
   <![CDATA[
       public function LoopAndPrint() : void
       {
           //your code here
       }
   ]]>
 </mx:Script> 
loomisjennifer
  • 535
  • 1
  • 4
  • 9