首页 > 其他分享 >ABAP 动态内表

ABAP 动态内表

时间:2022-10-17 10:56:48浏览次数:139  
标签:内表 ty text item ABAP alv 动态 TYPE &------------------------------------------

因根据查询条件展示的报表列数不一定一致,因此采用动态内表的方式进行处理
这里写了一个简单的Demo记录一下
效果如下图:

一般来说通过选择屏幕来控制列的,我这里简单了,直接对内表赋予相关值,包括相关指标的数据,都是手动赋值的,一般来说都是通过数据库表取值在赋值的,这里根据业务情况的不同需要做出改变,代码相关如下:

REPORT y_test_demo01.

TYPES: BEGIN OF ty_alv,
         item   TYPE char6,
         target TYPE char30,
       END OF ty_alv.

TYPES: BEGIN OF ty_item,
         item  TYPE char6,  
         ittxt TYPE char20,  
       END OF ty_item.

DATA: gt_item TYPE STANDARD TABLE OF ty_item,
      gt_alv  TYPE STANDARD TABLE OF ty_alv,
      ls_item TYPE ty_item,
      ls_alv  TYPE ty_alv.
*&---Field-symbols
FIELD-SYMBOLS: <dyn_alv> TYPE STANDARD TABLE.

*&---ALV Object
DATA: gt_fieldcat TYPE lvc_t_fcat,
      gs_layout   TYPE lvc_s_layo.

*&---------------------------------------------------------------------*
*& INITIALIZATION/选择屏幕前初始化(选择屏幕展示之前执行)
*&---------------------------------------------------------------------*
INITIALIZATION .

*&---------------------------------------------------------------------*
*& AT SELECTION-SCREEN OUTPUT/选择屏幕控制(第二屏输出)
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN OUTPUT .

*&---------------------------------------------------------------------*
*& AT SELECTION-SCREEN/选择屏幕执行(参数输入检查)
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN .

*&---------------------------------------------------------------------*
*& START-OF-SELECTION/开始选择屏幕(点击执行按钮之后执行的语块)
*&---------------------------------------------------------------------*
START-OF-SELECTION .
  PERFORM frm_deal_data.  " 处理数据

*&---------------------------------------------------------------------*
*& END-OF-SELECTION/结束选择屏幕(程序结束处理,输出等)
*&---------------------------------------------------------------------*
END-OF-SELECTION .
  PERFORM frm_display_alv.  " 展示报表

*&---------------------------------------------------------------------*
*& Form frm_deal_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_deal_data .
  " 主数据填值
  ls_alv-item = '010'.
  ls_alv-target = '十'.
  APPEND ls_alv TO gt_alv.

  ls_alv-item = '020'.
  ls_alv-target = '二十'.
  APPEND ls_alv TO gt_alv.

  ls_alv-item = '030'.
  ls_alv-target = '三十'.
  APPEND ls_alv TO gt_alv.

  ls_alv-item = '040'.
  ls_alv-target = '四十'.
  APPEND ls_alv TO gt_alv.

  ls_alv-item = '050'.
  ls_alv-target = '五十'.
  APPEND ls_alv TO gt_alv.

  "填值 限定展示列的 内表  通过对这张表的 控制 来决定列的数量
  ls_item-item = '010'.
  ls_item-ittxt = '表头一'.
  APPEND ls_item TO gt_item.

*  ls_item-item = '020'.
*  ls_item-ittxt = '表头二'.
*  APPEND ls_item TO gt_item.

  ls_item-item = '030'.
  ls_item-ittxt = '表头三'.
  APPEND ls_item TO gt_item.

*  ls_item-item = '040'.
*  ls_item-ittxt = '表头四'.
*  APPEND ls_item TO gt_item.

  ls_item-item = '050'.
  ls_item-ittxt = '表头五'.
  APPEND ls_item TO gt_item.

ENDFORM.

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

  PERFORM frm_set_dynamic_table.  " 设置动态内表
  PERFORM frm_fill_dyntab.  " 填充动态内表
  PERFORM frm_set_layout.  "设置ALV输出格式
  PERFORM frm_call_alv TABLES <dyn_alv>.  "调用ALV函数

ENDFORM.

*&---------------------------------------------------------------------*
*& Form frm_set_dynamic_table
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_set_dynamic_table .
  DEFINE _set_fieldcat.
    APPEND VALUE #(
      fieldname = &1
      ref_table = &2
      ref_field = &3
      coltext = &4
      scrtext_l = &4
      scrtext_m = &4
      scrtext_s = &4
    ) TO gt_fieldcat.
  END-OF-DEFINITION.


  _set_fieldcat 'TARGET' '' '' '指标'.  "
  LOOP AT gt_item INTO DATA(ls_item).
    _set_fieldcat ls_item-item '' '' ls_item-ittxt.
  ENDLOOP.

  " 根据字段目录创建动态内表
  DATA: dyn_table TYPE REF TO data.        " 指向任意类型
  CALL METHOD cl_alv_table_create=>create_dynamic_table   
    EXPORTING
      it_fieldcatalog = gt_fieldcat
    IMPORTING
      ep_table        = dyn_table.

  " 创建动态内表
  ASSIGN dyn_table->* TO <dyn_alv>.
