【问题标题】:Flex: How can I Make changes to an ArrayCollection bound to data grid live?Flex:如何对绑定到实时数据网格的 ArrayCollection 进行更改?
【发布时间】:2010-02-21 02:41:49
【问题描述】:
我有一个绑定到可编辑 DataGrid 的 ArrayCollection,另一个组件需要知道 ArrayCollection 何时更改(由于 DataGrid 中的更改),因此它也可以自行更新,监听 ArrayCollection 的 COLLECTION_CHANGE 事件也是如此。
问题是 DataGrid 仅在正在编辑的行失去焦点时更新 ArrayCollection。这对我的应用程序不利,因为用户可以编辑一行上的列,而不是长时间单击表上的其他位置(导致该行失去焦点),因此更改不会传播到其他部分应用程序。
如何让数据网格在每次文本输入发生 keyup 事件时通知 ArrayCollection 更改,而不是每次一行失去焦点时?
干杯,
克里斯
【问题讨论】:
标签:
apache-flex
data-binding
arraycollection
【解决方案1】:
我会将处理程序添加到用于编辑值的组件而不是 ArrayCollection。示例:
<mx:DataGridColumn dataField="name" headerText="Name" itemEditor="{nameEditor}" editorDataField="selectedItem" />
然后这是用来编辑值的:
<mx:Component id="nameEditor">
<mx:ComboBox dataProvider="{outerDocument.names}" change="outerDocument.setNameField(event)" close="outerDocument.setNameField(event)" />
</mx:Component>
这是更改(和关闭)事件的处理程序:
public function setDestinationField(event:*):void {
var destination:String = (event.target as ComboBox).selectedLabel;
if (destination === '') {
delete _gridData[_currentlyEditedRowIndex].destination;
} else {
_gridData[_currentlyEditedRowIndex].destination = destination;
}
}
_currentlyEditedRowIndex 是通过将其添加到网格来设置的:
itemEditBegin="beginEdit(event);"
【解决方案2】:
感谢加布里埃尔,你让我走上了正确的道路。我将在这里发布我的最终解决方案,以防其他人将来需要做同样的事情。
<mx:DataGridColumn headerText="Title" width="300" dataField="title">
<mx:itemEditor>
<mx:Component>
<mx:TextInput change="data.title = text" />
</mx:Component>
</mx:itemEditor>
</mx:DataGridColumn>
现在,只要输入中的文本发生更改,用作数据提供者的 ArrayCollection 也会更新,因此其他组件会侦听 COLLECTION_CHANGE 事件。