从下拉列表中选取自定义对象

发布于 2024-12-06 15:38:20 字数 969 浏览 0 评论 0原文

我在从下拉列表中选取自定义数据类型时遇到问题。为了使其尽可能容易理解,我将使用一个简单的示例来说明我希望能够执行的操作

,假设我有一个自定义数据类型(例如 Dog 类型)。狗包含名称、品种和年龄。我将狗的每个实例存储在 ArrayCollection 中:

[Bindable]
private var dogData : ArrayCollection;

此 ArrayCollection 包含 1..N 个 Dog 对象以及相应的信息。现在有一个像这样的下拉菜单:

<s:DropDownList x="81" y="178" id="dogSelected" prompt="Dog Selected:" dataProvider="{dogData}" labelField="dogNameData"  />

dogNameData 假设来自一个自定义 ActionScript 类,该类在该对象中具有 Dog 的“name”字段。

现在我想从下拉列表中选择一只狗。我尝试这样做:

var theDog : Dog;
theDog = dogSelected.selectedItem;

但是,ActionScript 似乎不喜欢这样。现在,我四处阅读并发现使用标签字段是能够选择它的方法。我无法选择狗项目,因此我可以将其绑定到:

var selectedDogBreed : String;
//var theDog : Dog = the selected object from my drop down
selectedDogBreed = theDog.breed

任何人都可以帮助我从下拉列表中选择这个对象吗?非常感谢。

另请注意,ArrayCollection 是动态生成的。在我的实际应用程序中,我试图解决这个问题,我的自定义数据数组是动态的。没有什么是硬编码的

I am having an issue with picking up custom data types from a drop down lists. To make this as easy to understand as possible, I'll use a simple example of what I want to be able to do

So say I have a custom data type (Say of type Dog). Dog contains a name, breed and age. I store each instance of a dog in an ArrayCollection:

[Bindable]
private var dogData : ArrayCollection;

This ArrayCollection holds 1..N Dog objects with the respective information. Now having a dropdown like so:

<s:DropDownList x="81" y="178" id="dogSelected" prompt="Dog Selected:" dataProvider="{dogData}" labelField="dogNameData"  />

the dogNameData would hypothetically come from a custom ActionScript class that has the 'name' field of the Dog in that object.

Now I want to select a certain dog from the dropdown. I tried to just do it this way:

var theDog : Dog;
theDog = dogSelected.selectedItem;

However, ActionScript does not seem to like this. Now, I read around and found out that using the label field is the way to be able to select this. I have been unable to select the dog item, so I can then bind it to:

var selectedDogBreed : String;
//var theDog : Dog = the selected object from my drop down
selectedDogBreed = theDog.breed

Would anyone be able to help me be able to select this object from the drop down? Much thanks in advance.

Also to note, the ArrayCollection is dynamically generated. In my actual application I am trying to figure this out for, my array of custom data is dynamic. Nothing is hard coded

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

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

发布评论

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

评论(2

万水千山粽是情ミ 2024-12-13 15:38:20

访问下拉列表的 selectedItem;您必须将其转换为您想要的类型:

var theDog : Dog;
theDog = dogSelected.selectedItem as Dog

labelfield 与访问 selectedItem 无关。 labelField 仅由默认 itemRenderer 使用来决定在下拉列表中显示什么值。如果您没有看到下拉列表中显示任何文本;或者看到 [object object] 或类似的东西,那么这就是 labelField 发挥作用的地方。

To access the selectedItem of a drop down; you'll have to cast it as the type you want:

var theDog : Dog;
theDog = dogSelected.selectedItem as Dog

The labelfield has nothing to do with accessing the selectedItem. The labelField is just used by the default itemRenderer to decide what value to display in the drop down. IF you're not seeing any text displayed in the drop down; or seeing [object object] or something similar, then that is where labelField comes into play.

嘿嘿嘿 2024-12-13 15:38:20

你尝试过吗?

trace( 'name ' + (dogData[dogSelected.selectedIndex] as Dog).name )

或不进行类型转换

trace( 'name ' + dogSelected.selectedItem.name )

如果这不起作用,则发布您的错误代码。

Have you tried?

trace( 'name ' + (dogData[dogSelected.selectedIndex] as Dog).name )

or without type casting

trace( 'name ' + dogSelected.selectedItem.name )

If this does not work then post your error codes.

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