ALV with user defined menu on toolbar

Hello friends in this article we are going to see how to add user defined menu on toolbar in sap abap. user defined menu on toolbar using alv in sap abap.

REPORT ZALV_INTMENUTOOL.
*Author : http://mcqvillage.in
*AS : Simple ALV with user defined menu in toolbar

Published at SAPTechnical.COM
*Class declarations
CLASS lcl_event_receiver DEFINITION DEFERRED.
*type pool declarations
TYPE-POOLS : icon.
*Internal table and work area declarations for dd02l
DATA: it_dd02l TYPE TABLE OF dd02l,
wa_dd02l TYPE dd02l.
*Data declaration for alv.
DATA :it_layout TYPE lvc_s_layo,
it_toolbar TYPE stb_button,
c_alv TYPE REF TO cl_gui_alv_grid,
custom_container TYPE REF TO cl_gui_custom_container,
event_receiver TYPE REF TO lcl_event_receiver.
*Select options multiple values no ranges
SELECT-OPTIONS : s_table FOR wa_dd02l-tabname NO INTERVALS.
*Initialization event
INITIALIZATION.
*Start of selection event
START-OF-SELECTION.
*sUBROUTINE FOR ALV DISPLAY
PERFORM alvdisplay.
*Class definition
CLASS lcl_event_receiver DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:
*handling toolbar for interactive
handle_toolbar
FOR EVENT toolbar OF cl_gui_alv_grid
IMPORTING e_object e_interactive,
*handling menu button
handle_menu_button
FOR EVENT menu_button OF cl_gui_alv_grid
IMPORTING e_object e_ucomm,
*On click of the menu button
handle_user_command
FOR EVENT user_command OF cl_gui_alv_grid
IMPORTING e_ucomm.
PRIVATE SECTION.
ENDCLASS. “lcl_event_receiver DEFINITION
*Class implementation
CLASS lcl_event_receiver IMPLEMENTATION.
METHOD handle_toolbar.

handle toolbar
CLEAR it_toolbar.
MOVE ‘DETAIL’ TO it_toolbar-function.
MOVE icon_detail TO it_toolbar-icon.
MOVE 2 TO it_toolbar-butn_type.
APPEND it_toolbar TO e_object->mt_toolbar.
ENDMETHOD. “handle_toolbar
METHOD handle_menu_button.

handle own menubuttons
IF e_ucomm = ‘DETAIL’.
CALL METHOD e_object->add_function
EXPORTING
fcode = ‘DISPLAY’
text = ‘DISPLAY’.
ENDIF.
ENDMETHOD. “handle_menu_button
METHOD handle_user_command.
*On click
CASE e_ucomm.
WHEN ‘DISPLAY’.
MESSAGE ‘Menu Clicked’ TYPE ‘I’.
ENDCASE.
ENDMETHOD. “handle_user_command
ENDCLASS. “lcl_event_receiver IMPLEMENTATION
*&—————————————————————–*
*& Module PBO OUTPUT
*&—————————————————————–*

text
——————————————————————
MODULE pbo OUTPUT.
IF custom_container IS INITIAL.

select data from table dd02l
PERFORM fetch_dd02l.

create a custom container control for our ALV Control
CREATE OBJECT custom_container
EXPORTING
container_name = ‘CCONT’.

create an instance of alv control
CREATE OBJECT c_alv
EXPORTING i_parent = custom_container.

Set a titlebar for the grid control
it_layout-grid_title = ‘TABLE DETAILS’.
*ALV display
CALL METHOD c_alv->set_table_for_first_display
EXPORTING
i_structure_name = ‘dd02l’
is_layout = it_layout
CHANGING
it_outtab = it_dd02l.
*Handlers for the events
CREATE OBJECT event_receiver.
SET HANDLER event_receiver->handle_user_command
event_receiver->handle_menu_button
event_receiver->handle_toolbar FOR ALL INSTANCES.
*Calling the interactive toolbar method of ALV
CALL METHOD c_alv->set_toolbar_interactive.
ENDIF.
ENDMODULE. ” PBO OUTPUT
*&—————————————————————–*
*& Module PAI INPUT
*&—————————————————————–*

text
—————————————————————–
MODULE pai INPUT.
ENDMODULE. ” PAI INPUT
&—————————————————————-
*& form fetch_dd02l
*&—————————————————————-*

text
—————————————————————–
*Subroutine to fetch data
FORM fetch_dd02l.
SELECT * FROM dd02l INTO CORRESPONDING FIELDS OF TABLE it_dd02l
WHERE tabname IN s_table.
ENDFORM. ” SELECT_TABLE_dd02l
&—————————————————————–
*& Form ALVDISPLAY
*&—————————————————————–*

text
——————————————————————

–> p1 text

<– p2 text
——————————————————————
FORM alvdisplay .

ALV output
SET SCREEN 600.
ENDFORM. ” ALVDISPLAY

Leave a comment