Local ALV event class template (to be completed with missing events) :
CLASS lcl_alv DEFINITION.
*------------------------------
* Public section
*------------------------------
PUBLIC SECTION.
METHODS :
handle_toolbar
FOR EVENT toolbar OF cl_gui_alv_grid
IMPORTING e_object
e_interactive,
handle_before_user_command
FOR EVENT before_user_command OF cl_gui_alv_grid
IMPORTING e_ucomm
sender,
handle_data_changed
FOR EVENT data_changed OF cl_gui_alv_grid
IMPORTING er_data_changed e_onf4 e_onf4_before e_onf4_after,
handle_data_changed_finished
FOR EVENT data_changed OF cl_gui_alv_grid
IMPORTING er_data_changed e_onf4 e_onf4_before e_onf4_after,
handle_user_command
FOR EVENT user_command OF cl_gui_alv_grid
IMPORTING e_ucomm,
on_f4 FOR EVENT onf4 OF cl_gui_alv_grid
IMPORTING e_fieldname
es_row_no
er_event_data
et_bad_cells
e_display,
handle_menu_button
FOR EVENT menu_button OF cl_gui_alv_grid
IMPORTING e_object e_ucomm.
ENDCLASS. " End of lcl_alv DEFNITION
CLASS lcl_alv IMPLEMENTATION.
METHOD handle_before_user_command .
*** "overriding standard function
*** CASE e_ucomm.
*** WHEN '&MB_FILTER'.
*** "suppress ALV further execution
*** CALL METHOD sender->set_user_command
*** EXPORTING
*** i_ucomm = space.
***
*** ENDCASE.
***
ENDMETHOD.
METHOD handle_data_changed.
*** CLEAR gv_flag_alv_has_error.
***
*** LOOP AT er_data_changed->mt_good_cells ASSIGNING FIELD-SYMBOL(<fs_cell>).
***
*** CASE <fs_cell>-fieldname.
***
*** WHEN 'FIELD'.
*** " -----------------------------------------------------------------------------------
*** " In case of error
*** gv_flag_alv_has_error = abap_true.
*** er_data_changed->add_protocol_entry( i_msgid = 'BD' i_msgno = '899' i_msgty = 'E'
*** i_msgv1 = 'Veuillez saisir un nombre entier positif'
*** i_fieldname = 'FIELD' i_row_id = <fs_cell>-row_id ).
*** ENDCASE.
***
*** ENDLOOP.
***
*** IF gv_flag_alv_has_error = abap_true.
*** er_data_changed->display_protocol( ).
*** ENDIF.
***
ENDMETHOD.
METHOD handle_data_changed_finished.
***
*** LOOP AT er_data_changed->mt_good_cells ASSIGNING FIELD-SYMBOL(<fs_cell>).
***
*** CASE <fs_cell>-fieldname.
***
*** WHEN 'FIELD1'.
*** " -----------------------------------------------------------------------------------
*** " Update other field value linked to value of FIELD1
*** er_data_changed->modify_cell( i_row_id = <fs_cell>-row_id
*** i_fieldname = 'FIELD2'
*** i_value = 'New value for FIELD2'
*** ).
***
*** ENDCASE.
***
*** ENDLOOP.
***
ENDMETHOD.
METHOD handle_toolbar.
***
*** APPEND INITIAL LINE TO e_object->mt_toolbar ASSIGNING FIELD-SYMBOL(<fs_toolbar>).
*** "<fs_toolbar>-butn_type = 0.
*** <fs_toolbar>-function = 'INSERT' ##NO_TEXT.
*** <fs_toolbar>-icon = icon_insert_row.
*** <fs_toolbar>-quickinfo = TEXT-002. " Tooltip (char30 max)
***
ENDMETHOD.
METHOD handle_user_command.
" CASE e_ucomm
ENDMETHOD.
METHOD on_f4.
***
*** " --------------------------------------------------------------------
*** " Declare handler (to be done at ALV initialisation) for field SFHNR :
**** DATA : lt_f4 TYPE lvc_t_f4.
**** lt_f4 = VALUE #( ( fieldname = 'SFHNR' register = abap_true ) ).
**** go_alv->register_f4_for_fields( lt_f4 ).
**** DATA(lo_alv_events) = NEW lcl_alv( ).
**** SET HANDLER lo_alv_events->on_f4 FOR go_alv.
*** " --------------------------------------------------------------------
***
***
*** DATA : lt_return TYPE TABLE OF ddshretval,
*** lt_map TYPE TABLE OF dselc.
***
*** " If several fields registered for F4, control the current field ==> IF e_fieldname = 'SFHNR'.
***
*** " Specific search-help example :
***
*** lt_map = VALUE #( ( fldname = 'F0001' dyfldname = 'SFHNR' )
*** ( fldname = 'F0002' dyfldname = 'KTEXT' )
*** ( fldname = 'F0003' dyfldname = 'PZLFH' ) ).
***
*** CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
*** EXPORTING
*** retfield = 'SFHNR'
*** value_org = 'S'
*** TABLES
*** value_tab = lt_data " to be defined
*** dynpfld_mapping = lt_map
*** return_tab = lt_return
*** EXCEPTIONS
*** parameter_error = 1
*** no_values_found = 2
*** OTHERS = 3.
***
*** IF lt_return IS NOT INITIAL.
*** LOOP AT lt_return INTO DATA(ls_return).
*** CASE ls_return-retfield.
*** WHEN 'SFHNR'.
*** ls_alv_data-sfhnr = ls_return-fieldval.
***
*** WHEN 'KTEXT'.
*** ls_alv_data-ktext = ls_return-fieldval.
***
*** WHEN 'PZLFH'.
*** ls_alv_data-pzlfh = ls_return-fieldval.
***
*** ENDCASE.
*** ENDLOOP.
*** ENDIF.
***
***
*** " Inform the ALV Grid Control that an own f4 help has been processed to suppress the standard f4 help.
*** er_event_data->m_event_handled = 'X'.
***
ENDMETHOD.
METHOD handle_menu_button .
ENDMETHOD.
ENDCLASS. " End of lcl_alv IMPLEMENTATION
Link handlers to ALV
ALV used here is type CL_GUI_ALV_GRID :
DATA : lt_f4 TYPE lvc_t_f4.
DATA(lo_alv_events) = NEW lcl_alv( ).
" Specific search help for field SFHNR
lt_f4 = VALUE #( ( fieldname = 'SFHNR' register = abap_true ) ).
go_alv->register_f4_for_fields( lt_f4 ).
SET HANDLER : lo_alv_events->handle_data_changed FOR go_alv,
lo_alv_events->handle_data_changed_finished FOR go_alv,
lo_alv_events->handle_user_command FOR go_alv,
lo_alv_events->handle_before_user_command FOR go_alv,
lo_alv_events->handle_toolbar FOR go_alv,
lo_alv_events->on_f4 FOR go_alv.
lo_alv_events->handle_menu_button FOR go_alv.
About the author