ABAP OO ALV模板(Docking 容器)

前言

来了新入职的开发同事,因为之前双方都来自不同的乙方公司,所以开发习惯上有挺大的差异
我之前的公司是使用的LVC函数进行的报表开发,另一位同事是公司是使用的OO ALV进行开发,借此机会拓展一下知识面,再补补报表开发的基础
感觉Docking容器的应用应该比Custome Control来的多一些,毕竟不需要在屏幕上固定划出一块区域作为ALV展示的自定义容器,用户还能通过拖拽自定义容器的大小
开发步骤如下:

新建屏幕

开发Docking 容器的OO ALV只需要新建一个屏幕即可
ABAP OO ALV模板(Docking 容器)_第1张图片
OO ALV中常用的是ALV的tool bar,所以会把应用程序工具栏隐藏掉,勾选【无应用程序工具栏】即可
ABAP OO ALV模板(Docking 容器)_第2张图片
在屏幕流逻辑中新建module
ABAP OO ALV模板(Docking 容器)_第3张图片

新建GUI 状态 & GUI 标题

新建的GUI状态是空白的,标准工具栏的功能大多数都没有,至少把退出的几个功能先实现出来,否则用户进入ALV之后就无法退出
ABAP OO ALV模板(Docking 容器)_第4张图片
新建GUI标题,代码中可以用占位符&填充自定义文本
ABAP OO ALV模板(Docking 容器)_第5张图片

代码实现

********************************************************************
* 事务代码:                                                       *
* 程序名称:                                                       *
* 程序目的:OO ALV Docking容器                                     *
* 设 计 人:                                                       *
* 开 发 人:                                                       *
* 设计时间:2023-05-30                                             *
* 程序类型: ABAP/4 程序 ,报表程序                                 *
* 应用类型: XX                                                     *
* 描    述:                                                        *
*(修改日志)--------------------------------------------------------*
*                                                                  *
* 日志号   修改人  修改时间       修改说明              传输号码   *
*  ----    ----    ------         -----------
*   001    Seele   2023-05-29     创建报表程序                     *
********************************************************************

REPORT zdemo_seele10.
*----------------------------------------------------------------------*
* 数据库表声明/Database table declaration
*----------------------------------------------------------------------*
TABLES:mkpf,  " 物料凭证抬头
       mara,
       mseg.  " 物料凭证行项目

*----------------------------------------------------------------------*
* 结构声明类型/Structure type declaration
*----------------------------------------------------------------------*
*&---主表数据/master table data
TYPES:BEGIN OF ty_output,

        matnr TYPE makt-matnr           , " 物料
        maktx TYPE makt-maktx           , " 物料描述
        spras TYPE makt-spras           , " 语言代码

        box   TYPE c                    , " 选择框

      END OF ty_output.

*----------------------------------------------------------------------*
* 全局变量定义/Global variable definition
*----------------------------------------------------------------------*
*&---全局内表定义
DATA:gt_output TYPE TABLE OF ty_output.  " 主数据表

*&---全局结构定义
DATA:gs_output TYPE ty_output.           " 主数据结构

*&---全局变量定义
DATA:gv_lines TYPE string.  " 统计行数

*&---------------------------------------------------------------------*
*& 字段串定义/Field-symbols                                            *
*&---------------------------------------------------------------------*
FIELD-SYMBOLS: TYPE ty_output.

*&---------------------------------------------------------------------*
*& 类定义/Class                                                        *
*&---------------------------------------------------------------------*
CLASS cl_events DEFINITION.
  PUBLIC SECTION.
    " ALV工具栏按钮
    METHODS: handle_toolbar       FOR EVENT toolbar      OF cl_gui_alv_grid
               IMPORTING e_object e_interactive,
             handle_user_command  FOR EVENT user_command OF cl_gui_alv_grid
               IMPORTING e_ucomm.

ENDCLASS.

*&---------------------------------------------------------------------*
*& 类实现/Class Implementation                                                        *
*&---------------------------------------------------------------------*
CLASS cl_events IMPLEMENTATION.
  " ALV工具栏按钮
  METHOD handle_toolbar.
    PERFORM frm_handle_toolbar CHANGING e_object e_interactive.
  ENDMETHOD.
  " ALV工具栏事件
  METHOD handle_user_command.
    PERFORM frm_handle_command CHANGING e_ucomm.
  ENDMETHOD.

