Flex Datagrid Edit Cell on Row Click

With flex datagrids you sometimes find you have a datagrid with multiple columns but only one or two columns are editable.

Which column are editable are not immediately apparent to a user, they have to click on cells to find out what's editable or not editable.

You can add some sort of visual indication to show what cells are editable (see here) OR you can make clicking on a row edit a cell.

Here's how to detect clicking on a cell and set the editable column of the datagrid.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()" viewSourceURL="srcview/index.html">
   
   <mx:DataGrid id="myDG" dataProvider="{customers}" rowCount="5" width="300" height="200" editable="true" itemClick="editCell(event)" >
      <mx:columns>
         <mx:DataGridColumn headerText="Name" width="100" dataField="name" editable="false" editorDataField="text" />
         <mx:DataGridColumn headerText="Addresss" dataField="address" width="200" editable="true" />
      </mx:columns>
   </mx:DataGrid>
   
   <mx:Model id="custdata">
      <customers>
         <customer>
            <name>Cust1</name>
            <address>Address1</address>
         </customer>
         <customer>
            <name>Cust2</name>
            <address>Address2</address>
         </customer>
         <customer>
            <name>Cust3</name>
            <address>Address3</address>
         </customer>      
      </customers>
   </mx:Model>   

   <mx:Script>
      <![CDATA[
         import mx.events.ListEvent;
         import mx.collections.ArrayCollection;
         
         [Bindable] private var customers:ArrayCollection = new ArrayCollection();

         private function init():void {
            customers = new ArrayCollection(custdata.customer);
         }
         
         public function editCell(event:ListEvent):void {
            // don't want to edit header
            if (event.rowIndex > 0) {
               myDG.editedItemPosition = {columnIndex:1, rowIndex:event.rowIndex-1};
            }
         }
      ]]>
   </mx:Script>

</mx:Application>

The only thing to be be careful of is that the itemClick event handler will be called not only if a cell in the datagrid is clicked on but also if the header of the datagrid is clicked on so you need to take one off the rowIndex when setting the edited item position.

Here what the code looks like when run (right click to view source and/or download):

Related Blog Entries

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Justin Mclean's Gravatar One thing to be careful of is when using this method is to handle column draging otherwise you can drag the columns about and then edit the wrong field (as the column index is hard coded).
# Posted By Justin Mclean | 7/1/07 10:03 AM
Steve's Gravatar Hi. Nice tip, I was looking for this. Unfortunately, it's not working for me -- it always activates the first editor.

I've got two data grids in my app, with drag and drop between them. The destination list is the one I need to do this to. One column uses a custom item editor, a numeric stepper. Problem is, I drag one item, click it, nothing. Drag a second item over, click it -- the FIRST row's stepper highlights. Same happens for each, only the first row highlight no matr what row is clicked on. Thoughts?
# Posted By Steve | 7/15/07 11:08 AM
Justin Mclean's Gravatar Hi Steve,

Email me your code and I'll take a quick look at it, sounds like you're not setting the row number.

Justin
# Posted By Justin Mclean | 7/16/07 5:36 PM
Lou's Gravatar Thanks this is a great tip on editing datagrid cells.
The item renderer from a component doesn't seem to work for me.
I don't know if it's a bug or what.

But how about if I got numeric cell to edit and I want to format them?
# Posted By Lou | 7/27/07 1:14 PM
Justin Mclean's Gravatar The simple way is to use a standard text input control but restrict what the user types by using setting the restrict attribute to be &quot;0-9&quot;.

Email me you code and I'll take a look at why the item renderer isn't working.
# Posted By Justin Mclean | 7/27/07 1:39 PM
Sinan's Gravatar Thanks Justin
That was exactly what i was looking for!
# Posted By Sinan | 11/17/07 12:09 AM
Javier Julio's Gravatar Great example Justin, thanks! I used your example as a stepping stone for something I'm trying to achieve. Based on a boolean in the selected row of a DataGrid I'd like to disable the editable property of a field. I thought itemClick might help me get around that but its not.

Do you know if there is a way to set an editable property on a specific cell? I still want the entire column to be editable but with some logic I need to disable a cell being editable depending on the value of a boolean in that selected row (selectedItem). Is this possible? Was having trouble with this and this post has gotten me the closest to figuring it out.
# Posted By Javier Julio | 12/20/07 8:59 AM
Justin Mclean's Gravatar Yes it should be possible.

The simple way would be to enable/disable the text field based on the boolean ie the user can't edit the field event though it shows up as a text input.

I'll look into not having it text input coming up. My first try would be to make a custom renderer with a viewstack with a text field on the first view and a text field on the second. Then use the editable property to change the selectedIndex to display the non editable version or non editable version of the content.
# Posted By Justin Mclean | 12/20/07 9:20 AM
Randy's Gravatar I am trying to use this idea and it is working great, except then I have a scrolling datagrid. When I select a row and the datagrid is not scrolled all the way to the bottom the whole datagrid jumps. I am using Flex 3. Has anyone else had this problem?
# Posted By Randy | 5/16/08 2:09 AM
Chuck G's Gravatar This is a great plugin. Just FYI, in Flex3, the header row is treated differently, so you can remove the line:
if (event.rowIndex &gt; 0) {...}
and change the editedItemPosition to
rowIndex:event.rowIndex

Thanks!
# Posted By Chuck G | 6/18/08 12:38 AM
Copyright © Justin Mclean 2008
BlogCFC by Raymond Camden.