views:

27

answers:

1

I have a datagrid from which I'd like be able to remove rows at will. Below is the component item renderer I'm putting together in order to achieve desired result, but there has to be a better way to access the data provider of the "parent" data grid. Anyone have any ideas/suggestions?

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

You don't...

public function deleteRow( event : MouseEvent = null ) : void {
    var delEvent : DynamicEvent = new DynamicEvent( "deleteEvent", true );
    delEvent.dataToDelete = data;
    dispatchEvent( delEvent );
}

then in the class with your DG your DG.

<mx:DataGrid id="foo" dataProvider="{someDP}" />
foo.addEventListener( "deleteEvent", updateDP );
private function updateDP( event : DynamicEvent ) : void {
    foo.dataProvider.removeItem( event.dataToDelete );
}
Gregor Kiddie
Mads610