Stephen's dev blog

Posts Tagged ‘datagrid

I just remembered that I had trouble finding out what I could access with this event handler function. Sure it was easy to find out when the event is dispatched, but how can I access the new field value edited by the user? How do I access the previous value?

So this is just a simple example showing you that. This also applies to the normal DataGrid and standard list controls.

The itemEditEnd event is dispatched (it’s in the name) when a user finishes editing a cell so this is pretty much the best time to save the user’s changes to that field/row.

Here’s our function:

protected function saveChange(event:AdvancedDataGridEvent):void
{
    /**
     * use event.dataField to get the edited field name
     */
    var field:String = event.dataField;

    /**
     * i'm using the following line to get the 'id' of the row being edited
     * because i passed an 'id' field through from my dataProvider.
     */
    var selectedID:String = event.currentTarget.selectedItem.id.toString();

    /**
     * use the itemEditorInstance to get the newly edited value but retrieve
     * the previous value from the existing grid data. i'm using the default
     * textinput itemRenderer so I can retrieve the new value using the
     * itemEditInstance's 'text' property.
     */
    var newValue:String = event.currentTarget.itemEditorInstance.text;
    var prevValue:String = ProductPersonalisationList.selectedItem[event.dataField];

    // run a http service call or something to save the change or validate the data
}

I’ve just had a thought that it was hard to come accross resources for handling double click events on DataGrids in Flex 3.

There are lots of tutorials on how to handle them in Flex 2 but this doesn’t apply for Flex 3, as Flex 3 natively handles double click events and Flex 2 did not.

There are also a lot of tutorials on how to amend functionality so that you have to double click to edit a row instead of the default single click. This tutorial does not cover this, this tutorial covers functionality of double clicking a row and then triggering something. I.E. a table of products, you want to double click a row to view product details etc.

First off, we need to make a slight amendment to our DataGrid (or AdvancedDataGrid, same applies to both):

<mx:DataGrid id="MyDataGrid" doubleClickEnabled="true" itemDoubleClick="functionName(event);">
...

There are obvious meanings to both of these new attributes; doubleClickEnabled is used to tell Flex we want to allow/handle double click events on this DataGrid and itemDoubleClick tells Flex which function to use.

The above function should look something like:

protected function functionName(event:ListEvent):void
{
    var selectedRow:Object = event.currentTarget.selectedItem;
    Alert.show(selectedRow.myVariableName);
}

You can directly access the selected row from the DataGrid using the event.currentTarget.selectedItem reference. You can then access any variable that exists in the DataGrid’s dataProvider for that row.

Of course to get the above working you’ll need to:

import mx.controls.Alert;
import mx.events.ListEvent;

Stephen.



  • Patrick: I changed my code, but now I have another problem: Fatal error: Call to a member function isAvailable() on a non-object in /var/www/vhost/web/shopA
  • Stephen Gray: Hi Patrick, That first error is my fault. I had the method name for the refund() method as void() as I had copied the code from the other method!
  • Patrick: Hi Stephen, thanks for sharing your knowledge about creating a custom payment module. I need an extension for magento to handle a credit card payme

Categories