Tuesday, May 17, 2011

USING ALV EVENT HANDLER FOR ALL INSTANCES

 We have more than one alv grid instance ( cl_gui_alv_grid ). Instead of defining separate event handlers for each instance , we could define one for an event using  FOR ALL INSTANCES keyword.

 When any of them fires related event, we could catch and take actions. But how do we know the instance ? Sender parameter is the key.

*** INSTANCES
    DATA go_alv01 TYPE REF TO cl_gui_alv_grid ,
           go_alv02 TYPE REF TO cl_gui_alv_grid ,
           go_alv03 TYPE REF TO cl_gui_alv_grid .
 
*** LINK EVENTS TO INSTANCE
SET HANDLER go_event_handler->handle_double_click FOR ALL INSTANCES.

*** DEFINITION OF EVENT
*---------------------------------------------------------------------*
*       CLASS lcl_event_handler DEFINITION
*---------------------------------------------------------------------*
class lcl_event_handler definition .

  public section .

    class-methods:

     handle_double_click
     for event double_click of cl_gui_alv_grid
     importing e_row e_column sender.

  private  section.

endclass.                    "lcl_event_handler DEFINITION


*** IMPLEMENTATION OF EVENTS
*---------------------------------------------------------------------*
*       CLASS lcl_event_handler IMPLEMENTATION
*---------------------------------------------------------------------*
class lcl_event_handler implementation .


  method handle_hotspot_click.


   CASE sender.
   
     WHEN go_alv01.
     " go_alv01 has fired the event
   
     WHEN go_alv02.
     " go_alv02 has fired the event
    
     WHEN go_alv03.
     " go_alv03 has fired the event
    
   ENDCASE.



  endmethod.                    "handle_hospot_click