It seems to me that Adobe's click-to-edit Datagrid is dysfunctional: if you click any cell, you immediately jump into edit mode, and run the risk of screwing up your data when Mr. Pickles runs across your keyboard late at night.
Wouldn't it be nice to have click merely
select the cell, and have
double-click trigger the editor?
After much searching I realized most "examples" are either broken, outdated, or involve a complete rewrite of the DataGrid and/or ItemRenderers, which is overkill, imho.
Below is a much simpler method:
ActionScript Code:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Script>
<![CDATA[
import mx.events.DataGridEvent;
import mx.events.ListEvent;
private var dblClickEditMode:Boolean;
[Bindable] private var dp:Array = [
{Artist:"Pink Floyd", Price:"11.99", Album:"The Wall"},
{Artist:"Pink Floyd", Price:"13.99", Album:"Animals"},
{Artist:"Squeeze", Price:"12.99", Album:"Singles: 45's and Under"},
{Artist:"Squeeze", Price:"10.99", Album:"Some Fantastic Place"},
]
protected function dgGrid_itemClick(event:ListEvent):void {
dblClickEditMode=false;
}
protected function dgGrid_itemDoubleClick(event:ListEvent):void {
dblClickEditMode=true;
}
protected function dgGrid_itemEditBegin(event:DataGridEvent):void {
if(!dblClickEditMode) event.preventDefault();
}
]]>
</fx:Script>
<mx:DataGrid id="dgGrid"
editable="true"
doubleClickEnabled="true"
itemClick="dgGrid_itemClick(event)"
itemDoubleClick="dgGrid_itemDoubleClick(event)"
itemEditBegin="dgGrid_itemEditBegin(event)"
dataProvider="{dp}">
<mx:columns>
<mx:DataGridColumn dataField="Album"/>
<mx:DataGridColumn dataField="Price"/>
</mx:columns>
</mx:DataGrid>
</s:Application>
All I'm doing is trapping the "Open Editor" event and checking if it was immediately preceded by a double-click or single-click event. If it was a single-click event, I simply block it, preventing the editor from opening.
It seems to me this is a much better method than rewriting components, but I'm open to comments/suggestions...