ENDFORM.

*&---------------------------------------------------------------------*
*& Form frm_fill_dyntab
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_fill_dyntab .
  DATA: lv_num TYPE i,
        lv_sum TYPE i.

  LOOP AT gt_alv INTO DATA(ls_alv).

    DEFINE _set_value.
      IF sy-subrc EQ 0 AND <fs_value> IS ASSIGNED.
        <fs_value> = &1.
        UNASSIGN <fs_value>.
      ENDIF.
    END-OF-DEFINITION.

    APPEND INITIAL LINE TO <dyn_alv> ASSIGNING FIELD-SYMBOL(<fs_alv>).

    " 固定列 填值
    ASSIGN COMPONENT 'TARGET' OF STRUCTURE <fs_alv> TO FIELD-SYMBOL(<fs_value>).
    _set_value: ls_alv-target.

    " 动态列 填值
    LOOP AT gt_item INTO DATA(ls_item).
      lv_num = lv_num + 1.
      lv_sum = lv_num * 10.
      ASSIGN COMPONENT ls_item-item OF STRUCTURE <fs_alv> TO <fs_value>.
      _set_value: lv_sum.

    ENDLOOP.

  ENDLOOP.

ENDFORM.

*&---------------------------------------------------------------------*
*& Form frm_set_layout
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_set_layout .
  gs_layout = VALUE #( zebra = abap_on
                       cwidth_opt = abap_on ).
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_call_alv
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      --> <DYN_ALV>
*&---------------------------------------------------------------------*
FORM frm_call_alv
  TABLES
    pt_outtab TYPE table.

  IF pt_outtab[] IS INITIAL.
    MESSAGE s373(g0) DISPLAY LIKE 'E'.  " 无数据记录存在
    LEAVE LIST-PROCESSING.
  ENDIF.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_callback_program = sy-repid
*     i_callback_user_command  = 'USER_COMMAND'
*     i_callback_pf_status_set = 'SET_PF_STATUS'
      is_layout_lvc      = gs_layout
      it_fieldcat_lvc    = gt_fieldcat
      i_save             = 'A'
    TABLES
      t_outtab           = pt_outtab[]
    EXCEPTIONS
      program_error      = 1
      OTHERS             = 2.

ENDFORM.

标签:内表,ty,text,item,ABAP,alv,动态,TYPE,&------------------------------------------
From: https://www.cnblogs.com/pnj-owowa/p/16797976.html

相关文章

  • 动态加载style标签改变打印方向
    注意innerHTM必须换行书写exportfunctionsetStyle(direction){conststyleNode=document.createElement('style')styleNode.setAttribute('id','print-style......
  • 【Vue】vue 动态设置 src 地址
    <imgclass="logo-srkg":src="getImgUrl('hwtm.png')"alt=""/>constgetImgUrl=(src:string):string=>{returnnewURL(......
  • 动态规划 完全背包问题
     完全背包问题与简单背包问题最为明显的区别就是,每件物品可以无限次使用,也就是说,你的背包可以只装这一件物品下面附上题解/*f[g]代表当背包空间为g的时候,背包的最......
  • elementUI 如何动态的给 el-tree 添加子节点数据 children
    elementUI如何动态的给el-tree添加子节点数据children一、需求有这样一个数据结构的tree。​​element​​的tree懒加载是从根上就开始懒加载,但我需要实现的是已经......
  • (六)动态规划
    基础动态规划例\(1.1\):[NOIP2010提高组]乌龟棋-洛谷记录\(f[i][j][k][l]\)表示每种卡片用的个数即可,距离可以现算。评测记录-洛谷例\(1.2\):[HAOI2006]数字序......
  • gcc编译源码生成静态库和动态库
    函数库分为静态库和动态库两种。静态库在程序编译时会被连接到目标代码中,程序运行时将不再需要该静态库。动态库在程序编译时并不会被连接到目标代码中,而是在程序运行是才......
  • 代理模式——JDK动态代理与CGLib原理及对比分析
    1.前言首先回顾下代理模式(ProxyPattern)的定义:代理模式指为其他对象提供一种代理,以控制这个对象的访问,属于结构型设计模式。其适用于在某些情况下,一个对象不适合或者不能......
  • 动态存储管理实战之GlusterFS
    动态存储管理实战之GlusterFS一、准备工作在各个节点都安装上GFS客户端yuminstallglusterfsglusterfs-fuse-yGFS运行在k8s上需要特权模式#如果没有加上即可......
  • 动态内存分布
    在C中,不同数据在内存中分配说明: 动态内存分配的相关函数:   void*只是提供一个纯地址,不指向任何数据,如下图的*p3void指针类型: 即指针类型为void,不能用*p的......
  • leetcode1480.一维数组的动态和
    1.题目描述给你一个数组nums。数组「动态和」的计算公式为:runningSum[i]=sum(nums[0]…nums[i])。请返回nums的动态和。2.示例示例1:输入:nums=[1,2,3,4]输......