How to add user-defined buttons in SAP ALV TOOLBAR

In this blog post, we discuss about the events of the class CL_GUI_ALV_GRID. The old SAP ALV model provides standard functions such as summing, sorting, search function, Excel Export function etc. It also enables us to add new functions to the SAP ALV toolbar.

The Event TOOLBAR of the class CL_GUI_ALV_GRID is used to add functions to the toolbar of the SAP ALV List.

The following steps are needed in order to add a Button.

  1. Define the Event handler.
  2. Register the Event handler for the event.
  3. Activate the event through the method SET_TOOLBAR_INTERACTIVE()
  1. Definition of Event Handler.

The Event handler is defined in the static method of the class like below.

              CLASS zcl_handler DEFINITION PUBLIC.

   PUBLIC SECTION.

                       CLASS-METHODS st_select_all_method FOR EVENT toolbar

                          OF cl_gui_alv_grid IMPORTING e-object.

               ENDCLASS.

               CLASS zcl_handler IMPLEMENTATION.

                      METHOD st_select_all_method.

                            DATA ls_button_toolbar TYPE stb_button.

                            ls_button_toolbar-function = ‘SELALL’.

                            ls_button_toolbar-icon = icon-select-all.

                            ls_button_toolbar-quickinfo = ‘SELECT ALL function’.

                            INSERT ls_button_toolbar INTO e_object -> mt_toolbar INDEX 2.

                     ENDMETHOD.

               ENDCLASS.

2. Event handler Registration.

The Event handler is registered using the ‘SET HANDLER … FOR … ‘ command.

An example below shows the registration.

SET HANDLER zcl_handler => st_select_all_method FOR lr_gui_alv_grid.

3. Activation of Event.

As a last step, the event must be activated through the method SET_TOOLBAR_INTERACTIVE. This ensures that the event is triggered from ALV. It is the method of the ALV object.

CALL METHOD lr_gui_alv_grid -> set_toolbar_interactive( )

 The event handler registration and event activation code section appears between the ALV table call ‘SET_TABLE_FOR_FIRST_DISPLAY’ and the ‘CALL SCREEN’ of Dynpro call.

 The Event handler uses the parameter e_object in order to insert the button entry into the internal table mt_toolbar. The parameter E_OBJECT is of type CL_ALV_EVENT_TOOLBAR_SET.