动态内表相当于从一张表里捞数据,filecat是不固定的,想用什么传什么。
例子:
type-pools: slis.
data: it_fcat type slis_t_fieldcat_alv,
is_fcat like line of it_fcat.
data: it_fieldcat type lvc_t_fcat, "存储内表想要的字段
is_fieldcat like line of it_fieldcat.
data: new_table type ref to data. "引用变量,指向任意类型,但在创建时必须指定对象类型
data: new_line type ref to data.
field-symbols: <l_table> type any table,
<l_line> type any,
<l_field> type any.
call function 'REUSE_ALV_FIELDCATALOG_MERGE'
"把表SYST的字段给it_fcat[]
exporting
i_structure_name = 'SYST'
changing
ct_fieldcat = it_fcat[].
loop at it_fcat into is_fcat where not reptext_ddic is initial.
move-corresponding is_fcat to is_fieldcat.
is_fieldcat-fieldname = is_fcat-fieldname.
is_fieldcat-ref_field = is_fcat-fieldname.
is_fieldcat-ref_table = is_fcat-ref_tabname.
append is_fieldcat to it_fieldcat.
endloop.
"创建动态内表
call method cl_alv_table_create=>create_dynamic_table
exporting
it_fieldcatalog = it_fieldcat
importing
ep_table = new_table.
assign new_table->* to <l_table>.
create data new_line like line of <l_table>.
assign new_line->* to <l_line>.
do 30 times.
assign component 'SUBRC' of structure <l_line> to <l_field>.
"将<l_line>中SUBRC组件赋给<l_field>
<l_field> = sy-index.
insert <l_line> into table <l_table>.
enddo.
loop at <l_table> assigning <l_line>.
assign component 'SUBRC' of structure <l_line> to <l_field>.
write <l_field>.
endloop.
assign component 'SUBRC' of structure <l_line> to <l_field> 这句话可谓是精髓,不用考虑你的字段到底是什么,反正会给你分配
优点:由于FILECAT不是写死的,就比较灵活
缺点:个人觉得麻烦
网友评论