Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to add my own menu item into alv context menu

Former Member
0 Kudos

Hi Experts

Pls guide to me that how can i add my own Menu item into alv grid.

Rajnish

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Rajnish,

The event related with context menus is “context_menu_request”. When

you right click on the ALV Grid area the event-handling method is triggered. Under

this method you can add entries in the context menu. You can utilize

“GET_SELECTED…” methods to retrieve which cell, row or column the user has rightclicked.

Adding functions to the context menu is accomplished as in adding

subfunctions to non-standard menu buttons

<b>Code Part 31 – Adding an entry for the context menu</b>

FORM handle_context_menu_request USING i_object TYPE REF TO cl_ctmenu .

*--Here you can add some logic to retrive what was clicked and

*--decide what to add to the context menu

CALL METHOD i_object->add_function

EXPORTING

fcode = 'CREA'

text = 'Create Booking'(204) .

ENDFORM .

You can add a separator by “add_separator”, a menu by “add_menu”, and a

submenu by “add_submenu” methods.

You can also use “disable_functions” to disable some of the functions on

the menu, “enable_functions” to enable, “hide_functions” to hide them and

“show_functions” to make them displayed again. You pass the list of the function

codes to those methods via the parameter “fcodes”. These are all about the class

“CL_CTMENU” since the context menu instantiates that class. For further functionalities

refer to that class.

<b>Code Part 32 – An example filling the table to be passed to above functions</b>

DATA: LT_FCODES TYPE UI_FUNCTIONS .

CLEAR LT_FCODES.

APPEND CL_GUI_ALV_GRID=>MC_FC_COL_OPTIMIZE TO LT_FCODES.

APPEND 'CREA' TO LT_FCODES.

CALL METHOD E_OBJECT->DISABLE_FUNCTIONS

EXPORTING FCODES = LT_FCODES.

4 REPLIES 4

Former Member
0 Kudos

Hi Rajnish,

The event related with context menus is “context_menu_request”. When

you right click on the ALV Grid area the event-handling method is triggered. Under

this method you can add entries in the context menu. You can utilize

“GET_SELECTED…” methods to retrieve which cell, row or column the user has rightclicked.

Adding functions to the context menu is accomplished as in adding

subfunctions to non-standard menu buttons

<b>Code Part 31 – Adding an entry for the context menu</b>

FORM handle_context_menu_request USING i_object TYPE REF TO cl_ctmenu .

*--Here you can add some logic to retrive what was clicked and

*--decide what to add to the context menu

CALL METHOD i_object->add_function

EXPORTING

fcode = 'CREA'

text = 'Create Booking'(204) .

ENDFORM .

You can add a separator by “add_separator”, a menu by “add_menu”, and a

submenu by “add_submenu” methods.

You can also use “disable_functions” to disable some of the functions on

the menu, “enable_functions” to enable, “hide_functions” to hide them and

“show_functions” to make them displayed again. You pass the list of the function

codes to those methods via the parameter “fcodes”. These are all about the class

“CL_CTMENU” since the context menu instantiates that class. For further functionalities

refer to that class.

<b>Code Part 32 – An example filling the table to be passed to above functions</b>

DATA: LT_FCODES TYPE UI_FUNCTIONS .

CLEAR LT_FCODES.

APPEND CL_GUI_ALV_GRID=>MC_FC_COL_OPTIMIZE TO LT_FCODES.

APPEND 'CREA' TO LT_FCODES.

CALL METHOD E_OBJECT->DISABLE_FUNCTIONS

EXPORTING FCODES = LT_FCODES.

uwe_schieferstein
Active Contributor
0 Kudos

Hello Rajnish

Here is a sample report for context menus in ALV lists. If you want to have cell-specific context menus (which makes sense) then you have to refine event handler method HANDLE_CONTEXT_MENU_REQUEST and check which cell has been selected and adjust the coding appropriately.

*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_ALVGRID_EVENTS_CTXMENU
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZUS_SDN_ALVGRID_EVENTS_CTXMENU.



DATA:
  gd_okcode        TYPE ui_func,
*
  gt_fcat          TYPE lvc_t_fcat,
  go_docking       TYPE REF TO cl_gui_docking_container,
  go_grid1         TYPE REF TO cl_gui_alv_grid.


DATA:
  gt_knb1          TYPE STANDARD TABLE OF knb1.


PARAMETERS:
  p_bukrs      TYPE bukrs  DEFAULT '2000'  OBLIGATORY.



*---------------------------------------------------------------------*
*       CLASS lcl_eventhandler DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_eventhandler DEFINITION.
  PUBLIC SECTION.
    CLASS-DATA:
      ms_row     TYPE lvc_s_row.

    CLASS-METHODS:
      handle_context_menu_request
        FOR EVENT context_menu_request OF cl_gui_alv_grid
        IMPORTING
          e_object  " type ref to cl_ctmenu
          sender,

       handle_user_command
         FOR EVENT user_command OF cl_gui_alv_grid
         IMPORTING
           e_ucomm
           sender.

ENDCLASS.                    "lcl_eventhandler DEFINITION


