Macromedia Flex Macromedia Flex
Change vs Click in the Flex 2 DataGrid
  Home

May 24, 2006 - Change vs Click in the Flex 2 DataGrid
Using an Event Listener to handle a click event in the DG in Flex2

In learning Flex 2, instead of binding directly to the dataGrid (as all the examples seem to do), I wanted to handle that using an event listener (the docs seem to use the term event listener, when in my opinion a function that handles the result of an event, is an event handler).

Anyways, I found this can be accomplished by either using the change or the click events. After testing both I can't really see the difference, other than the change event seems to be of a more generic level by firing off a plain Event, where as the click event fires off a MouseEvent.

Other subtle differences is that the change Event has event.target, where as the click MouseEvent uses an event.currentTarget.

Using a Click Handler:

<mx:DataGrid id="DG1" click="clickHandler(event)"/>
<mx:Script>
  public function clickHandler(event:MouseEvent):void
  {
      someControl.text = event.currentTarget.selectedItem.someDataField;
  } 
</mx:Script>

Using a Change Handler:

<mx:DataGrid id="DG2" change="changeHandler(event)"/>
<mx:Script>
  public function changeHandler(event:Event):void
  {
      someControl.text = event.target.selectedItem.someDataField;
  } 
</mx:Script>

File Details
Created On May, 24, 2006 by Scott Russel
Last Modified On May, 24, 2006 by Scott Russel
Group: Tips and Articles
Flex Versions: 2.0
Category: Controls:Lists (eg DataGrid/Tile/Tree)
Type: Tip
Difficulty: Beginner
Keywords:
Comments (1)
February 24, 2008 06:07PM - John Evans
The term "event listener" is used instead of "event handler" because you can add multiple event listeners for the same event. The actual "handling" of the event may be spread across multiple listeners, or the event may go "unhandled" despite the fact that there are listeners. For an example you may add a listener for debugging purposes that does not "handle" the event. The difference between the change and click events is that the change event fires no matter how the change was made -- e.g. by using the keyboard to select a different row... whereas the click event only fires if the change was made by clicking the mouse.

You must be logged in to post.