我有一个数据网格,我希望能够随意删除行。下面是我为了达到预期结果而组合在一起的组件项目渲染器,但必须有更好的方法来访问“父”数据网格的数据提供者。有人有什么想法/建议吗?
<?xml version="1.0" encoding="utf-8"?>
<s:MXDataGridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
focusEnabled="true">
<fx:Script>
<![CDATA[
public function deleteRow(event:MouseEvent):void
{
var dataGrid:DataGrid = event.target.parent.parent.parent; // There has to be a better way to get the data grid then climbing up the entity chain like this.
dataGrid.dataProvider.removeItemAt(dataGrid.dataProvider.getItemIndex(data));
}
]]>
</fx:Script>
<s:Button label="Delete" click="deleteRow(event)"/>
</s:MXDataGridItemRenderer>
答案 0 :(得分:1)
你不......
public function deleteRow( event : MouseEvent = null ) : void {
var delEvent : DynamicEvent = new DynamicEvent( "deleteEvent", true );
delEvent.dataToDelete = data;
dispatchEvent( delEvent );
}
然后在课堂上与你的DG你的DG。
<mx:DataGrid id="foo" dataProvider="{someDP}" />
foo.addEventListener( "deleteEvent", updateDP );
private function updateDP( event : DynamicEvent ) : void {
foo.dataProvider.removeItem( event.dataToDelete );
}