从下拉列表中选取自定义对象
我在从下拉列表中选取自定义数据类型时遇到问题。为了使其尽可能容易理解,我将使用一个简单的示例来说明我希望能够执行的操作
,假设我有一个自定义数据类型(例如 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
访问下拉列表的 selectedItem;您必须将其转换为您想要的类型:
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:
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.
你尝试过吗?
或不进行类型转换
如果这不起作用,则发布您的错误代码。
Have you tried?
or without type casting
If this does not work then post your error codes.