在前面的博文中,已经讲了封装的思路和实现,主要是利用 cl_salv_data_descr=>read_structdescr () 方法来实现。在这里,贴出代码方便大家参考。
编写获取内表组件的通用方法
form frm_get_fields using pt_data type any table
changing pt_fields type ddfields.
data: lr_tabdescr type ref to cl_abap_structdescr,
lr_data type ref to data,
lt_fields type ddfields.
create data lr_data like line of pt_data.
lr_tabdescr ?= cl_abap_structdescr=>describe_by_data_ref( lr_data ).
lt_fields = cl_salv_data_descr=>read_structdescr( lr_tabdescr ).
pt_fields = lt_fields.
endform.
编写函数获取FieldCatalog
function z_falv_field_catalog.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(IT_OUTPUT) TYPE ANY TABLE
*" TABLES
*" FIELD_CATALOG TYPE SLIS_T_FIELDCAT_ALV
*"----------------------------------------------------------------------
data: lt_ddfields type ddfields,
ls_fields type dfies.
data: ls_fieldcat type slis_fieldcat_alv.
data: lt_fieldcat type slis_t_fieldcat_alv.
perform frm_get_fields using it_output[] changing lt_ddfields[].
loop at lt_ddfields into ls_fields.
move-corresponding ls_fields to ls_fieldcat.
append ls_fieldcat to lt_fieldcat.
clear ls_fieldcat.
endloop.
append lines of lt_fieldcat to field_catalog.
endfunction.