*---------------------------------------------------------------------*
*       CLASS lcl_eventhandler IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_eventhandler IMPLEMENTATION.

  METHOD handle_user_command.
*   define local data
    DATA:
      ls_knb1     TYPE knb1,
      ls_col_id   TYPE lvc_s_col.

    CHECK ( e_ucomm = 'XD03' ).

    READ TABLE gt_knb1 INTO ls_knb1 INDEX ms_row-index.
    CHECK ( ls_knb1-kunnr IS NOT INITIAL ).


    SET PARAMETER ID 'KUN' FIELD ls_knb1-kunnr.
    SET PARAMETER ID 'BUK' FIELD ls_knb1-bukrs.

    CALL TRANSACTION 'XD03' AND SKIP FIRST SCREEN.


  ENDMETHOD.                    "handle_user_command

  METHOD handle_context_menu_request.
*   define local data
    DATA:
      lt_rows    TYPE lvc_t_row.

    CLEAR: ms_row.
    CALL METHOD go_grid1->get_selected_rows
      IMPORTING
        et_index_rows = lt_rows.
    READ TABLE lt_rows INTO ms_row INDEX 1.

    e_object->add_separator( ).
    CALL METHOD e_object->add_function
      EXPORTING
        fcode       = 'XD03'
        text        = 'Display Customer'
*        ICON        =
*        FTYPE       =
*        DISABLED    =
*        HIDDEN      =
*        CHECKED     =
*        ACCELERATOR =
        .



  ENDMETHOD.                    "handle_context_menu_request

ENDCLASS.                    "lcl_eventhandler IMPLEMENTATION


START-OF-SELECTION.

  SELECT        * FROM  knb1 INTO TABLE gt_knb1
         WHERE  bukrs  = p_bukrs.



* Create docking container
  CREATE OBJECT go_docking
    EXPORTING
      parent                      = cl_gui_container=>screen0
      ratio                       = 90
    EXCEPTIONS
      OTHERS                      = 6.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


* Create ALV grid
  CREATE OBJECT go_grid1
    EXPORTING
      i_parent          = go_docking
    EXCEPTIONS
      OTHERS            = 5.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

* Set event handler
  SET HANDLER:
    lcl_eventhandler=>handle_user_command         FOR go_grid1,
    lcl_eventhandler=>handle_context_menu_request for go_grid1.


* Build fieldcatalog and set hotspot for field KUNNR
  PERFORM build_fieldcatalog_knb1.



* Display data
  CALL METHOD go_grid1->set_table_for_first_display
    CHANGING
      it_outtab       = gt_knb1
      it_fieldcatalog = gt_fcat
    EXCEPTIONS
      OTHERS          = 4.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.




* Link the docking container to the target dynpro
  CALL METHOD go_docking->link
    EXPORTING
      repid                       = syst-repid
      dynnr                       = '0100'
*      CONTAINER                   =
    EXCEPTIONS
      OTHERS                      = 4.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


* ok-code field = GD_OKCODE
  CALL SCREEN '0100'.
* NOTE: no dynpro elements on screen. Flow logic as shown below:
*PROCESS BEFORE OUTPUT.
*  MODULE STATUS_0100.
**
*PROCESS AFTER INPUT.
*  MODULE USER_COMMAND_0100.

END-OF-SELECTION.

*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  SET PF-STATUS 'STATUS_0100'.
*  SET TITLEBAR 'xxx'.


ENDMODULE.                 " STATUS_0100  OUTPUT

*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.

  CASE gd_okcode.
    WHEN 'BACK' OR
         'END'  OR
         'CANC'.
      SET SCREEN 0. LEAVE SCREEN.


    WHEN OTHERS.
  ENDCASE.

  CLEAR: gd_okcode.

ENDMODULE.                 " USER_COMMAND_0100  INPUT


*&---------------------------------------------------------------------*
*&      Form  BUILD_FIELDCATALOG_KNB1
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM build_fieldcatalog_knb1 .
* define local data
  DATA:
    ls_fcat        TYPE lvc_s_fcat.

  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
*     I_BUFFER_ACTIVE              =
      i_structure_name             = 'KNB1'
*     I_CLIENT_NEVER_DISPLAY       = 'X'
*     I_BYPASSING_BUFFER           =
*     I_INTERNAL_TABNAME           =
    CHANGING
      ct_fieldcat                  = gt_fcat
    EXCEPTIONS
      inconsistent_interface       = 1
      program_error                = 2
      OTHERS                       = 3.
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


*  LOOP AT gt_fcat INTO ls_fcat
*          WHERE ( fieldname = 'KUNNR'  OR
*                  fieldname = 'ERNAM' ).
*    ls_fcat-hotspot = abap_true.
*    MODIFY gt_fcat FROM ls_fcat.
*  ENDLOOP.


ENDFORM.                    " BUILD_FIELDCATALOG_KNB1

Regards

Uwe

anversha_s
Active Contributor
0 Kudos

hi,

chk this.

<b>http://www.sapdevelopment.co.uk/reporting/alv/alvgrid_pfstatus.htm</b>

Regards

Anver

Former Member
0 Kudos

thank to all