ENDCLASS.

*&---------------------------------------------------------------------*
*& 对象定义/Object                                                     *
*&---------------------------------------------------------------------*
DATA: go_events TYPE REF TO cl_events,
      go_alv    TYPE REF TO cl_gui_alv_grid,
      go_dctn   TYPE REF TO cl_gui_docking_container.

*&---------------------------------------------------------------------*
*&  ALV TYPE/ALV 类型定义
*&---------------------------------------------------------------------*
*&---ALV数据组,类型池
TYPE-POOLS:slis,
           vrm.

*&---定义ALV显示的字段列及其描述等属性
DATA:gt_fieldcat TYPE TABLE OF lvc_s_fcat, " ALV 控制: 字段目录
     gs_fieldcat TYPE lvc_s_fcat,          " ALV 控制: 字段目录
     gs_layout   TYPE lvc_s_layo.          " ALV 控制: 布局结构

*&---------------------------------------------------------------------*
*&  Macro 宏定义
*&---------------------------------------------------------------------*
DEFINE mcr_set_catalog.
  gs_fieldcat-fieldname  = &1. " 字段技术名称
  gs_fieldcat-coltext    = &2. " 显示名称
  gs_fieldcat-ref_table  = &3. " 参照表
  gs_fieldcat-ref_field  = &4. " 参照表字段
  gs_fieldcat-qfieldname = &5. " 参考计量单位的字段名称
  gs_fieldcat-key        = &6. " key 值
  gs_fieldcat-edit       = &7. " 可编辑

  APPEND gs_fieldcat TO gt_fieldcat.
  CLEAR  gs_fieldcat.
END-OF-DEFINITION.

*&---------------------------------------------------------------------*
*& Selection Screen/选择屏幕
*&---------------------------------------------------------------------*
*&---选择屏幕块
SELECTION-SCREEN BEGIN OF BLOCK blk01 WITH FRAME TITLE TEXT-101.

*&---范围
SELECT-OPTIONS:s_matnr FOR mara-matnr .            " 物料凭证

SELECTION-SCREEN END OF BLOCK blk01.

*&---------------------------------------------------------------------*
*& Start-of-selection/开始选择屏幕                                     *
*&---------------------------------------------------------------------*
START-OF-SELECTION.
*&---内表数据获取

  PERFORM frm_get_data.

  IF gt_output IS INITIAL.
    MESSAGE s000(zmm001) DISPLAY LIKE 'E'.
    EXIT.
  ENDIF.

*&---------------------------------------------------------------------*
*& end-of-selection/结束选择屏幕(程序结束处理,输出等)               *
*&---------------------------------------------------------------------*
END-OF-SELECTION.

  CALL SCREEN '9001'.

*&---------------------------------------------------------------------*
*&      Form  frm_get_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_10     text
*----------------------------------------------------------------------*
FORM frm_get_data.

  DATA:lv_tabix TYPE i,
       lv_atinn TYPE ausp-atinn.

  FREE:gt_output.
  SELECT
    matnr,  " 物料编码
    maktx,  " 物料描述
    spras   " 语言代码
  FROM makt
  WHERE
    matnr IN @s_matnr
  INTO CORRESPONDING FIELDS OF TABLE @gt_output
  UP TO 100 ROWS.

  gv_lines = lines( gt_output ).
  gv_lines = gv_lines && TEXT-t01.

ENDFORM.                    " frm_get_data

*&---------------------------------------------------------------------*
*& Form FRM_INIT_SCREEN_9001
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_init_screen_9001 .
*&===ALV 输出
*&---设置ALV对象
  PERFORM frm_init_object.
*&---注册ALV事件
  PERFORM frm_register_event.
*&---设置ALV输出格式
  PERFORM frm_init_layout.
*&---设置ALV输出字段
  PERFORM frm_set_fieldcat.
*&---ALV 显示
  PERFORM frm_display_alv.
ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  frm_init_object
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_init_object .

  " 容器对象
  CREATE OBJECT go_dctn
    EXPORTING
      repid     = sy-repid      " 当前程序名
      dynnr     = sy-dynnr      " 当前屏幕号
      extension = 2000.   " 当前长宽

  " GRID对象
  CREATE OBJECT go_alv
    EXPORTING
      i_parent = go_dctn.

  " 事件对象
  CREATE OBJECT go_events.

