flex 3 迭代对象值

发布于 2024-07-15 10:23:58 字数 119 浏览 7 评论 0原文

我有一个代表数据库表的对象。 我想迭代这个对象并打印每个值。 我可以用什么来做到这一点?

我想在我的 mxml 中执行此操作,而不是

为每个对象属性执行此操作,我想创建一个输入字段

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北恋 2024-07-22 10:23:58

查找有关 Flex 3 循环的文档。 如果你这样做,你会发现:

for..in

for..in 循环迭代对象的属性或数组的元素。 例如,您可以使用 for..in 循环来迭代通用对象的属性(对象属性不以任何特定顺序保存,因此属性可能以看似随机的顺序出现):

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

而不是尝试创建输入每个对象的字段,我建议您查看 DataGrid和自定义 ItemEditors

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.

时光无声 2024-07-22 10:23:58

我同意这个答案没有用。 它仅适用于通用对象,不适用于用户声明的对象
对象。

然而,这里有一些代码应该/可以使用上面建议的describeType来工作。 (而且我真的不认为它太复杂)。 请注意,仅公开公共属性/方法等:

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

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]);
}
假情假意假温柔 2024-07-22 10:23:58

“for...in”的问题在于它仅迭代动态属性。 也就是说,如果您的对象被定义为类(并且不是动态的),则“for..in”不会给出任何内容。

ActionScript 文档建议使用describeType() 来获取固定属性,但对于这个简单的任务来说它看起来过于复杂......

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…

您可以像 actionscript 一样编写它,但将其包含在带有 script 标记的 mxml 文件中:

<mx:Script>
   <![CDATA[
       public function LoopAndPrint() : void
       {
           //your code here
       }
   ]]>
 </mx:Script> 

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> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文