有时候alv显示得的字段是不一定的,所以要根据某种数据来确定fieldcat的值。
所以内表也不是固定的,这时候需要定义动态指针内表,参考如下:
FORM frm_create_dynamic_table .
DATA: lo_struct TYPE REF TO cl_abap_structdescr,
lt_comp TYPE cl_abap_structdescr=>component_table,
ls_comp TYPE cl_abap_structdescr=>component,
lo_abap_desc TYPE REF TO cl_abap_typedescr,
lv_reffield TYPE string,
lr_data_line TYPE REF TO data,
lv_type TYPE lvc_s_fcat-inttype,
lv_size TYPE i,
lv_decm TYPE i,
ls_fieldcat TYPE lvc_s_fcat,
gr_data TYPE REF TO data.
FIELD-SYMBOLS:
<data_line> TYPE any.
LOOP AT gt_fieldcat INTO ls_fieldcat.
CLEAR: ls_comp.
ls_comp-name = ls_fieldcat-fieldname.
IF ls_fieldcat-ref_table IS NOT INITIAL
AND ls_fieldcat-ref_field IS NOT INITIAL.
CONCATENATE ls_fieldcat-ref_table '-' ls_fieldcat-ref_field INTO lv_reffield.
lo_abap_desc = cl_abap_typedescr=>describe_by_name( p_name = lv_reffield ).
lv_size = lo_abap_desc->length. "ls_fieldcat-intlen.
lv_decm = lo_abap_desc->decimals.
lv_type = lo_abap_desc->type_kind.
ELSE.
lv_size = ls_fieldcat-intlen.
lv_decm = ls_fieldcat-decimals.
lv_type = ls_fieldcat-inttype.
ENDIF.
CASE lv_type.
WHEN 'I' OR 's'.
ls_comp-type = cl_abap_elemdescr=>get_i( ).
WHEN 'D'.
ls_comp-type = cl_abap_elemdescr=>get_d( ).
WHEN 'P'.
ls_comp-type = cl_abap_elemdescr=>get_p( p_length = lv_size p_decimals = lv_decm ).
WHEN 'C'.
ls_comp-type = cl_abap_elemdescr=>get_c( p_length = lv_size ).
WHEN 'N'.
ls_comp-type = cl_abap_elemdescr=>get_n( p_length = lv_size ).
WHEN 'X'.
ls_comp-type = cl_abap_elemdescr=>get_x( p_length = lv_size ).
WHEN OTHERS.
ls_comp-type = cl_abap_elemdescr=>get_c( p_length = lv_size ).
ENDCASE.
APPEND ls_comp TO lt_comp.
ENDLOOP.
"Summary flag
ls_comp-name = 'recty'.
ls_comp-type = cl_abap_elemdescr=>get_c( p_length = 1 ) .
APPEND ls_comp TO lt_comp.
ls_comp-name = 'CELLTAB'.
ls_comp-type ?= cl_abap_typedescr=>describe_by_name( p_name = 'LVC_T_SCOL' ).
APPEND ls_comp TO lt_comp.
ls_comp-name = 'ROWTYPE'.
ls_comp-type = cl_abap_elemdescr=>get_c( p_length = lv_size ).
APPEND ls_comp TO lt_comp.
TRY.
CLEAR: lo_struct.
lo_struct = cl_abap_structdescr=>create( lt_comp ).
CREATE DATA lr_data_line TYPE HANDLE lo_struct.
ASSIGN lr_data_line->* TO <data_line>.
CREATE DATA gr_data LIKE TABLE OF <data_line>.
ASSIGN gr_data->* TO <fs_table>.
CATCH cx_sy_struct_comp_name.
MESSAGE e398(00) WITH 'Create dynamic table error.' space space space.
CATCH cx_sy_struct_creation.
MESSAGE e398(00) WITH 'Create dynamic table error.' space space space.
CATCH cx_sy_table_creation.
MESSAGE e398(00) WITH 'Create dynamic table error.' space space space.
ENDTRY.
ENDFORM. " FRM_CREATE_DYNAMIC_TABLE