One of the more mysterious features of the Flex DataGrid is how to use item editors. Well I am going to start a few tutorials on this subject. This first tutorial is going to jump in and show how to create an easy to use item editor. Item editors can be simple or very complex - anything from a text input to a full input form. This tutorial is going to use a basic ComboBox to change a single value.
In the example application below we have two columns, one for the name and one to define if they are "guapo". The second column has been defined as editable so if you click on it you will see the ComboBox show up and you will be able to change the value to Yes or No. After editing has finished the dataprovider on the DataGrid will be updated with the new value. This means you actually have to click somewhere else or Enter to submit the changes - this is part of the item editing process. Well take a second and check it out. The source code for the item editor example is available.
Jumping right into it I am going to throw out the code for the main application. This code has the DataGrid and a little bit of script to initialize the data we use. The SimpleItem class is also below and is simple.
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="300" height="150"
layout="absolute" creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
private var items:ArrayCollection;
private function init():void
{
items = new ArrayCollection();
items.addItem(new SimpleItem("Charlie", true));
items.addItem(new SimpleItem("Brandon", true));
items.addItem(new SimpleItem("Kevin", false));
items.addItem(new SimpleItem("Mike", true));
items.addItem(new SimpleItem("Joker", false));
}
]]>
</mx:Script>
<mx:DataGrid dataProvider="{items}" editable="true" width="100%" height="100%">
<mx:columns>
<mx:DataGridColumn headerText="Name" dataField="name" editable="false" />
<mx:DataGridColumn headerText="Is Guapo" dataField="guapo"
itemEditor="ComboEditor" editorDataField="newValue" width="125"/>
</mx:columns>
</mx:DataGrid>
</mx:Application>
And the SimpleItem class, which is of course Bindable.
{
[Bindable]
public class SimpleItem
{
public var name:String;
public var guapo:Boolean;
public function SimpleItem(name:String, guapo:Boolean)
{
this.name = name;
this.guapo = guapo;
}
}
}
There are a few items in the DataGrid that I should go over. I set the editable property on the grid to be true. For the first column I set the editable property to false to keep people from changing the name associated with the item. All the hook up magic is done in the second column which is the column that can be edited. The itemEditor property takes the name of the component we are going to use for the editor - in our case ComboEditor, a custom component. The second important property is the editorDataField which tells the column what property to bind back to the dataField property. We now need to build the custom component.
The component is based off of a Box and has a ComboBox. The component code lies below. I will explain the nuances after.
<mx:Box xmlns:mx="http://www.adobe.com/2006/mxml" width="100%"
height="100%" horizontalAlign="center">
<mx:Script>
<![CDATA[
override public function set data(value:Object):void
{
super.data = value;
newValue = value.guapo;
}
]]>
</mx:Script>
<mx:Boolean id="newValue" />
<mx:ComboBox id="dd" selectedIndex="{data.guapo ? 0 : 1}"
change="{newValue = (dd.selectedIndex == 0)}">
<mx:dataProvider>
<mx:Array>
<mx:String>Yes</mx:String>
<mx:String>No</mx:String>
</mx:Array>
</mx:dataProvider>
</mx:ComboBox>
</mx:Box>
First and foremost you will notice the script which overrides the set data function to initialize the newValue. The second thing you will see in the component is that I have a Boolean value named newValue, same as the editorDataField. Next we have the drop down which has its selectedIndex bound to guapo property on item. If guapo is true the drop down will show "Yes" otherwise "No". The final thing we need to do is update newValue when the value changes. To do this we hook into the event fired when the drop down changes, when this happens we set newValue equal the boolean expression dd.selectedIndex == 0 which will return true if the "Yes" value is selected.
That pretty much takes care of creating and using a very basic item editor in Flex. I plan on expanding on these basic ideas in a later tutorial so keep an eye out. Until then feel free to ask any questions or just leave a nice comment.
09/19/2008 - 01:51
Got here searching for "How to make one column of a datagrid editable based on value of a combobox in the same row (with ItemRenderer)"
Dint really get an answer to that , but your other examples are great !
09/13/2009 - 21:04
Good posting, just what I was looking for :-)
I just started looking at Flex seriously and actually working on a project to learn more.
This site is pretty sweet esp with all the Flex tuts around.
Thanks for the good work, hopefully soon I will also get my site up and add you to my blog roll.
Have a good one
09/18/2009 - 08:56
Great post!
This article saves me lot of hours of work!
Thanks a lot
01/31/2010 - 06:12
Thx. a lot - exactly what I was looking for :)
Add Comment
[language] [/language]
Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]
See here for supported languages.
Javascript must be enabled to submit anonymous comments - or you can login.