SAP ALV合并单元格示例(合并单元格为表头)

前言

  • 实现参考Enno Wulff的文章(德文):Zellen verbinden
  • 在原有基础上新增了部分功能(支持输入合并单元格的显示值)

效果

SAP ALV合并单元格示例(合并单元格为表头)_第1张图片

代码

  • 主程序

    *&---------------------------------------------------------------------*
    * TABLES 申明表类型工作区/Work area for declaring table object types
    *&---------------------------------------------------------------------*
    
    
    *&---------------------------------------------------------------------*
    * TYPE-POOL 引入类型池/Introduce type pool
    *&---------------------------------------------------------------------*
    
    
    *&---------------------------------------------------------------------*
    * TYPES 定义数据类型/defines a standalone data type
    *&---------------------------------------------------------------------*
    TYPES: BEGIN OF t_check_styles,
             matnr TYPE mara-matnr,
             ersda  TYPE mara-ersda,
             ernam  TYPE mara-ernam,
             laeda  TYPE mara-laeda,
             aenam  TYPE mara-aenam,
             vpsta  TYPE mara-vpsta,
             pstat  TYPE mara-pstat,
             lvorm  TYPE mara-lvorm,
             mtart  TYPE mara-mtart,
             mbrsh  TYPE mara-mbrsh,
             matkl  TYPE mara-matkl,
             bismt  TYPE mara-bismt,
    END OF t_check_styles.
    
    *&---------------------------------------------------------------------*
    * CONSTANTS 申明常量/declares a constant data object
    *&---------------------------------------------------------------------*
    
    
    *&---------------------------------------------------------------------*
    * DATA 申明全局变量/declares a variable of any data type
    * Declaration type:var, ref, struc, comp, itab, and range_tab
    *&---------------------------------------------------------------------*
    
    INCLUDE <cl_alv_control>.
    INCLUDE <icon>.
    
    DATA ok_code TYPE sy-ucomm.
    DATA gt_main TYPE TABLE OF t_check_styles.
    
    FIELD-SYMBOLS <fs_styles>        TYPE t_check_styles.
    DATA : lt_fieldcatalog  TYPE lvc_t_fcat.
    
    FIELD-SYMBOLS  <fs_cat> TYPE lvc_s_fcat.
    
    CLASS zcl_gui_alv_grid DEFINITION DEFERRED .
    DATA it_col_merge        TYPE lvc_t_co01.
    DATA wa_col_merge        TYPE lvc_s_co01.
    DATA: g_custom_container TYPE REF TO cl_gui_custom_container.
    DATA  g_alv_grid         TYPE REF TO zcl_gui_alv_grid.
    CLASS cl_gui_cfw DEFINITION LOAD.
    
    DATA: x_save,                     "for Parameter I_SAVE
          gs_variant TYPE disvariant. "for parameter IS_VARIANT
    DATA gs_layout TYPE lvc_s_layo.   " Layout
    DATA wa_style  TYPE lvc_s_styl.
    
    *&---------------------------------------------------------------------*
    * DEFINE 定义全局宏/Define global macro
    *&---------------------------------------------------------------------*
    
    
    *&---------------------------------------------------------------------*
    * CLASS DEFINITION 定义类/Definition class
    *&---------------------------------------------------------------------*
    CLASS zcl_gui_alv_grid DEFINITION INHERITING FROM cl_gui_alv_grid.
    
      "Implement CL GUI ALV GRID method
      PUBLIC SECTION.
        "Horizontal merge method
        METHODS z_set_merge_horiz
          IMPORTING
            row           TYPE i
            value         TYPE lvc_value
          CHANGING
            tab_col_merge TYPE lvc_t_co01.
        "Vertical merge method
        METHODS z_set_merge_vert
          IMPORTING
            row           TYPE i
            value         TYPE lvc_value
          CHANGING
            tab_col_merge TYPE lvc_t_co01.
    
        METHODS z_set_header_value
          IMPORTING
            row TYPE i
            col TYPE i
            value TYPE lvc_value.
    
        "Change output to CL_GUI_ALV_GRID display method
        METHODS z_display.
        "Sets the cell style method
        METHODS z_set_cell_style
          IMPORTING
            row    TYPE i OPTIONAL
            col    TYPE i OPTIONAL
            style  TYPE lvc_style
            style2 TYPE lvc_style OPTIONAL.
        "Set fixed columns/rows method
        METHODS z_set_fixed_col_row
          IMPORTING
            col TYPE i
            row TYPE i.
        "Initialize cell style method
        METHODS z_init_cell_styles.
    ENDCLASS.                    "ZCL_GUI_ALV_GRID DEFINITION
    
    *&---------------------------------------------------------------------*
    * CLASS IMPLEMENTATION 实现类/Implementation class
    *&---------------------------------------------------------------------*
    CLASS zcl_gui_alv_grid IMPLEMENTATION.
      METHOD z_set_merge_horiz.
    * ROW - row whose columns are to be merged
    * tab_col_merge - Columns to be merged
        FIELD-SYMBOLS  TYPE lvc_s_co01.
        FIELD-SYMBOLS  TYPE lvc_s_data.
        DATA outputlen TYPE i.
    
        SORT tab_col_merge.
    * Columns to merge
        LOOP AT tab_col_merge ASSIGNING .
    * data verification
          IF -col_id    LE 0.                CONTINUE. ENDIF.
          IF -outputlen LE -col_id. CONTINUE. ENDIF.
          outputlen = -outputlen - -col_id.
          LOOP AT mt_data ASSIGNING 
          WHERE row_pos = row  AND
          ( col_pos BETWEEN -col_id AND
          -outputlen ).
    * Set how far to merge From column in length
    * starting with the 1st column
            IF -col_pos = -col_id.
              -mergehoriz = outputlen.
    * with all others who belong together
    * the value has to go out because it comes from the 1st column
    * and the merge indicator must also go!
              -value = value.
            ELSE.
              CLEAR -mergehoriz.
              CLEAR -value.
            ENDIF.
          ENDLOOP.
        ENDLOOP.
      ENDMETHOD.                    "Z_SET_MERGE_HORIZ
    
      METHOD z_set_merge_vert.
    * ROW - row whose columns are to be merged
    * tab_col_merge - Columns to be merged
        FIELD-SYMBOLS <fs_cols> TYPE lvc_s_co01.
        FIELD-SYMBOLS <fs_data> TYPE lvc_s_data.
        DATA outputlen TYPE i.
    
        SORT tab_col_merge.
    * Columns to merge
        LOOP AT tab_col_merge ASSIGNING <fs_cols>.
    * data verification
          IF <fs_cols>-col_id    LE 0.                CONTINUE. ENDIF.
          IF <fs_cols>-outputlen LE <fs_cols>-col_id. CONTINUE. ENDIF.
          outputlen = <fs_cols>-outputlen - <fs_cols>-col_id.
          LOOP AT mt_data ASSIGNING 

你可能感兴趣的:(ABAP开发,ABAP,实用程序记录,ABAP,ALV)