6 Replies Latest reply on Jul 27, 2006 2:49 PM by dimival

    DataGrid EventListener

    Josh Johnson Level 1
      I've set up a listener on a DataGrid to listen for row selections, and then fire events. The MouseEvents that I set work just fine, but I also wanted to trap keyboard navigation within the DataGrid. I did this by importing the mx.events.DataGridEvent, and then referencing the ITEM_FOCUS_IN public constant. Problem is that the event isn't picking up changes like arrow up and arrow down.

      What am I missing here?

      Here's my code:

      import mx.events.DataGridEvent;
      public function createListeners():void {
      theGrid.addEventListener(MouseEvent.CLICK, setDetailPanelHeight);
      theGrid.addEventListener(DataGridEvent.ITEM_FOCUS_IN, setDetailPanelHeight);
      }
        • 1. Re: DataGrid EventListener
          peterent Level 2
          Are you editing cells? If so, the arrow keys won't get you out of the cell. If you are not editing cells, then up and down move from row to row.

          What condition are trying to trap the events for: editing or not?
          • 2. Re: DataGrid EventListener
            peterent Level 2
            If you are editing cells, use itemFocusIn as the event and if you are not editing, use change which tells you when the selectedItem or selectedIndex changes - that's what using the arrow keys will do.
            • 3. Re: DataGrid EventListener
              dimival Level 1
              Why don't you use the Change event instead????
              According to the documentation of the DataGrid, the change event says:
              Dispatched when the selectedIndex or selectedItem property changes as a result of user interaction.

              I use this event whenever i want to do something when a row is selected, and it works when the mouse is clicked as when the keyboard is used as well.
              I recommend you to register an event listener to the change event like this:
              theGrid.addEventListener('change', setDetailPanelHeight);

              This surely will work, try it and if it does dont forget to mark this post as the answer :P jeje.
              • 4. DataGrid EventListener
                Josh Johnson Level 1
                Well, the change Event worked like a charm on my non-editable dataGrid, though I'm a little confused by where that event is defined from. From the documentation, the signature is:

                componentInstance.addEventListener(event_type:String, event_listener:Function)

                but in this case it's just defining the String, without specifying the event_type. Where's that event come from?

                Either way, thanks to Peter and Dimival for the suggestion.
                • 5. Re: DataGrid EventListener
                  Josh Johnson Level 1
                  Sorry, the thread won't let me go back to mark previous entries as "the answer", but Dimival's example worked just right.
                  • 6. Re: DataGrid EventListener
                    dimival Level 1
                    Ah well about the type of the change event, if you refer to the Language Reference it only says:

                    change event
                    Event object type: flash.events.Event

                    Dispatched when the selectedIndex or selectedItem property changes as a result of user interaction.


                    As a matter of fac when you register the listener you need to pass a String with the type of the event, so it's the same if you use a string or a constant containing the string. For the clcik event for example , you can do:

                    theGrid.addEventListener(MouseEvent.CLICK, setDetailPanelHeight);
                    or
                    theGrid.addEventListener('click', setDetailPanelHeight);

                    because in the end MouseEvent.CLICK = 'change'.

                    To mark my reply as the answer, log in into the forums, once logged many options appear: Home, Profile, etc, go to the My Forums section and you will see all the topics where you have participated, select this one and since you created this one, on each reply it will appear a link saying "Mark this post as the answer", you click it and that reply will be marked as the answer.