Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flex 3 iterate through object values

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

like image 879
cdugga Avatar asked Mar 23 '09 16:03

cdugga


3 Answers

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.

like image 146
dirkgently Avatar answered Sep 28 '22 16:09

dirkgently


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]);
}
like image 38
taudep Avatar answered Sep 28 '22 18:09

taudep


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…

like image 36
Kemenaran Avatar answered Sep 28 '22 18:09

Kemenaran