因根据查询条件展示的报表列数不一定一致,因此采用动态内表的方式进行处理
这里写了一个简单的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