[英]Bidirectional binding for flex ComboBox?
I have a collection that I want to bind as data input for a ComboBox
: 我有一个要绑定为
ComboBox
数据输入的集合:
private static var LOGOS:ArrayCollection = new ArrayCollection([
{index:0, label=logo1},
{index:1, label=logo2}
]);
<s:ComboBox selectedItem="@{model.labelIndex}" labelField="@label" dataProvider="{LOGOS}" />
Now, when selecting an item, the binding should send the associated index
property of the objext to the model and update labelIndex
. 现在,当选择一个项目时,绑定应将
labelIndex
的关联index
属性发送到模型并更新labelIndex
。 Of course it does not work as above, because labelIndex
is of different datatype than the ArrayCollection
. 当然,它不能如上所述工作,因为
labelIndex
的数据类型与ArrayCollection
数据类型不同。
[Bindable]
private var model:MyModel;
[Bindable]
class MyModel {
public var:Number labelIndex;
}
Question: how can I map the array element to the model and vice versa? 问题:如何将数组元素映射到模型,反之亦然?
What you are looking for will require some scripting, binding isn't smart enough to figure out how to handle this on its own. 您要查找的内容将需要一些脚本,绑定不够聪明,无法弄清楚如何自行处理。
You can use the BindingUtils
class to define the bindings, and use the chain
argument of the bindProperty
method to modify how values are being looked up. 您可以使用
BindingUtils
类来定义绑定,并使用bindProperty
方法的chain
参数来修改查找值的方式。
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/binding/utils/BindingUtils.html http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/binding/utils/BindingUtils.html
For the combo.selectedItem
to model.labelIndex
binding, you can specify the chain as an array, where the elements define the path where to look for the value: 对于
combo.selectedItem
到model.labelIndex
绑定,您可以将链指定为数组,其中元素定义查找值的路径:
BindingUtils.bindProperty(model, 'labelIndex', combo, ['selectedItem', 'index']);
This will bind to the selectedItem
property, and pass the value of the items index
property. 这将绑定到
selectedItem
属性,并传递items index
属性的值。
The other way around is a little more tricky and will require using a getter function which grabs the object from the datasource, based on the labelIndex
value: 另一种方法则比较棘手,将需要使用getter函数,该函数根据
labelIndex
值从数据源中获取对象:
BindingUtils.bindProperty(combo, 'selectedItem', model, {
name: 'labelIndex',
getter: function(host:MyModel):Object
{
return LOGOS.source.filter(function(item:Object, index:int, array:Array):Boolean
{
return item.index === host.labelIndex;
})[0];
}
});
This will bind to the labelIndex
property, and the getter
function will be invoked when the property changes. 这将绑定到
labelIndex
属性,并且在属性更改时将调用getter
函数。 The function will filter the datasource based on the models changed labelIndex
property value, and return the source object with the matching index
property value, which will finally be set for the combobox selectedItem
property. 该函数将根据模型更改后的
labelIndex
属性值过滤数据源,并返回具有匹配index
属性值的源对象,该属性最终将为组合框selectedItem
属性设置。
Your combobox definition will of course need an id
in order to be targetable via script 您的组合框定义当然需要一个
id
才能通过脚本进行定位
<s:ComboBox id="combo" dataProvider="{LOGOS}" />
Note that there's no need forthe the @
in the labelField
property, this is only used with XML datasources where you need to target an attribute. 请注意,在
labelField
属性中不需要@
,它仅用于需要定位属性的XML数据源。 However, actually you don't need to specify this at all, since label
is the default value of the labelField
property. 但是,实际上,您根本不需要指定它,因为
label
是labelField
属性的默认值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.