ENDFORM.                    " frm_init_object

*&---------------------------------------------------------------------*
*& Form FRM_REGISTER_EVENT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_register_event .
  SET HANDLER go_events->handle_toolbar      FOR go_alv.
  SET HANDLER go_events->handle_user_command FOR go_alv.
ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  frm_init_layout
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_init_layout .
  CLEAR gs_layout.
  gs_layout-zebra      = 'X' .    " 斑马线
  gs_layout-cwidth_opt = 'X' .    " 自动调整ALVL列宽
*  gs_layout-no_toolbar = 'X' .    " 隐藏工具栏
  gs_layout-sel_mode   = 'A'.     "选择模式
  gs_layout-box_fname  = 'BOX'.   "选择字段
ENDFORM.                    " frm_init_layout
*&---------------------------------------------------------------------*
*&      Form  frm_fieldcat
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_set_fieldcat .
*&---ALV 输出字段目录,涉及字段必须大写
  mcr_set_catalog: 'MATNR'       TEXT-a01    'MAKT'      'MATNR'       '' ''  '', " 物料
                   'MAKTX'       TEXT-a02    'MAKT'      'MAKTX'       '' ''  '', " 物料描述
                   'SPRAS'       TEXT-a03    'MAKT'      'SPRAS'       '' ''  ''. " 语言代码


ENDFORM.                    " frm_fieldcat

*&---------------------------------------------------------------------*
*&      Form  frm_display_alv
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_display_alv .
*&---ALV 显示函数
  IF go_alv IS BOUND.
    CALL METHOD go_alv->set_table_for_first_display
      EXPORTING
*        is_variant      = ls_variant
        is_layout       = gs_layout
        i_save          = 'A'
        i_default       = 'X'
      CHANGING
        it_outtab       = gt_output
        it_fieldcatalog = gt_fieldcat.
  ENDIF.
  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.                    " frm_display_alv
*&---------------------------------------------------------------------*

*&---------------------------------------------------------------------*
*& Form FRM_HANDLE_TOOLBAR
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      <-- E_OBJECT
*&      <-- E_INTERACTIVE
*&---------------------------------------------------------------------*
FORM frm_handle_toolbar  CHANGING pe_object       TYPE REF TO cl_alv_event_toolbar_set
                                  pe_interactive  .
  DATA ls_toolbar TYPE stb_button.

  " 设置竖线以区分按钮区域
  ls_toolbar-butn_type = 3.
  APPEND ls_toolbar TO pe_object->mt_toolbar.
  CLEAR ls_toolbar.

  " 自定义功能按钮
  ls_toolbar = VALUE #( function  = 'PRT'
                        icon      = icon_print
                        text      = '打印'
                        quickinfo = 'Print' ).
  APPEND ls_toolbar TO pe_object->mt_toolbar.

ENDFORM.

*&---------------------------------------------------------------------*
*& Form FRM_USER_COMMAND_9001
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_user_command_9001 .

  CASE sy-ucomm.
    WHEN 'BACK'.
      LEAVE TO SCREEN 0.
    WHEN 'EXIT'.
      LEAVE PROGRAM.
    WHEN OTHERS.
  ENDCASE.

ENDFORM.

*&---------------------------------------------------------------------*
*& Form FRM_HANDLE_COMMAND
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      <-- E_UCOMM
*&---------------------------------------------------------------------*
FORM frm_handle_command  CHANGING pe_ucomm.
  CASE pe_ucomm.
    WHEN 'PRT'.
      MESSAGE '点击打印' TYPE 'S'.
    WHEN OTHERS.
  ENDCASE.
ENDFORM.

*&---------------------------------------------------------------------*
*& Module STATUS_9001 OUTPUT
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
MODULE status_9001 OUTPUT.
  SET PF-STATUS 'STATUS_9001'.
  SET TITLEBAR 'TITLE_9001' WITH gv_lines.
  PERFORM frm_init_screen_9001.
ENDMODULE.

*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_9001  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_9001 INPUT.
  PERFORM frm_user_command_9001.
ENDMODULE.

效果图

ABAP OO ALV模板(Docking 容器)_第6张图片
参考文档:大佬夜猫君的博客

你可能感兴趣的:(模板,SAP)