Interesting.  We haven't provided enough APIs to make this sort of thing
simpler.  Worthy of an enhancement request, but it is too late to make
it into 3.0.
 
You get a CANCELLED when you update the dataprovider.  It automatically
kills any existing edit session.  I would actually try faking
KeyboardEvents and keyFocusChange events to mimic TAB/shift-TAB and
ENTER/shift-ENTER in response to the arrow keys.  Might make the code
simpler.  However, I sort of got it working by modifying the example as
attached.  The idea is to not reset the DP and just call updateList,
then restore the editor afterwards.

________________________________

From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of j_lentzz
Sent: Tuesday, November 27, 2007 5:42 PM
To: [email protected]
Subject: [flexcoders] Re: Losing Focus in DataGrid



Ok. Here is the sample. If you select an item column, the arrow keys
navigate just fine. If you navigate to the 1st column and then nav
away, the focus isn't there. The effect of changing the first column
and having it mirrored in the others works, but the focus doesn't go
to the next cell. Thanks for all the help.

John
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml
<http://www.adobe.com/2006/mxml> "
xmlns:ns1="widgets.*" layout="absolute" xmlns:n1="FAB_widgets/widgets">

<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.events.DataGridEvent;
import mx.controls.ComboBox;
import mx.controls.TextInput;

[Bindable]
protected var _listData:ArrayCollection = new
ArrayCollection([{item1:'2', item2:'5',item3:'2'},
{item1:'1', item2:'1',item3:'1'}]);

private function handleEditEnd(event:DataGridEvent):void {

if (event.reason != "CANCELLED" && event.dataField == "applyAll") {
var tempString:String =
TextInput(event.currentTarget.itemEditorInstance).text;
if (tempString != null && tempString != "" && tempString != " ")
callLater(updateRow, [event,
Number(TextInput(event.currentTarget.itemEditorInstance).text)]);
}
}

private function updateRow(event:DataGridEvent, newData:Number):void {
var dpAC:ArrayCollection = dg.dataProvider as ArrayCollection;
var dgCols:Array = dg.columns;

// set all qty columns to this number
var obj:Object = dpAC.getItemAt(event.rowIndex);

for (var i:int=0; i< dgCols.length; i++) { // go through defined
cols and set date columns to selectAll value
if ((dgCols[i] as DataGridColumn).dataField.indexOf("item") == 0) {
obj[(dgCols[i] as DataGridColumn).dataField] = newData;
}
}
dpAC.setItemAt(obj,event.rowIndex);
dg.dataProvider = dpAC;
(dg.dataProvider as ArrayCollection).itemUpdated(obj); 

}

]]>
</mx:Script>

<ns1:DataGridWithKeyboard headerHeight="21" editable="true" rowHeight
= "30" id="dg" dataProvider="{_listData}" visible="true" 
selectable="false" itemEditEnd="handleEditEnd(event)" >
<ns1:columns>
<mx:DataGridColumn dataField="applyAll" headerText="Apply to all"/>
<mx:DataGridColumn dataField="item1" headerText="Item 1"/>
<mx:DataGridColumn dataField="item2" headerText="Item 2"/>
<mx:DataGridColumn dataField="item3" headerText="Item 3"/>
</ns1:columns>
</ns1:DataGridWithKeyboard>

</mx:Application>

/* ------ DataGridWithKeyboard Class ----- */

