Adding Custom Context Menu in Classical List
Adding Custom Context Menu in Classical List
*&---------------------------------------------------------------*
*& Report Z_CONTEXT_MENU_DEMO
*&
*&---------------------------------------------------------------*
*& published at SAPTechnical.COM
*&
*&---------------------------------------------------------------*
REPORT z_context_menu_demo.
* Table declaration
DATA: i_vbak TYPE STANDARD TABLE OF vbak INITIAL SIZE 0,
i_vbap TYPE STANDARD TABLE OF vbap INITIAL SIZE 0.
* Workarea declaration
DATA: w_vbak TYPE vbak,
w_vbap TYPE vbap.
START-OF-SELECTION.
* Set custom status
SET PF-STATUS 'BASIC'.
* Fetch header data
SELECT * FROM vbak
INTO TABLE i_vbak
UP TO 50 ROWS.
IF sy-subrc = 0.
* Fetch line item data
SELECT * FROM vbap
INTO TABLE i_vbap
FOR ALL ENTRIES IN i_vbap
WHERE vbeln = i_vbap-vbeln.
CHECK sy-subrc = 0.
* Display basic list
LOOP AT i_vbak INTO w_vbak.
FORMAT COLOR COL_HEADING.
WRITE : /10 w_vbak-vbeln,
20 w_vbak-erdat,
35 w_vbak-erzet,
55 w_vbak-ernam.
HIDE: w_vbak-vbeln.
ENDLOOP.
ENDIF.
AT USER-COMMAND.
* Handle user command
CASE sy-ucomm.
WHEN 'DETAIL'.
CHECK NOT w_vbak IS INITIAL.
* Display detail list
LOOP AT i_vbap INTO w_vbap WHERE vbeln =
w_vbak-vbeln.
FORMAT COLOR COL_HEADING.
WRITE : /10 w_vbap-vbeln,
25 w_vbap-posnr,
35 w_vbap-matnr,
55 w_vbap-matwa.
ENDLOOP.
WINDOW STARTING AT 20 20
ENDING AT 120 110.
ENDCASE.
*&---------------------------------------------------------------*
*& Form on_ctmenu_request
*&---------------------------------------------------------------*
* Creation of custom context menu- It is called dynamically
* by ABAP runtime
*----------------------------------------------------------------*
* -->L_MENU Handle for context menu
*----------------------------------------------------------------*
FORM on_ctmenu_request USING l_menu TYPE REF TO cl_ctmenu.
DATA lin TYPE i.
GET CURSOR LINE lin.
IF lin > 2 AND sy-lsind = 0.
* Add menu
CALL METHOD l_menu->add_function
EXPORTING
fcode = 'DETAIL'
text = text-001.
ENDIF.
* Add menu
CALL METHOD l_menu->add_function
EXPORTING
fcode = 'BACK'
text = text-002.
ENDFORM. "on_ctmenu_request
Whenever user presses right mouse button or shift + F10 key combinations sap triggers system
event and calls the method DISPATCH_SYSTEM_EVENTS of class CL_GUI_CFW. Within it, it
calls the method DISPATCH of class LCL_DYNPRO_PROXY (defined within the class pool of
CL_GUI_CFW).
From this method (DISPATCH) it calls the routine ON_CTMENU_REQUEST which is defined in
our program.
Output:
Basic list:
Detail list:
Code:
***********************************************************************
* Type pool declaration
***********************************************************************
TYPE-POOLS: slis.
***********************************************************************
* Internal table declaration
***********************************************************************
DATA: BEGIN OF gt_outtab OCCURS 0.
**********************************************************************
* Structure / Variable declaration
**********************************************************************
DATA: g_repid LIKE sy-repid,
**********************************************************************
* Event: START-OF-SELECTION
**********************************************************************
START-OF-SELECTION.
EXPORTING
i_list_type = 4
IMPORTING
et_events = gt_events
EXCEPTIONS
list_type_wrong = 1
OTHERS = 2.
IF sy-subrc = 0.
REFRESH gt_events.
* Adding records for CONTEXT_MENU event
event-name = 'CONTEXT_MENU'.
event-form = 'CONTEXT_MENU'.
ENDIF.
* Data Selection
SELECT * FROM sflight INTO CORRESPONDING FIELDS
OF TABLE gt_outtab
UP TO 00030 ROWS.
EXPORTING
i_callback_html_top_of_page = 'HTML_TOP_OF_PAGE'
i_callback_program = g_repid
i_callback_user_command = 'USER_COMMAND'
i_structure_name = 'SFLIGHT'
it_events = gt_events
TABLES
t_outtab = gt_outtab
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
***********************************************************************
* FORM html_top_of_page
***********************************************************************
FORM html_top_of_page USING top TYPE REF TO cl_dd_document.
EXPORTING
sap_style = 'heading'.
EXPORTING
width = 200.
EXPORTING
picture_id = 'ENJOYSAP_LOGO'.
ENDFORM. "html_top_of_page
***********************************************************************
* Form context_menu
***********************************************************************
FORM context_menu USING e_object TYPE REF TO cl_ctmenu.
IF e_object IS BOUND.
EXPORTING
fcode = 'ZFN1'
EXPORTING
menu = l_smenu
text = 'Hide'(002).
ENDIF.
ENDFORM. "CONTEXT_MENU
***********************************************************************
* Form user_command
***********************************************************************
FORM user_command USING r_ucomm TYPE sy-ucomm
CASE r_ucomm.
e_callback_program = l_repid
e_grid = g_grid
et_fieldcat_lvc = t_catalog.
IF sy-subrc = 0.
EXPORTING
it_fieldcatalog = t_catalog.
ENDIF.
ENDIF.
WHEN OTHERS.
* Do nothing
ENDCASE.
ls_selfield-refresh = 'X'.
ENDFORM. "USER_COMMAND
Output: