views:

5400

answers:

4

hi,

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

+1  A: 

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
+4  A: 

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.

dirkgently
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
+2  A: 

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  A: 

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