package widgets
{
import mx.controls.DataGrid;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import mx.controls.dataGridClasses.DataGridColumn;
import mx.collections.ArrayCollection;
import mx.events.DataGridEventReason;
import mx.events.DataGridEvent;


public class DataGridWithKeyboard extends DataGrid
{
public function DataGridWithKeyboard()
{
super();
}
override protected function keyDownHandler(event:KeyboardEvent):void {
trace('keyDownHandler - DataGridWithKeyboard');
var eip:Object = this.editedItemPosition;
var numColumns:int = this.columns.length;
var dgc:DataGridColumn;
var numRows:int = (this.dataProvider as ArrayCollection).length;

if (event.keyCode == Keyboard.TAB)
return;

if (eip != null) {

switch (event.keyCode) {

case Keyboard.RIGHT:
// go one cell over (or to the next visible and non-0 width
column),
// or if at end of line, then go to start of next line
// if no line exists, then create the new line
var colFound:Boolean = false;
while (!colFound) {
if (eip.columnIndex < numColumns) {
eip.columnIndex++;
dgc = this.columns[eip.columnIndex] as DataGridColumn;
if (dgc != null && dgc.visible && dgc.width > 0 && dgc.editable)
colFound = true;
}
else { // at end of row, so go to start of next row
if (eip.rowIndex < numRows) {
eip.rowIndex++;
eip.columnIndex = -1; // start before row, so next loop can
increment and check
}
else { // end of last row, so add row
// DO THIS
}
}
}
// set new values
this.editedItemPosition = eip;
this.dataProvider.refresh();
break;

case Keyboard.LEFT:
// go one cell to left(or to the next visible and non-0 width
column). If at start of line, then do nothing.
colFound = false;
var saveColIndex:int = eip.columnIndex;
while (!colFound) {
if (eip.columnIndex > 0) {
eip.columnIndex--; 
dgc = this.columns[eip.columnIndex] as DataGridColumn;
if (dgc != null && dgc.visible && dgc.width > 0 && dgc.editable)
colFound = true;
}
else {
eip.columnIndex = saveColIndex;
colFound = true;
}
}
// set new values
this.editedItemPosition = eip;
this.dataProvider.refresh();
break;

case Keyboard.UP:
// go one row up, if at first row, then do nothing
if (eip.rowIndex > 0)
eip.rowIndex--;
// set new values
this.editedItemPosition = eip;
this.dataProvider.refresh();
break;

case Keyboard.DOWN:
// go down one row, if at last row, then add new row and go to
the cell below this one
if (eip.rowIndex <= numRows)
eip.rowIndex++;
// set new values
this.editedItemPosition = eip;
this.dataProvider.refresh();
break;
}
}
}

}
}
--- In [email protected] <mailto:flexcoders%40yahoogroups.com>
, "Alex Harui" <[EMAIL PROTECTED]> wrote:
>
> Can you post a working mini-testcase?
> 
> ________________________________
> 
> From: [email protected] <mailto:flexcoders%40yahoogroups.com>
[mailto:[email protected] <mailto:flexcoders%40yahoogroups.com>
] On
> Behalf Of j_lentzz
> Sent: Tuesday, November 27, 2007 12:58 PM
> To: [email protected] <mailto:flexcoders%40yahoogroups.com> 
> Subject: [flexcoders] Re: Losing Focus in DataGrid
> 
> 
> 
> After I set the editedItemPosition from the arrow key pressed, I
> refresh the dataprovider, because that seemed to make it move the
> focus properly. For the 'selectAll' function, I perform an
> itemUpdated on the arraycollection after setting the new item in it,
> and then refresh the dataprovider, since I didn't see the new data in
> the grid if I didn't. Is there a better way (i.e. one that works)
> that I should do to get the cell to navigate properly with the arrow
> keys and the new data to be reflected in the row?
> 
> Thanks,
> 
> John
> --- In [email protected]
<mailto:flexcoders%40yahoogroups.com>
<mailto:flexcoders%40yahoogroups.com>
> , "Alex Harui" <aharui@> wrote:
> >
> > Are you resetting the dataprovider or some other dataprovider? I
would
> > not recommend doing that.
> > 
> > ________________________________
> > 
> > From: [email protected]
<mailto:flexcoders%40yahoogroups.com>
<mailto:flexcoders%40yahoogroups.com>
> [mailto:[email protected]
<mailto:flexcoders%40yahoogroups.com>
<mailto:flexcoders%40yahoogroups.com>
> ] On
> > Behalf Of j_lentzz
> > Sent: Tuesday, November 27, 2007 11:32 AM
> > To: [email protected] <mailto:flexcoders%40yahoogroups.com>
<mailto:flexcoders%40yahoogroups.com> 
> > Subject: [flexcoders] Losing Focus in DataGrid
> > 
> > 
> > 
> > Hi,
> > 
> > I've got a dataGrid that I want to allow someone to set a value in a
> > 'selectAll' column and that value will be passed to all the other
> > appropriate columns on that row upon the
DataGridEvent.ITEM_EDIT_END.
> > I also have a custom keyDownHandler, to allow them to use the arrow
> > keys to navigate in the grid. The arrow keys navigate just fine (and
> > the focus is correct) for all the columns except for the 'selectAll'
> > column. When a value is typed in this column and the arrow keys are
> > used, the value is passed to the other columns properly, but the
grid
> > is getting a CANCELLED edit event and the row is deselected. If I
use
> > the TAB key to navigate from the 'selectAll' column, the other
columns
> > are updated and the row stays selected. It seems like I'm missing a
> > call of some kind in my keyDownHandler, to allow it to function
> > properly. A sample of the code follows. If anyone can tell me what
> > I'm doing wrong, I'd greatly appreciate it.
> > 
> > Thanks,
> > John
> > override protected function keyDownHandler(event:KeyboardEvent):void
{
> > var eip:Object = this.editedItemPosition;
> > var numColumns:int = this.columns.length;
> > var dgc:DataGridColumn;
> > var numRows:int = (this.dataProvider as ArrayCollection).length;
> > 
> > if (event.keyCode == Keyboard.TAB)
> > return;
> > 
> > if (eip != null) {
> > switch (event.keyCode) {
> > 
> > case Keyboard.RIGHT:
> > // go one cell over (or to the next visible and non-0 width column),
> > // or if at end of line, then go to start of next line
> > // if no line exists, then create the new line
> > var colFound:Boolean = false;
> > while (!colFound) {
> > if (eip.columnIndex < numColumns) {
> > eip.columnIndex++;
> > dgc = this.columns[eip.columnIndex] as DataGridColumn;
> > if (dgc != null && dgc.visible && dgc.width > 0 && dgc.editable)
> > colFound = true;
> > }
> > else { // at end of row, so go to start of next row
> > if (eip.rowIndex < numRows) {
> > eip.rowIndex++;
> > eip.columnIndex = -1; // start before row, so next loop can
> > increment and check
> > }
> > else { // end of last row, so add row
> > // DO THIS
> > }
> > }
> > }
> > // set new values
> > this.editedItemPosition = eip;
> > this.dataProvider.refresh();
> > break;
> > 
> > 
> > SAME FOR OTHER ARROW KEYS...
> > }
> > }
> > 
> > private function handleEditEnd(event:DataGridEvent):void {
> > trace('dgevent reason: ' + event.reason + ' dataField: ' +
> > event.dataField );
> > 
> > if (event.reason != "CANCELLED" && event.dataField == "selectAll") {
> > var tempString:String =
> > NumericInEmbedded(event.currentTarget.itemEditorInstance).text;
> > trace('tempString: ' + tempString);
> > if (tempString != null && tempString != "" && tempString != " ")
> > callLater(updateRow, [event,
> >
>
Number(NumericInEmbedded(event.currentTarget.itemEditorInstance).text)])
> > ;
> > }
> > }
> > 
> > private function updateRow(event:DataGridEvent, newData:Number):void
{
> > trace('updateRow called');
> > var dpAC:ArrayCollection = LPA_ItemList.dataProvider;
> > var dgCols:Array = LPA_ItemList.dg.columns;
> > 
> > // set all qty columns to this number
> > var obj:Object = dpAC.getItemAt(event.rowIndex);
> > if (obj["startDate"] != null)
> > obj["startDate"] = newData;
> > if (obj["endDate"] != null)
> > obj["endDate"] = newData;
> > 
> > for (var i:int=0; i< dgCols.length; i++) { // go through defined
> > cols and set date columns to selectAll value
> > if ((dgCols[i] as DataGridColumn).dataField.indexOf("date") == 0) {
> > obj[(dgCols[i] as DataGridColumn).dataField] = newData;
> > }
> > }
> > dpAC.setItemAt(obj,event.rowIndex);
> > LPA_ItemList.dataProvider = dpAC;
> > (LPA_ItemList.dg.dataProvider as ArrayCollection).itemUpdated(obj); 
> > // LPA_ItemList.dg.dataProvider.refresh();
> > 
> > }
> >
>



 

Attachment: DataGridWithKeyboard.as
Description: DataGridWithKeyboard.as

Attachment: test.mxml
Description: test.mxml

Reply via email to