2010-09-28 81 views
2

我有一個datagrid,我希望能夠隨意刪除行。下面是我爲了實現所需結果而放在一起的組件項呈示器,但必須有更好的方式來訪問「父」數據網格的數據提供者。任何人有任何想法/建議?如何刪除datagrid中的行?

<?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> 

回答

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); 
} 
+0

使用事件是一種更簡潔的方法;感謝節目和告訴。 – Mads610 2010-09-28 21:26:13