It’s not a trivial thing to add new field into already existing itab during run time, but there is a way how to modify the ITAB strucuture. You can add, remove or modify order of the components (fields) in the itab. Let’s assume you have your itab created as data source for ALV grid. This itab is generated dynamically from a field catalog.
But after this itab is generated, you would like to add some columns. In my case it was cell coloring information.
Class instance attributes:
mr_data TYPE REF TO data,
mt_fieldcat TYPE lvc_t_fcat.
I assume the field catalog has already been created. Now I create the itab dynamically and I append a ‘CELL_COLORS’ column as additional field (of type LVC_T_SCOL – Internal table)
METHOD load_data.
DATA:
lr_sdescr TYPE REF TO cl_abap_structdescr,
lr_tdescr TYPE REF TO cl_abap_tabledescr,
lr_data TYPE REF TO data,
lr_data_line TYPE REF TO data,
lt_components TYPE abap_component_tab,
l_lvc_t_scol type lvc_t_scol.
FIELD-SYMBOLS:
<fs_table> TYPE STANDARD TABLE,
<fs_comp> TYPE abap_componentdescr.
* Create itab from field catalog
IF mr_data IS INITIAL.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = mt_fieldcat
IMPORTING
ep_table = lr_data.
* Create dynamic line of this itab
ASSIGN lr_data->* TO <fs_table>.
CREATE DATA lr_data_line LIKE LINE OF <fs_table>.
* We are creating components of the new/updated ITAB
APPEND INITIAL LINE TO lt_components ASSIGNING <fs_comp>.
* We get description of the current itab
<fs_comp>-type ?= cl_abap_structdescr=>describe_by_data_ref( lr_data_line ).
* We assign some dummy name (will not be used anyhow)
<fs_comp>-name = 'DATA'.
* Include all fields (result will be fields of new itab
* If 'as_include' = abap_false, then all fields of the structure
* would be added in our new itab as field of type
* "Internal table" with name 'DATA'
<fs_comp>-as_include = abap_true.
* Now we're adding new component of type
* LVC_T_SCOL - Internal table
APPEND INITIAL LINE TO lt_components ASSIGNING <fs_comp>.
<fs_comp>-type ?= cl_abap_structdescr=>describe_by_data( l_lvc_t_scol ).
<fs_comp>-name = 'CELL_COLORS'.
* We create reference to a line description of the new itab
lr_sdescr = cl_abap_structdescr=>create( lt_components ).
* ...we create reference to the new ITAB description
lr_tdescr = cl_abap_tabledescr=>create( lr_sdescr ).
* Finally we create our new ITAB, where
* 1. all fields from fieldcat are included
* 2. New field of type LCV_T_SCOL is appended
CREATE DATA mr_data TYPE HANDLE lr_tdescr.
ENDIF.