The main theme of this FMis used generate dynamic internal table in all possible ways.
Most of the developer must have used component type structure using the following method,
cl_abap_structdescr=>describe_by_data
Inorder to improve the flexibilty this FM has been developed.
The inputs are passed to the FM in three ways,
- Import Parameters
- Components
- Field catalog
FUNCTION zxx_dyn_itab.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(START) TYPE NUMC4 DEFAULT 1
*" VALUE(END) TYPE NUMC4 DEFAULT 10
*" VALUE(DATA_TYPE) TYPE CHAR6 DEFAULT 'P'
*" VALUE(DATA_LEN) TYPE I DEFAULT 15
*" VALUE(DECIMAL) TYPE I DEFAULT 2
*" VALUE(COL_NAME) TYPE CHAR5 DEFAULT 'VE'
*" EXPORTING
*" REFERENCE(GT_TABLE) TYPE REF TO DATA
*" REFERENCE(GV_WORKAREA) TYPE REF TO DATA
*" CHANGING
*" REFERENCE(IT_GT_FCAT) TYPE LVC_T_FCAT
*" REFERENCE(IT_COMPONENTS) TYPE ABAP_COMPDESCR_TAB
*" EXCEPTIONS
*" TABLE_CREATION_FAILED
*"----------------------------------------------------------------------
" Get the predifined components
it_comp[] = it_components[].
" To get the total number of records present in fieldcatalog
DESCRIBE TABLE it_gt_fcat LINES gv_tabix.
LOOP AT it_comp INTO gwa_comp.
CLEAR gwa_fcat.
gv_tabix = gv_tabix + 1. " Position the column
MOVE : gwa_comp-name TO gwa_fcat-fieldname,
gwa_comp-length TO gwa_fcat-outputlen ,
gwa_comp-decimals TO gwa_fcat-decimals_o,
gwa_comp-type_kind TO gwa_fcat-inttype,
gwa_comp-length TO gwa_fcat-intlen,
gv_tabix TO gwa_fcat-col_pos.
IF gwa_comp-type_kind EQ 'P'.
gwa_fcat-outputlen = 15.
gwa_fcat-intlen = 15.
ENDIF.
APPEND gwa_fcat TO it_gt_fcat.
ENDLOOP.
gv_tabix = 0.
" If the user entered the details in import paramters, then check the starting number count
IF start GT 0.
" Set the column count
gv_col_ct = start.
DO.
CLEAR gwa_fcat.
gv_tabix = gv_tabix + 1.
" construct the coulmn name
CONCATENATE: col_name gv_col_ct INTO gwa_fcat-fieldname,
col_name gv_col_ct INTO gwa_comp-name.
" Specify the lenght and other technical inof's
MOVE : data_len TO gwa_fcat-outputlen ,
data_len TO gwa_comp-length ,
decimal TO gwa_fcat-decimals_o,
decimal TO gwa_comp-decimals,
data_type TO gwa_fcat-inttype,
data_type TO gwa_comp-type_kind,
data_len TO gwa_fcat-intlen,
data_len TO gwa_comp-length,
gv_tabix TO gwa_fcat-col_pos.
ADD 1 TO gv_col_ct.
" Add the records inorder to generate dynamic internal table
APPEND gwa_fcat TO it_gt_fcat.
" Sent as output for further use
APPEND gwa_comp TO it_components.
" If count reaches max limit then end the processing
IF gv_col_ct GT end.
EXIT.
ENDIF.
ENDDO.
ENDIF.
" Check weather user has entered the details in fieldcatalog otherwise throw error
IF it_gt_fcat[] IS NOT INITIAL.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
* I_STYLE_TABLE =
* ={color} =
it_fieldcatalog = it_gt_fcat
* I_LENGTH_IN_BYTE =
IMPORTING
ep_table = gv_table
* E_STYLE_FNAME =
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS = 2.
IF sy-subrc GT 0.
RAISE table_creation_failed.
ENDIF.
gt_table = gv_table.
ASSIGN gv_table->* TO <f_table>.
CREATE DATA gv_workarea LIKE LINE OF <f_table>.
ELSE.
RAISE table_creation_failed.
ENDIF.
ENDFUNCTION.
In the above FM the default parameters are:
START TYPE NUMC4 1 Count parameters
END TYPE NUMC4 10 Count parameters
DATA_TYPE TYPE CHAR6 'P' Character field of length 6
DATA_LEN TYPE I 15
DECIMAL TYPE I 2
COL_NAME TYPE CHAR5 'VE' R/2 table
For the list of allowed data type. Refer the domain "INTTYPE".