◆◆0[代码]基于动态内表的ALV
SAP项目有会要动态显示ALV列的需求,就是ALV的列数量不固定,这是就要用到动态内表的技术,动态内表通过cl_alv_table_create=>create_dynamic_table来创建。
完整代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
REPORT ZTEST_ALV_DYNAMIC.
*-------------ALV 相关--------------------------
DATA: wa_layo TYPE lvc_s_layo.
DATA: wa_fcat TYPE lvc_s_fcat,
it_fcat TYPE lvc_t_fcat.
DATA it_scol TYPE lvc_t_scol.
DATA: g_grid TYPE REF TO cl_gui_alv_grid.
FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,
<dyn_wa>,
<dyn_field>.
DATA: dy_table TYPE REF TO data,
dy_line TYPE REF TO data.
DATA: colname(10),
index(2).
* 列数
PARAMETERS: col TYPE i.
CLEAR: it_fcat.
* 根据条件动态生成列catalog
DO col TIMES.
index = index + 1.
CONCATENATE 'COL' index INTO colname.
PERFORM frm_add_fcat USING:
colname 'C' '描述' '100'.
ENDDO.
* 根据it_fact生成动态表
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_fcat
IMPORTING
ep_table = dy_table.
ASSIGN dy_table->* TO <dyn_table>.
CREATE DATA dy_line LIKE LINE OF <dyn_table>.
ASSIGN dy_line->* TO <dyn_wa>.
* 给表字段赋值
CLEAR: index.
DO col TIMES.
index = index + 1.
CONCATENATE 'COL' index INTO colname.
ASSIGN COMPONENT colname OF STRUCTURE <dyn_wa> TO <dyn_field>.
<dyn_field> = '列数据'.
ENDDO.
APPEND <dyn_wa> TO <dyn_table>.
* 相当于给内表放入两条数据
CLEAR: index.
DO col TIMES.
index = index + 1.
CONCATENATE 'COL' index INTO colname.
ASSIGN COMPONENT colname OF STRUCTURE <dyn_wa> TO <dyn_field>.
<dyn_field> = '列数据'.
ENDDO.
APPEND <dyn_wa> TO <dyn_table>.
CLEAR: wa_layo.
wa_layo-zebra = 'X'.
wa_layo-cwidth_opt = 'X'.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
is_layout_lvc = wa_layo
it_fieldcat_lvc = it_fcat
TABLES
t_outtab = <dyn_table>
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.
*&---------------------------------------------------------------------*
*& Form frm_add_fcat
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->VALUE1 text
* -->VALUE2 text
* -->VALUE3 text
* -->VALUE4 text
*----------------------------------------------------------------------*
FORM frm_add_fcat USING value1 value2 value3 value4.
wa_fcat-fieldname = value1.
wa_fcat-inttype = value2.
wa_fcat-reptext = value3.
wa_fcat-intlen = value4.
APPEND wa_fcat TO it_fcat.
CLEAR: wa_fcat.
ENDFORM. "frm_add_fcat
|
运行结果:
选择画面上输入alv显示的列数目,
运行后,
以上。