How to add Dropdown functionality in the ALV report

Vidhi ChijwaniVidhi Chijwani
2 min read

REPORT Example.
*Data Declaration
TYPES: BEGIN OF ty_ekko,
      ebeln TYPE ebeln,
      bukrs TYPE bukrs,
      ekorg TYPE ekorg,
      END OF ty_ekko.
 DATA : gt_ekko TYPE TABLE OF ty_ekko,
        gs_ekko TYPE ty_ekko.
*Custom Container
 DATA: r_container TYPE REF TO cl_gui_custom_container,
      r_grid      TYPE REF TO cl_gui_alv_grid.
*table for the dropdown data
  DATA: it_dropdown TYPE lvc_t_drop,
        ty_dropdown TYPE lvc_s_drop.
*Catalog/layout
DATA: gt_fieldcat  TYPE  lvc_t_fcat ,
      gs_fieldcat type lvc_s_fcat,
      gs_layout    TYPE lvc_s_layo,
      OK_CODE     TYPE SY-UCOMM.

START-OF-SELECTION.
CALL SCREEN 100.
MODULE status_0100 OUTPUT.
set PF-STATUS 'ZMV'.
PERFORM retrieve_data.
PERFORM create_catalog.
*Container definitions
 CREATE OBJECT r_container
  EXPORTING
  container_name    = 'CUST'.
 CREATE OBJECT r_grid
   EXPORTING
     i_parent          = r_container.
CALL METHOD r_grid->set_drop_down_table
   EXPORTING
      it_drop_down = it_dropdown.
CALL METHOD r_grid->set_table_for_first_display
  CHANGING
    it_outtab                     = gt_ekko
    it_fieldcatalog               = gt_fieldcat
  EXCEPTIONS
    invalid_parameter_combination = 1
    program_error                 = 2
    too_many_lines                = 3
    others                        = 4.
ENDMODULE.                 " STATUS_0100  OUTPUT

FORM retrieve_data.
select ebeln bukrs ekorg from ekko into table gt_ekko up to 20 rows.
ENDFORM.

FORM create_catalog.
  gs_fieldcat-fieldname  = 'EBELN'.
  gs_fieldcat-coltext = 'EBELN'.
  APPEND gs_fieldcat to gt_fieldcat.
  clear: gs_fieldcat.
   gs_fieldcat-fieldname  = 'BUKRS'.
  gs_fieldcat-coltext = 'BUKRS'.
  gs_fieldcat-edit = 'X'.
  gs_fieldcat-drdn_hndl = '2'.
  APPEND gs_fieldcat to gt_fieldcat.
  clear: gs_fieldcat.
  gs_fieldcat-fieldname  = 'EKORG'.
  gs_fieldcat-coltext = 'EKORG'.
  APPEND gs_fieldcat to gt_fieldcat.
    clear: gs_fieldcat.

PERFORM fill_dropdown.
ENDFORM.                    " CREATE_CATALOG
FORM fill_dropdown.
   ty_dropdown-handle = '2'.
   ty_dropdown-value = 'Yes'.
   APPEND ty_dropdown TO it_dropdown.
   ty_dropdown-handle = '2'.
   ty_dropdown-value = 'No'.
   APPEND ty_dropdown TO it_dropdown.

ENDFORM.   
MODULE user_command_0100 INPUT.
CASE OK_CODE.
  WHEN 'EXIT' OR 'BACK'.
    LEAVE PROGRAM.
ENDCASE. 
ENDMODULE.                 " USER_COMMAND_0100  INPUT
                 " FILL_DROPDOWN

Here in the above code, we are displaying ALV using the custom container.

To use a custom container with CL_GUI_ALV_GRID, you’ll need to work with a screen in your SAP program. This screen will serve as the area where your custom container will appear. Insert a custom control element on the screen and assign it a name, like 'CUST'. Then initiate the objects.

In setting the field catalog, set the column where the drop down list will appear. Set the parameter drdn_hndl which is included in the structure LVC_S_FCAT. So if your ALV grid will display four(4) columns and you want to display the drop down list on the second column, the value of the parameter would be drdn_hndl = 2.

We have the lvc_t_drop table. Fill in the values for the drop-down using this table, which contains two fields: handle and value**.** The handle will be 2 in our case because we are adding the dropdown for the second column. The value will be whatever you want to give.

We have the set_drop_down_table method of class cl_gui_alv_grid to set the dropdown for the column.

Then using the set_table_for_first_display method we are displaying the ALV.

12
Subscribe to my newsletter

Read articles from Vidhi Chijwani directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vidhi Chijwani
Vidhi Chijwani