
An Easy Reference for ALV Grid Control
General Scheme for the Event Handler Class
Here is a sample code to illustrate a general scheme for our local class.
CLASS lcl_event_handler DEFINITION .
PUBLIC SECTION .
METHODS:
*--To add new functional buttons to the ALV toolbar
handle_toolbar FOR EVENT toolbar OF cl_gui_alv_grid
IMPORTING e_object e_interactive ,
*--To implement user commands
handle_user_command
FOR EVENT user_command OF cl_gui_alv_grid
IMPORTING e_ucomm ,
*--Hotspot click control
handle_hotspot_click
FOR EVENT hotspot_click OF cl_gui_alv_grid
IMPORTING e_row_id e_column_id es_row_no ,
*--Double-click control
handle_double_click
FOR EVENT double_click OF cl_gui_alv_grid
IMPORTING e_row e_column ,
*--To be triggered before user commands
handle_before_user_command
FOR EVENT before_user_command OF cl_gui_alv_grid
IMPORTING e_ucomm ,
*--To be triggered after user commands
handle_after_user_command
FOR EVENT context_menu_request OF cl_gui_alv_grid
IMPORTING e_object
*--Controlling data changes when ALV Grid is editable
handle_data_changed
FOR EVENT data_changed OF cl_gui_alv_grid
IMPORTING er_data_changed ,
© 2005 SAP AG
36

An Easy Reference for ALV Grid Control
*--To be triggered after data changing is finished
handle_data_changed_finished
FOR EVENT data_changed_finished OF cl_gui_alv_grid
IMPORTING e_modified ,
*--To control menu buttons
handle_menu_button
FOR EVENT menu_button OF cl_gui_alv_grid
IMPORTING e_oject e_ucomm ,
*--To control button clicks
handle_button_click
FOR EVENT button_click OF cl_gui_alv_grid
IMPORTING e_oject e_ucomm .
PRIVATE SECTION.
ENDCLASS.
CLASS lcl_event_handler IMPLEMENTATION .
*--Handle Toolbar
METHOD handle_toolbar.
PERFORM handle_toolbar USING e_object e_interactive .
ENDMETHOD .
*--Handle Hotspot Click
METHOD handle_hotspot_click .
PERFORM handle_hotspot_click USING e_row_id e_column_id es_row_no .
ENDMETHOD .
*--Handle Double Click
METHOD handle_double_click .
PERFORM handle_double_click USING e_row e_column es_row_no .
ENDMETHOD .
*--Handle User Command
METHOD handle_user_command .
PERFORM handle_user_command USING e_ucomm .
ENDMETHOD.
© 2005 SAP AG
37

An Easy Reference for ALV Grid Control
Code Part 21 –
Local event handler class definition and implementation
*--Handle After User Command
METHOD handle_context_menu_request .
PERFORM handle_context_menu_request USING e_object .
ENDMETHOD.
*--Handle Before User Command
METHOD handle_before_user_command .
PERFORM handle_before_user_command USING e_ucomm .
ENDMETHOD .
*--Handle Data Changed
METHOD handle_data_changed .
PERFORM handle_data_changed USING er_data_changed .
ENDMETHOD.
*--Handle Data Changed Finished
METHOD handle_data_changed_finished .
PERFORM handle_data_changed_finished USING e_modified .
ENDMETHOD .
*--Handle Menu Buttons
METHOD handle_menu_button .
PERFORM handle_menu_button USING e_object e_ucomm .
ENDMETHOD .
*--Handle Button Click
METHOD handle_button_click .
PERFORM handle_button_click USING e_object e_ucomm .
ENDMETHOD .
ENDCLASS .
Here in the implementation part, we are branching to forms to get rid of restrictions driven by the OO
context. It is your choice to branch or directly write your codes into the method.
Here in local class coding, just implement the methods which will handle events that you want to be
triggered. In this tutorial, we will prefer to deal with topics as functionalities rather than explaining events
one-by-one. Having an event handler class we are now able to instantiate it and register its methods to
handle ALV Grid instance events.
© 2005 SAP AG
38

An Easy Reference for ALV Grid Control
Code Part 22 -
Creating event handler instance and registering handler methods
DATA gr_event_handler TYPE REF TO lcl_event_handler .
..
..
*--Creating an instance for the event handler
CREATE OBJECT gr_event_handler .
*--Registering handler methods to handle ALV Grid events
SET HANDLER gr_event_handler->handle_user_command FOR gr_alvgrid .
SET HANDLER gr_event_handler->handle_toolbar FOR gr_alvgrid .
SET HANDLER gr_event_handler->handle_menu_button FOR gr_alvgrid .
SET HANDLER gr_event_handler->handle_double_click FOR gr_alvgrid .
SET HANDLER gr_event_handler->handle_hotspot_click FOR gr_alvgrid .
SET HANDLER gr_event_handler->handle_button_click FOR gr_alvgrid .
SET HANDLER gr_event_handler->handle_before_user_command
FOR gr_alvgrid .
SET HANDLER gr_event_handler->handle_context_menu_request
FOR gr_alvgrid .
SET HANDLER gr_event_handler->handle_data_changed FOR gr_alvgrid .
SET HANDLER gr_event_handler->handle_data_changed_finished
FOR gr_alvgrid .
Hotspot Clicking
From field catalog field definitions, we know that we can make some columns to respond to single
clicks as hotspots by setting the value for the field “
HOTSPOT
” to ‘X’ while generating the field catalog.
After clicking, the event “
hotspot_click
” is triggered. This event has three parameters in its interface.
The parameter “e_row_id” is obsolete. Other two parameters are “
es_row_no
” which is of type
“
LVC_S_ROID
” and passes information about the row index at “
es_row_no-row_id
”, and
“
e_column_id
” of type “
LVC_S_COL
” which returns the column fieldname at “
e_column_id-
fieldname
”. Utilizing these parameters you know where the user clicked and trigger your action.
© 2005 SAP AG
39

An Easy Reference for ALV Grid Control
FORM handle_hotspot_click USING i_row_id TYPE lvc_s_row
i_column_id
TYPE
lvc_s_col
is_row_no
TYPE
lvc_s_roid.
READ TABLE gt_list INDEX is_row_no-row_id .
IF sy-subrc = 0 AND i_column_id-fieldname = 'SEATSOCC' .
CALL SCREEN 200 . "Details about passenger-seat matching
ENDIF .
ENDFORM .
Code Part 23 –
An example implementation for the method, handling the event “hotspot_click”
Double Clicking
As you can guess, handling a double-click is very similar to handle a hotspot click. You do nothing
additional to make a field double-click intensive; that is you need not set some option in the field catalog.
After double-click event occurs, the method handling it will return again three parameters and again one
of them, “
e_row
”, is obsolete. The parameters “
e_column
” and “
es_row_no
” are similar to the
parameters of the event “
hotpot_click
”.
FORM handle_double_click USING i_row TYPE lvc_s_row
i_column
TYPE
lvc_s_col
is_row_no
TYPE
lvc_s_roid.
READ TABLE gt_list INDEX is_row_no-row_id .
IF sy-subrc = 0 AND i_column-fieldname = 'SEATSOCC' .
CALL SCREEN 200 . "Details about passenger-seat matching
ENDIF .
ENDFORM .
Code Part 24 -
An example implementation for the method, handling the event “double_click”
© 2005 SAP AG
40