December 12th, 2007 by Kyle
Tags: datagrid, Flex, itemeditor, itemrenderer, poup
Posted in: ActionScript, Flex
This isn’t as difficult as some may think. Basically your itemRenderer opens up a popup. When the renderer creates the popup, it sets an “opener” property on the popup pointing back to “this”, which is the itemRenderer. That way, when the editing is finished in the popup, the popup can pass data back to a function in the itemRenderer to do the update, close itself, and set focus back to the itemRenderer.
Here is the code for the app:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:local="*" >
<mx:Array id="arr">
<mx:Object articleName="Finding out a characters Unicode character code Downloading the latest Adobe Labs version of Flex 3 SDK/Flex Builder 3 (codename: Moxie)" data="15" />
<mx:Object articleName="Setting an icon in an Alert control" data="14" />
<mx:Object articleName="Setting an icon in a Button control" data="13" />
<mx:Object articleName="Installing the latest nightly Flex 3 SDK build into Flex Builder 3" data="10" />
<mx:Object articleName="Detecting which button a user pressed to dismiss an Alert dialog" data="9" />
<mx:Object articleName="Using the Alert control Formatting data tips in a Slide" data="8" />
</mx:Array>
<mx:ArrayCollection id="AC" source="{arr}" />
<mx:DataGrid height="250" dataProvider="{AC}" variableRowHeight="true" width="60%" editable="true">
<mx:columns>
<mx:DataGridColumn dataField="data" headerText="ID" editable="false" width="125"/>
<mx:DataGridColumn
editable="false" wordWrap="true"
headerText="Article Name"
itemRenderer="MyRenderer" dataField="articleName"/>
</mx:columns>
</mx:DataGrid>
</mx:Application>
Here is the itemRenderer:
<mx:Text xmlns:mx="http://www.adobe.com/2006/mxml"
implements="mx.controls.listClasses.IDropInListItemRenderer"
toolTip="Double Click to Edit…" doubleClick="callLater(openPopup)" doubleClickEnabled="true"
text="{txt}">
<mx:Script>
<![CDATA[
import mx.controls.DataGrid;
import mx.controls.listClasses.ListData;
import mx.controls.dataGridClasses.DataGridListData;
import mx.controls.listClasses.BaseListData;
import mx.managers.PopUpManager;
import mx.events.FlexEvent;
private var _listData:DataGridListData;
[Bindable]
public var txt:String;
private var pop:Popup
override public function set data(value:Object):void {
super.data = value;
txt=data[_listData.dataField];
}
override public function get data():Object {
return super.data;
}
override public function get listData():BaseListData
{
return _listData;
}
override public function set listData(value:BaseListData):void
{
_listData = DataGridListData(value);
}
private function openPopup():void{
pop= Popup(PopUpManager.createPopUp(this.owner,Popup,true));
pop.txt=this.txt;
pop.opener=this;
}
public function updateDP(str:String):void{
var myDG:DataGrid=this.owner as DataGrid;
var row:int=_listData.rowIndex+myDG.verticalScrollPosition;
this.data[_listData.dataField]=str;
myDG.dataProvider.itemUpdated(data);
}
]]>
</mx:Script>
</mx:Text>
Here is the popup:
<mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml"
width="350" height="250" showCloseButton="false" creationComplete="centerMe()"
defaultButton="{btnSave}">
<mx:Script>
<![CDATA[
import mx.managers.FocusManager;
import mx.managers.PopUpManager;
[Bindable]
public var txt:String;
public var opener:Object;
private function save():void{
(opener as MyRenderer).updateDP(ta.text);
cancel();
}
private function cancel():void{
PopUpManager.removePopUp(this);
returnFocus();
}
private function returnFocus():void{
opener.setFocus();
}
private function centerMe():void{
PopUpManager.centerPopUp(this);
ta.setFocus();
}
]]>
</mx:Script>
<mx:TextArea id="ta" text="{txt}" height="100%" width="100%"/>
<mx:ControlBar>
<mx:HBox>
<mx:Button id="btnSave" label="save" click="save()"/>
<mx:Button id="btnCancel" label="cancel" click="cancel()"/>
</mx:HBox>
</mx:ControlBar>
</mx:TitleWindow>
Browse the source of this example.
Download a zipfile containing the source to this sample.


February 14th, 2008 at 2:41 pm
Hi,
Thanks for this and other examples.
I was trying to use this example but I get a null OBject reference when I click save any ideas?
I am using Flex 2.01
Anyway thanks again.
February 15th, 2008 at 10:15 am
The sample was compiled with 2.0.1 hf2. You should upgrade and see if it is still and issue.
April 4th, 2008 at 6:20 am
Hi,
Thank you, i got nice example
July 6th, 2008 at 1:35 am
Thanks, this works with Flex 3 as well!