Dynamic
Dynamic
”DYNAMIC TABLE
TYPE-POOLS : slis.
TYPES : BEGIN OF gfirst_typ,
vend(6) TYPE c,
month(5) TYPE c,
amt TYPE i.
TYPES : END OF gfirst_typ.
* RTTS Declaratoins.
DATA : gr_struct_typ TYPE REF TO cl_abap_datadescr,
gr_dyntable_typ TYPE REF TO cl_abap_tabledescr,
ls_component TYPE cl_abap_structdescr=>component,
gt_component TYPE cl_abap_structdescr=>component_table.
* SALV Declarations.
DATA : lo_cols TYPE REF TO cl_salv_columns,
lo_salv_table TYPE REF TO cl_salv_table,
lo_column TYPE REF TO cl_salv_column,
col_name(30),
col_desc(20).
*START-OF-SELECTION.
* Populate the initial input table. Usually this input table contents will be
populated at run time, which raises the requirement of dynamic table. The tabl
e contents are filled here for illustration purpose.
perform fill_table using :
'V100' 'JAN' '100',
'V100' 'FEB' '250',
'V200' 'FEB' '200',
'V300' 'FEB' '150',
'V200' 'MAR' '250',
'V300' 'MAR' '300',
'V100' 'APR' '200',
'V100' 'MAY' '100',
'V200' 'MAY' '50',
'V300' 'MAY' '125',
'V400' 'MAY' '475'.
*Loop through the internal table creating a column for every distinct month in
the internal table
LOOP AT it_zdemo INTO wa_zdemo.
* Search the component table if the month column already exists.
READ TABLE gt_component INTO ls_component WITH KEY name = wa_zdemo-month.
IF sy-subrc NE 0.
* The name of the column would be the month and the data type would be same as
the amount field of internal table.
ls_component-name = wa_zdemo-month.
ls_component-type ?= cl_abap_datadescr=>describe_by_data( wa_zdemo-amt ).
APPEND ls_component TO gt_component.
ENDIF.
CLEAR : ls_component, wa_zdemo.
ENDLOOP.
CREATE DATA:
gt_dyn_table TYPE HANDLE gr_dyntable_typ,
gw_dyn_line TYPE HANDLE gr_struct_typ,
gw_dyn_line1 TYPE HANDLE gr_struct_typ.
*
LOOP AT it_zdemo INTO wa_zdemo.
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = lo_salv_table
CHANGING
t_table = <gfs_dyn_table>
).
CATCH cx_salv_msg .
ENDTRY.
lo_salv_table->display( ).
FORM fill_table
USING p_fld1 TYPE gfirst_typ-vend
p_fld2 TYPE gfirst_typ-month
p_fld3 TYPE gfirst_typ-amt.
clear wa_zdemo.
wa_zdemo-vend = p_fld1 .
wa_zdemo-month = p_fld2.
wa_zdemo-amt = p_fld3.
APPEND wa_zdemo TO it_zdemo.
ENDFORM. "“FILL_TABLE
This document stems from the document Dynamic Internal Table iIlustrated with an example of
creating the transpose of internal table which explains use of field symbols, data references and
creating a dynamic internal table as a transpose of an internal table using
cl_alv_table_create=>create_dynamic_table.
Exploring into the much recommended RTTS method, I have found that not only is it more
powerful but that it is more simpler than the old method. I will be using the same example as
mentioned in the previous document to explain the use of RTTS.
As already mentioned, creation and population of dynamic internal table can be done in three
steps.
1. Creating structure
2. Creating dynamic internal table from this structure.
3. Populating the table.
While using the old technique (cl_alv_table_create=>create_dynamic_table,), the first step was
fulfilled by creating a field catalog and the second step using the above mentioned method call.
When creating dynamic table using RTTS, the first and second steps can be done using the
methods provided by RTTS.
Before going into the details, I shall brief you very shortly on RTTS and its methods.
In ABAP Objects, there is a class-based concept called Run Time Type Information (RTTI) that
you can use to determine type attributes at run time. The methods used in this class replaces
statements like describe table, describe field etc.
Another concept Run time Type Creation (RTTC) was added to the above to create data of any
type dynamically.
Both these together comprise the RTTS (Run time Type Services).
The RTTS contains a hierarchy of classes having attributes and methods that are used to perform
these dynamic functions, which could be to get the attributes of a data object at run time or to
create a data object dynamically.
We need to create something like a transpose for this table dynamically. The output should look
like this.
All these methods and attributes belong to the super class cl_abap_typedescr.
For our purpose, we shall be using the method describe_by_data to get the data type attribute of a
field in the input table.
We shall be calling this method from the class cl_abap_datadescr, since the destination variable
is an object type ref to cl_abap_datadescr. As we are calling the method of parent class from
inherited class, we should use the casting operator ‘?=’ , rather than the simple assignment
operator ‘=’, to avoid any type cast error.
Here the data type attributes of the field wa_zdemo-vend which is declared as a six character
variable will be passed to ls_component-type which is data reference pointing to an object of
type cl_abap_datadescr. This is the first column of the dynamic table.
DATA :
The rest of the columns are fields Jan, Feb containing the amount for that month depending on
the data of the input table. (Refer the input table example shown above to understand better)
*Loop through the internal table creating a column for every distinct month in the internal table
LOOP AT it_zdemo INTO wa_zdemo.
* Search the component table if the month column already exists.
READ TABLE gt_component INTO ls_component WITH KEY name = wa_zdemo-month.
IF sy-subrc NE 0.
* The name of the column would be the month and the data type would be same as the amount field of internal
table.
ls_component-name = wa_zdemo-month.
ls_component-type ?= cl_abap_datadescr=>describe_by_data( wa_zdemo-amt ).
APPEND ls_component TO gt_component.
ENDIF.
CLEAR : ls_component, wa_zdemo.
ENDLOOP.
Now we have the components of the structure and the table. To create the structure we shall be
using the method in RTTC.
Methods in RTTC were added to the RTTS type classes to facilitate the creation of types at
runtime.
To create the structure, we shall be using the static method create() in the class
cl_abap_structdescr.
To create a table, first we create the table type from the structure created above. For this, we use
the static method create() of the cl_abap_tabledescr.
Once the table type is created, we instantiate a reference variable to point to data object of this
table type.
Similarly a reference variable of the structure is also created from the structure created as a work
area of the table.
Now our dynamic table is alive and we just need to populate the data. This is done the same way
it was done in the earlier document Dynamic Internal Table iIlustrated with an example of
creating the transpose of internal table. You access each cell of the table using field symbols.
Refer the above document for more information on field symbols and data references.
The contents of the dynamic internal table can be displayed using the factory method in
cl_salv_table.
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = lo_salv_table
CHANGING
t_table = <gfs_dyn_table>
).
CATCH cx_salv_msg .
ENDTRY.
lo_salv_table->display( ).
Complete Code
*&———————————————————————*
*& Report ZDYNAMIC_TABLE
*&———————————————————————*
REPORT zdynamic_table_rtts.
*Author : Susmitha Susan Thomas
TYPE-POOLS : slis.
TYPES : BEGIN OF gfirst_typ,
vend(6) TYPE c,
month(5) TYPE c,
amt TYPE i.
TYPES : END OF gfirst_typ.
* RTTS Declaratoins.
DATA : gr_struct_typ TYPE REF TO cl_abap_datadescr,
gr_dyntable_typ TYPE REF TO cl_abap_tabledescr,
ls_component TYPE cl_abap_structdescr=>component,
gt_component TYPE cl_abap_structdescr=>component_table.
* SALV Declarations.
DATA : lo_cols TYPE REF TO cl_salv_columns,
lo_salv_table TYPE REF TO cl_salv_table,
lo_column TYPE REF TO cl_salv_column,
col_name(30),
col_desc(20).
*START-OF-SELECTION.
* Populate the initial input table. Usually this input table contents will be populated at run time, which raises the
requirement of dynamic table. The table contents are filled here for illustration purpose.
perform fill_table using :
‘V100’ ‘JAN’ ‘100’,
‘V100’ ‘FEB’ ‘250’,
‘V200’ ‘FEB’ ‘200’,
‘V300’ ‘FEB’ ‘150’,
‘V200’ ‘MAR’ ‘250’,
‘V300’ ‘MAR’ ‘300’,
‘V100’ ‘APR’ ‘200’,
‘V100’ ‘MAY’ ‘100’,
‘V200’ ‘MAY’ ’50’,
‘V300’ ‘MAY’ ‘125’,
‘V400’ ‘MAY’ ‘475’.
*Loop through the internal table creating a column for every distinct month in the internal table
LOOP AT it_zdemo INTO wa_zdemo.
* Search the component table if the month column already exists.
READ TABLE gt_component INTO ls_component WITH KEY name = wa_zdemo-month.
IF sy-subrc NE 0.
* The name of the column would be the month and the data type would be same as the amount field of internal table.
ls_component-name = wa_zdemo-month.
ls_component-type ?= cl_abap_datadescr=>describe_by_data( wa_zdemo-amt ).
APPEND ls_component TO gt_component.
ENDIF.
CLEAR : ls_component, wa_zdemo.
ENDLOOP.
CREATE DATA:
gt_dyn_table TYPE HANDLE gr_dyntable_typ,
gw_dyn_line TYPE HANDLE gr_struct_typ,
gw_dyn_line1 TYPE HANDLE gr_struct_typ.
*
LOOP AT it_zdemo INTO wa_zdemo.
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = lo_salv_table
CHANGING
t_table = <gfs_dyn_table>
).
CATCH cx_salv_msg .
ENDTRY.
lo_salv_table->display( ).
FORM fill_table
USING p_fld1 TYPE gfirst_typ-vend
p_fld2 TYPE gfirst_typ-month
p_fld3 TYPE gfirst_typ-amt.
clear wa_zdemo.
wa_zdemo-vend = p_fld1 .
wa_zdemo-month = p_fld2.
wa_zdemo-amt = p_fld3.
APPEND wa_zdemo TO it_zdemo.
ENDFORM. “FILL_TABLE
Output