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:
-
-
<?xml version="1.0" encoding="utf-8"?>
-
<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:
-
-
<?xml version="1.0" encoding="utf-8"?>
-
<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
opup -
-
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:
-
-
<?xml version="1.0" encoding="utf-8"?>
-
<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.
Tweet
10 Comments »

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!
January 7th, 2009 at 8:25 am
Very good stuff…
January 11th, 2009 at 8:14 am
Hi,
Thank you this help me a lot.
February 9th, 2009 at 3:54 am
Thanks man,
How can I use it in AdvancedDataGrid?
November 15th, 2009 at 10:44 pm
Very cool, and a great help. I ran into one small problem. Intermittently (gotta love those) the updated value would not render in the data grid. I could see that the value did get updated in the dataprovider, so I added myDG.invalidateList(). I also commented out the var row:int as it was not used within the function.
var myDG:DoubleClickDataGrid = this.owner as DoubleClickDataGrid;
var row:int=_listData.rowIndex+myDG.verticalScrollPosition;
this.data[_listData.dataField]=myVar;
myDG.dataProvider.itemUpdated(data);
myDG.invalidateList();
January 15th, 2010 at 1:17 am
Thanks a lot.
To make it work in AdvnacedDataGrid
The updateDP method in MyRenderer has to be modified to point to a AdvancedDataGrid instead of DataGrid.
public function updateDP(str:String):void{
var myDG:AdvancedDataGrid=this.owner as AdvancedDataGrid;
var row:int=_listData.rowIndex+myDG.verticalScrollPosition;
this.data[_listData.dataField]=str;
myDG.dataProvider.itemUpdated(data);
}
September 3rd, 2010 at 3:55 am
This is a great example!
Thanks for sharing!