flex 3 iterate through object values

心已入冬 提交于 2019-12-06 03:08:26

问题


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:


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.




回答2:


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]);
}



回答3:


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…




回答4:


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> 


来源:https://stackoverflow.com/questions/674158/flex-3-iterate-through-object-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!