REUSE_ALV_FIELDCATALOG_MERGE根据结构名称创建GT_FIELDCAT

源代码:

*&---------------------------------------------------------------------*
*& Report  YMYYT9012
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ymyyt9012.
TABLES:ymyyt9012.
DATA:gt_ymyyt9012 TYPE TABLE OF ymyyt9012,
     gt_fieldcat  TYPE          slis_t_fieldcat_alv,
     gs_layout    TYPE          slis_layout_alv,
     g_title      TYPE          lvc_title,
     l_num        TYPE          i.
FIELD-SYMBOLS:<fs_fieldcat> TYPE slis_fieldcat_alv.

SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN: COMMENT 1(18) text-c01 FOR FIELD s_kstar  .
SELECT-OPTIONS: s_kstar FOR ymyyt9012-kstar.
SELECTION-SCREEN: END OF LINE.

SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN: COMMENT 1(18) text-c02 FOR FIELD s_bkbs .
SELECT-OPTIONS: s_bkbs FOR ymyyt9012-bkbs.
SELECTION-SCREEN: END OF LINE.

SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN: COMMENT 1(18) text-c03 FOR FIELD s_gjahr .
SELECT-OPTIONS: s_gjahr FOR ymyyt9012-gjahr.
SELECTION-SCREEN: END OF LINE.

SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN: COMMENT 1(18) text-c04 FOR FIELD s_bukrs .
SELECT-OPTIONS: s_bukrs FOR ymyyt9012-bukrs.
SELECTION-SCREEN: END OF LINE.

SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN: COMMENT 1(18) text-c05 FOR FIELD s_monat .
SELECT-OPTIONS: s_monat FOR ymyyt9012-monat.
SELECTION-SCREEN: END OF LINE.

*SELECTION-SCREEN: BEGIN OF LINE.
*SELECTION-SCREEN: COMMENT 1(18) text-c06 FOR FIELD s_posnr .
*SELECT-OPTIONS: s_posnr FOR ymyyt9012-posnr.
*SELECTION-SCREEN: END OF LINE.

SELECTION-SCREEN: BEGIN OF LINE.
SELECTION-SCREEN: COMMENT 1(18) text-c07 FOR FIELD s_belnr .
SELECT-OPTIONS: s_belnr FOR ymyyt9012-belnr.
SELECTION-SCREEN: END OF LINE.

START-OF-SELECTION .
  REFRESH gt_ymyyt9012.
  SELECT *
    INTO TABLE gt_ymyyt9012
    FROM ymyyt9012
  WHERE kstar IN s_kstar
    AND bkbs IN s_bkbs
    AND gjahr IN s_gjahr
    AND bukrs IN s_bukrs
    AND monat IN s_monat
*    AND posnr IN s_posnr
    AND belnr IN s_belnr.

  REFRESH gt_fieldcat[].
  CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
    EXPORTING
      i_program_name         = sy-repid
      i_structure_name       = 'YMYYT9012'
    CHANGING
      ct_fieldcat            = gt_fieldcat
    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_fieldcat ASSIGNING <fs_fieldcat>.
    CASE <fs_fieldcat>-fieldname.
      WHEN 'ZFIELD1'.
        <fs_fieldcat>-seltext_l = '预留字段1'.
      WHEN 'ZFIELD2'.
        <fs_fieldcat>-seltext_l = '预留字段2'.
      WHEN 'ZFIELD3'.
        <fs_fieldcat>-seltext_l = '预留字段3'.
      WHEN 'ZFIELD4'.
        <fs_fieldcat>-seltext_l = '预留字段4'.
      WHEN 'ZFIELD5'.
        <fs_fieldcat>-seltext_l = '预留字段5'.
      WHEN OTHERS.
    ENDCASE.
  ENDLOOP.

  gs_layout-colwidth_optimize = 'X'.
  CLEAR:g_title.
  l_num = lines( gt_ymyyt9012 ).
  g_title = '共查询到:' && l_num && '条数据'.
  IF gt_ymyyt9012 IS INITIAL.
    MESSAGE s000(ymyy) WITH '没有查询到符合条件的数据!' DISPLAY LIKE 'S'.
  ENDIF.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = sy-repid
      i_grid_title       = g_title "ALV显示标题
      is_layout          = gs_layout
      it_fieldcat        = gt_fieldcat[]
    TABLES
      t_outtab           = gt_ymyyt9012
    EXCEPTIONS
      program_error      = 1
      OTHERS             = 2.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
上一篇:生产者消费者阻塞队列版


下一篇:tf.Variable、tf.get_variable、tf.variable_scope以及tf.name_scope之间的关系