I am trying to get the following code to run when I hit enter on a row within a data grid, it works when I click on the row (ListEvent) but how would I enable this to work when the enter key is hit (KeyboardEvent). I have the enter key working although it throws this error every time
private function onButtonClick(evt:KeyboardEvent):void
{
var item:Object = evt.itemRenderer.data;
openWorkflowItem(item.date.date, item.workFlowID);
$multiEdit = false;
if (target === currentWorkflowItems)
{
$histFilter['sym'] = item.sym;
histSym.text = item.sym;
applyHistFilters();
}
}
<mx:AdvancedDataGrid
id="historicalWorkflowItems"
dataProvider="{$historicalWFItems}"
width="100%" height="100%"
itemClick="{onWFItemClick(event)}"
keyDown="if (event.keyCode==Keyboard.ENTER){ onButtonClick(event)}"
borderStyle="none"
sortExpertMode="true"
useHandCursor="true"
headerShift="{saveColumnSettings('historical', historicalWorkflowItems)}"
columnStretch="{saveColumnSettings('historical', historicalWorkflowItems)}"
horizontalScrollPolicy="auto"
verticalScrollPolicy="auto"
allowMultipleSelection="true"
>
The problem is that you are assigning a key listener to the grid, so there's no specific item in this context. The
keyDownevent fires whenever a key is pressed with focus anywhere within the grid. You want something likeitemKeyDown, but the grid does not seem to provide you with such an event.This is why I asked you to check
Event/targetto see what value it holds. If an item renderer has focus and the user presses a key, your handler will fire and you can check if the eventtargetis an item renderer, exif(event.target is AdvancedDataGridItemRenderer). (The eventcurrentTargetwill always be the grid.) Now, I'm not super familiar with the implementation ofAdvancedDataGriditem rendering, but it's likely that the eventtargetwill not itself be an item renderer, only a child of an item renderer (say a text field inside the item renderer). To check if thetargetis inside an item renderer is possible by walking the display tree and checking if a parentisan item renderer class:Now you can handle your event like this:
But this is getting kind of silly to search the display hierarchy like this. What you should do is just dispatch an event from inside your item renderer, so that the event target is always the item renderer itself. For example:
Now you can handle this event: