ABAP OOALV 實現下拉框

2021-02-24 14:24:21 浏览数 (1)

代码语言:javascript复制
DATA: gv_repid TYPE sy-repid,
      gv_sname TYPE sy-dynnr.
TYPES: BEGIN OF gs_data.
    INCLUDE STRUCTURE spfli.
TYPES:  celtab TYPE lvc_t_styl.
TYPES:  END OF gs_data.
DATA: gs_data1 TYPE gs_data.
DATA: gt_data TYPE TABLE OF gs_data WITH HEADER LINE.

DATA: g_con01  TYPE REF TO cl_gui_custom_container.

DATA: g_grid01 TYPE REF TO cl_gui_alv_grid.

DATA: gs_fieldcat TYPE lvc_s_fcat.
DATA: gt_fieldcat TYPE lvc_t_fcat.
DATA: gs_dropdowm TYPE lvc_s_drop,
      gt_dropdowm TYPE lvc_t_drop.
DATA: ok_code TYPE sy-ucomm,
      save_ok TYPE sy-ucomm.
CONSTANTS: g_cus01 TYPE scrfname VALUE 'CUS01'.
*DATA: event_receiver TYPE REF TO cl_event_receiver.

START-OF-SELECTION.
  PERFORM get_data.
  CALL SCREEN 100.

*&---------------------------------------------------------------------*
*&      Form  GET_DATA
*&---------------------------------------------------------------------*
*       text  获取数据
*----------------------------------------------------------------------*
FORM get_data.
  SELECT *
    INTO CORRESPONDING FIELDS OF TABLE gt_data
    UP TO 30 ROWS
    FROM spfli.

  gs_dropdowm-handle = '2'.
  gs_dropdowm-value  = 'drop-value1'.
  APPEND gs_dropdowm TO gt_dropdowm.

  CLEAR: gs_dropdowm.
  gs_dropdowm-handle = '2'.
  gs_dropdowm-value  = 'drop-value2'.
  APPEND gs_dropdowm TO gt_dropdowm.

ENDFORM.                    "GET_DATA

*&---------------------------------------------------------------------*
*&      Form  SET_FIELDCAT
*&---------------------------------------------------------------------*
*       text  ALV显示字段文本
*----------------------------------------------------------------------*
FORM set_fieldcat.
  CLEAR: gt_fieldcat[].
  DEFINE macro_fieldcat.
    CLEAR gs_fieldcat.
    gs_fieldcat-fieldname = &1.
    gs_fieldcat-coltext   = &2.
    gs_fieldcat-hotspot   = &3.
    IF gs_fieldcat-fieldname = 'CONNID'.
      gs_fieldcat-drdn_hndl = 2.
    ENDIF.
    APPEND gs_fieldcat TO gt_fieldcat.
  END-OF-DEFINITION.

  macro_fieldcat 'CARRID'     '航线代码'   'X'.
  macro_fieldcat 'CONNID'     '航班连接Id' ''.
  macro_fieldcat 'COUNTRYFR'  '起飞国家'   ''.
  macro_fieldcat 'CITYFROM'   '起飞城市'   ''.
  macro_fieldcat 'AIRPFROM'   '起飞机场'   ''.
  macro_fieldcat 'COUNTRYTO'  '目标国家'   ''.
  macro_fieldcat 'CITYTO'     '目标城市'   ''.
  macro_fieldcat 'AIRPTO'     '目标机场'   ''.
  macro_fieldcat 'FLTIME'     '航班时间'   ''.
  macro_fieldcat 'DEPTIME'    '启程时间'   ''.
  macro_fieldcat 'ARRTIME'    '到达时间'   ''.
  macro_fieldcat 'DISTANCE'   '距离'       ''.

ENDFORM.                    "SET_FIELDCAT

*&---------------------------------------------------------------------*
*&      Form  SET_LAYOUT
*&---------------------------------------------------------------------*
*       text  布局
*----------------------------------------------------------------------*
*      -->P_LAYOUT   text
*----------------------------------------------------------------------*
FORM set_layout CHANGING p_layout TYPE lvc_s_layo.
  p_layout-cwidth_opt = 'X'.   " 自动调节字段长度
  p_layout-sel_mode   = 'D'.   " 选择模式,可选择多行
  p_layout-zebra      = 'X'.   " 颜色交替显示
  p_layout-edit = 'X'."編輯模式
ENDFORM.                      "SET_LAYOUT

*&---------------------------------------------------------------------*
*&      Module  INIT_CON  OUTPUT
*&---------------------------------------------------------------------*
*       text  创建ALV
*----------------------------------------------------------------------*
MODULE init_con OUTPUT.

  DATA: ls_layout TYPE lvc_s_layo.

  IF g_con01 IS INITIAL.
    PERFORM set_fieldcat.
    PERFORM set_layout CHANGING ls_layout.

    " 创建SAP容器实例
    CREATE OBJECT g_con01
      EXPORTING
        container_name              = g_cus01 " 自定义控件名称
      EXCEPTIONS
        cntl_error                  = 1
        cntl_system_error           = 2
        create_error                = 3
        lifetime_error              = 4
        lifetime_dynpro_dynpro_link = 5
        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.
    " 创建GRID实例
    CREATE OBJECT g_grid01
      EXPORTING
        i_parent          = g_con01
      EXCEPTIONS
        error_cntl_create = 1
        error_cntl_init   = 2
        error_cntl_link   = 3
        error_dp_create   = 4
        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.

    CALL METHOD g_grid01->set_drop_down_table
      EXPORTING
        it_drop_down = gt_dropdowm. "添加下拉框方法到ALV中顯示

    " 调用ALV 显示方法
    CALL METHOD g_grid01->set_table_for_first_display
      EXPORTING
*       i_structure_name              = 'SPFLI' " 参照表结构字段显示
        is_layout                     = ls_layout " 布局
      CHANGING
        it_fieldcatalog               = gt_fieldcat " 显示字段
        it_outtab                     = gt_data[]
      EXCEPTIONS
        invalid_parameter_combination = 1
        program_error                 = 2
        too_many_lines                = 3
        OTHERS                        = 4.
    IF sy-subrc <> 0.
*     Implement suitable error handling here
    ENDIF.
  ENDIF.

ENDMODULE.                 " INIT_CON  OUTPUT

*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  SET PF-STATUS 'S0001'.
ENDMODULE.                 " STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_EXIT_0100  INPUT
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
MODULE user_exit_0100 INPUT.
  save_ok = ok_code.
  CLEAR ok_code.
  CASE save_ok.
    WHEN 'BACK'.
      LEAVE PROGRAM.
  ENDCASE.
ENDMODULE.                 " USER_EXIT_0100  INPUT

0 人点赞