ALV tutorial 09 – Dropdown lists

SAPExample of setting an ALV column as dropdown listbox.

REPORT  z_alv_demo_09.

TYPES: BEGIN OF ty_data.
        INCLUDE STRUCTURE sflight.
TYPES:  flight_status(2) TYPE c,
       END OF ty_data.

DATA: gt_data TYPE TABLE OF ty_data,
      g_grid TYPE REF TO cl_gui_alv_grid,
      gs_layout TYPE lvc_s_layo,
      gt_fieldcat TYPE lvc_t_fcat,
      gt_dropdown1 TYPE lvc_t_drop,                 " table with dropdown items for solution 1
      gt_dropdown2 type lvc_t_dral.                 " table with dropdown items for solution 2

CONSTANTS:
  c_max_rows TYPE i VALUE 100,
  c_solution type i value 1.                        " Comment/uncomment to test solution 1 or 2
* c_solution type i value 2.                        " Comment/uncomment to test solution 1 or 2

CALL SCREEN 100.

*&---------------------------------------------------------------------*
*&      Form  read_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM read_data.
  FIELD-SYMBOLS:
    <fs_data> TYPE ty_data.

  SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_data
    FROM sflight
    UP TO c_max_rows ROWS.

  LOOP AT gt_data ASSIGNING <fs_data>.
    IF <fs_data>-fldate > sy-datum.
      <fs_data>-flight_status = '01'.
    ELSE.
      <fs_data>-flight_status = '02'.
    ENDIF.
  ENDLOOP.
ENDFORM.                    "read_data

*&---------------------------------------------------------------------*
*&      Form  display_grid
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM display_grid.

  CREATE OBJECT g_grid
    EXPORTING
      i_parent = cl_gui_container=>default_screen.

  PERFORM get_layout.
  PERFORM get_fieldcat.
  PERFORM get_dropdowns.

  IF c_solution = 1.
    CALL METHOD g_grid->set_drop_down_table
      EXPORTING
        it_drop_down = gt_dropdown1.
  ELSEIF c_solution = 2.
    CALL METHOD g_grid->set_drop_down_table
      EXPORTING
        it_drop_down_alias = gt_dropdown2.
  ENDIF.

*method to display the dropdown in ALV
  CALL METHOD g_grid->set_table_for_first_display
    EXPORTING
      i_structure_name              = 'SFLIGHT'
      is_layout                     = gs_layout
    CHANGING
      it_outtab                     = gt_data
      it_fieldcatalog               = gt_fieldcat
    EXCEPTIONS
      invalid_parameter_combination = 1
      program_error                 = 2
      too_many_lines                = 3
      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.
ENDFORM.                    "display_grid
*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  SET PF-STATUS 'MAIN'.
  SET TITLEBAR 'ALV_EXAMPLES'.
ENDMODULE.                 " STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  DISPLAY_GRID  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE display_grid OUTPUT.
  PERFORM read_data.
  PERFORM display_grid.
ENDMODULE.                 " DISPLAY_GRID  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
*   to react on oi_custom_events:
  CALL METHOD cl_gui_cfw=>dispatch.
  CASE sy-ucomm.
    WHEN 'BACK' OR
         'EXIT' OR
         'CANCEL'.
      LEAVE PROGRAM.
    WHEN OTHERS.
*     do nothing
  ENDCASE.
ENDMODULE.                 " USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*&      Form  GET_LAYOUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*    p1        text
*   TYPE lvc_s_fcat.

  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
      i_structure_name       = 'SFLIGHT'
    CHANGING
      ct_fieldcat            = gt_fieldcat
    EXCEPTIONS
      inconsistent_interface = 1
      program_error          = 2
      OTHERS                 = 3.
  CHECK sy-subrc = 0.

  APPEND INITIAL LINE TO gt_fieldcat ASSIGNING <fs_fcat>.
  <fs_fcat>-fieldname = 'FLIGHT_STATUS'.
  <fs_fcat>-drdn_hndl = '1'.
  IF c_solution = 2.
    <fs_fcat>-drdn_alias = 'X'.
  ENDIF.
  <fs_fcat>-outputlen = 15.
ENDFORM.                    " GET_FIELDCAT
*&---------------------------------------------------------------------*
*&      Form  GET_DROPDOWNS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*   TYPE lvc_s_drop,
    <fs_drop2> TYPE lvc_s_dral.

* Listbox with flight statuses (handle '1').
  IF c_solution = 1.
    APPEND INITIAL LINE TO gt_dropdown1 ASSIGNING <fs_drop1>.
    <fs_drop1>-handle = '1'.
    <fs_drop1>-value = '01 Scheduled'.

    APPEND INITIAL LINE TO gt_dropdown1 ASSIGNING <fs_drop1>.
    <fs_drop1>-handle = '1'.
    <fs_drop1>-value = '02 On-Time'.

    APPEND INITIAL LINE TO gt_dropdown1 ASSIGNING <fs_drop1>.
    <fs_drop1>-handle = '1'.
    <fs_drop1>-value = '03 Delayed'.
  ELSEIF c_solution = 2.
    APPEND INITIAL LINE TO gt_dropdown2 ASSIGNING <fs_drop2>.
    <fs_drop2>-handle = '1'.
    <fs_drop2>-int_value = '01'.
    <fs_drop2>-value = 'Scheduled'.

    APPEND INITIAL LINE TO gt_dropdown2 ASSIGNING <fs_drop2>.
    <fs_drop2>-handle = '1'.
    <fs_drop2>-int_value = '02'.
    <fs_drop2>-value = 'On-Time'.

    APPEND INITIAL LINE TO gt_dropdown2 ASSIGNING <fs_drop2>.
    <fs_drop2>-handle = '1'.
    <fs_drop2>-int_value = '03'.
    <fs_drop2>-value = 'Delayed'.
  ENDIF.

ENDFORM.                    " GET_DROPDOWNS

Leave a Reply