Vous êtes sur la page 1sur 26

To create the dynamic table using the much recommended methods in RTTS, refer this

document.
Create Dynamic Table using RTTS and display in ALV
The method mentioned below is now obsolete and is not preferred anymore.

To learn about field symbols and data references, continue reading.


Creating a dynamic table is not a big deal. Once you understand the concept, it is as simple as 12-3. So if your picture about dynamic internal table is something complicated, you need to
change that mindset first before proceeding to learn about it.
Dynamic internal table is an internal table with variable rows and columns which can be defined
during run time. The different attributes that can be defined at run time includes the field name,
column attributes, data type, width, reference table and reference fields. There are just three main
steps involved in it.
1. Create the structure of the table
2. Create the dynamic internal table with this structure
3. Populate the dynamic table.
And then you have your dynamic internal table just like any other internal table.
Pre-requisites
Before we start to create a dynamic internal table, we must have an idea of field symbols and
data references. If you know them, skip this session and go directly to dynamic table creation.
Field Symbols
Field symbols are like pointers in C. (Technically, they are not the same, using this analogy just
to get the picture). They just point to fields. They are like place holders or pseudonyms or alias
for other fields.
When you assign a field symbol to a variable, whatever you do to that field symbol, it will be
instantly reflected in the variable it points to (or in ABAP Language, the variable it is assigned
to)
Syntax
Declarations

FIELD-SYMBOLS: <fs1>.

Assignment
ASSIGN f TO <fs1>.

It is like two containers that are connected with a pipe. As long as the field symbol is assigned to
the object, whatever you put in the field symbol, it will flow to the data object. And whatever is
in the data object can be accessed through the field symbol.
Any changes you make in <fs1> will be instantly reflected in the data object f.
<fs1> = 10.
Now variable f will have the value 10.
It can point to any object determined at run time. And for the same reason, it can adopt to any
data type or size at run time depending on the object it is pointing to.
Data References
Data references are used to create data objects dynamically.
Syntax.
Declaration.

DATA <dref> TYPE REF TO DATA.

Creation at run time

CREATE DATA <dref> TYPE <type>|LIKE <obj>.

Now the data type of the object <Dref> will be <type> or the data type of object <obj>

To access the contents of the data object to which a data reference is pointing, you must
deference it. This is where we need field symbols.

ASSIGN <dref>->* TO <FS>.

Now whatever statements are performed on Field symbols, it will be reflected in the object dref
that we created at run time. Accessing field symbol <Fs> is equivalent to accessing data
reference object dref.
Ok. So now we are all set to create dynamic table.
Dynamic Table Creation
Lets take an example to learn the concept.
Suppose I have an internal table IT_DEMO containing three columns vendor name (vend),
Month(month), Amount Due(amt).
VENDOR

MONTH

AMOUNT DUE

V100

Jan

100

V100

Feb

250

V200

Feb

216

V300

Feb

550

V200

Mar

200

V300

Mar

310

V100

Apr

145

V100

May

350

V200

May

600

V300

May

200

V400

May

800

I need to create something like a transpose for this table dynamically. The output should look like
this.
VENDO
R

JAN1
3

FEB1
3

V100

100

250

MAR1
3

APR1
3

MAY1
3

145

350

V200

216

200

600

V300

550

310

200

V400

800

Step 1 Create Structure.


We create structure using field catalog.
If you have used ALV, you must be familiar with field catalog and its fields.

Some of the components of field catalog structure is field name, table name, column text, output
length. These are the attributes that can be defined for each field of the dynamic internal table we
are creating.
Declare a structure of type lvc_s_fcat.
Declare an internal table of type lvc_t_fcat (The line type of this internal table is lvc_s_fcat).
Field Catalog Declaration.
gw_dyn_fcat
gt_dyn_fcat

TYPE lvc_s_fcat,
TYPE lvc_t_fcat.

** This would create structure Vendor Jan13 Feb13 Mar13 .


DATA : gv_pos TYPE i.
DATA : fname TYPE string.

* Declaring the first column vendor


gv_pos = gv_pos + 1.
gw_dyn_fcat-fieldname = VEND.
Field Name
gw_dyn_fcat-outputlen = 5.
Output Length
gw_dyn_fcat-tabname = IT_DEMO. Internal Table Name
gw_dyn_fcat-coltext = VENDOR.
Header text for the column
gw_dyn_fcat-col_pos = gv_pos.
Column position
gw_dyn_fcat-key = X.
Key attribute is set for the field vend.
APPEND gw_dyn_fcat TO gt_dyn_fcat.

clear gw_dyn_fcat.

*Loop through the internal table and creatinga column for every distinct month in the internal table
LOOP AT it_zdemo INTO wa_zdemo.
gv_pos = gv_pos + 1.
CONCATENATE wa_zdemo-month 13 INTO fname.
read table gt_dyn_fcat into gw_dyn_fcat with key fieldname = wa_zdemo-month.
if sy-subrc NE 0.
gw_dyn_fcat-fieldname = wa_zdemo-month.
gw_dyn_fcat-tabname = IT_DEMO.
gw_dyn_fcat-coltext = fname.
gw_dyn_fcat-outputlen = 10.
gw_dyn_fcat-col_pos = gv_pos.
APPEND gw_dyn_fcat TO gt_dyn_fcat.
endif.
clear gw_dyn_fcat.
ENDLOOP.

Now gt_dyn_fcat contains the structure of the table.

Step 2 Create Dynamic Table.


Dynamic internal tables can be created using method CREATE_DYNAMIC_TABLE in class
CL_ALV_TABLE_CREATE.
Importing parameter is the field catalog created in step 1 and the exporting parameter is the
dynamic table. The dynamic table must have been declared as dynamic data using data reference.
DATA : gt_dyn_table TYPE REF TO data.,
gw_line
gw_line1

TYPE REF TO data,


TYPE REF TO data,

* Create a dynamic internal table with this structure.


CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
i_style_table
= X
it_fieldcatalog
= gt_dyn_fcat
IMPORTING
ep_table
= gt_dyn_table
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS
= 2.

Now we have the dynamic table gt_dyn_table. To access the data, we use field symbols.
We shall create two work areas gw_line and gw_line1 like line of gt_dyn_table. (or like line of
<gfs_dyn_table> which is the field-symbol assigned to gt_dyn_table). The work area gw_line
will be accessed by field-symbol <gfs_line> and gw_line1 will be accessed by field symbol
<gfs_line1>.
IF sy-subrc EQ 0.
* Assign the new table to field symbol
ASSIGN gt_dyn_table->* TO <gfs_dyn_table>.
* Create dynamic work area for the dynamic table
CREATE DATA gw_line LIKE LINE OF <gfs_dyn_table>.
CREATE DATA gw_line1 LIKE LINE OF <gfs_dyn_table>.
ASSIGN gw_line->* TO <gfs_line>.
ASSIGN gw_line1->* TO <gfs_line1>.
ENDIF.

Note : Field symbols were declared previously with the following statement.
FIELD-SYMBOLS: <gfs_line>,<gfs_line1>,
<gfs_dyn_table> TYPE STANDARD TABLE,
<fs1>.

Step 3 Populating the dynamic table


Each cell in the dynamic table is accessed using field symbols. We use the field symbol <fs1> to
point to each component of work area <gfs_line> (alias gw_line). The values are moved to the
work area, component by component through this field symbol <fs1>.
LOOP AT it_zdemo INTO wa_zdemo.
* Avoid duplicate entries for key field VEND.
READ TABLE <gfs_dyn_table> INTO <gfs_line1> WITH KEY (VEND) = wa_zdemo-vend.
IF sy-subrc = 0.
CONTINUE.
ENDIF.
* The component vendor of the workarea is assigned to <fs1>
ASSIGN COMPONENT VEND OF STRUCTURE <gfs_line> TO <fs1>.
* The value for vendor in the current loop wa_zdemo-vend flows to the work area through <fs1>
<fs1> = wa_zdemo-vend.
UNASSIGN <fs1>.
* Move the amount for that vendor for each month in the dynamic table. Each month in the dynamic table
can be looped using the field catalog table.
LOOP AT gt_dyn_fcat INTO gw_dyn_fcat.
IF gw_dyn_fcat-fieldname = VEND. Move amount only for month fields, not vendor
CONTINUE.
ENDIF.
READ TABLE it_zdemo WITH KEY vend = wa_zdemo-vend month = gw_dyn_fcat-fieldname INTO
wa_zdemo1.
IF sy-subrc = 0.
ASSIGN COMPONENT gw_dyn_fcat-fieldname OF STRUCTURE <gfs_line> TO <fs1>.
<fs1> = wa_zdemo1-amt.
UNASSIGN <fs1>.
ENDIF.
clear : wa_zdemo1.
ENDLOOP.
* Append the dynamic work area to the dynamic table.
APPEND <gfs_line> TO <gfs_dyn_table>.
CLEAR: <gfs_line>.
clear: wa_zdemo, wa_zdemo1.
ENDLOOP.

Now the dynamic table has been created and has been populated with the values based on the
contents of the initial internal table.
Drawbacks of Dynamic Internal table

Programs with many dynamic internal tables are less readable.

They are less secure since errors cannot be detected by syntax check, but only at runtime

Performance is not as good as static internal table.

Given below is the complete code for the above program.


REPORT zdynamic_table.
*Author ; Susmitha Susan Thomas
TYPES : BEGIN OF gfirst_typ,
vend(6) TYPE c,
month(5) TYPE c,
amt TYPE i.
TYPES : END OF gfirst_typ.
DATA : it_zdemo TYPE TABLE OF gfirst_typ.
DATA : wa_zdemo LIKE LINE OF it_zdemo,
wa_zdemo1 LIKE LINE OF it_zdemo.
DATA : gv_pos TYPE i.
DATA : fname TYPE string.
* Dynamic Table Declarations
DATA : gt_dyn_table TYPE REF TO data,
gw_line
TYPE REF TO data,
gw_line1
TYPE REF TO data,
gw_dyn_fcat
TYPE lvc_s_fcat,
gt_dyn_fcat
TYPE lvc_t_fcat.
* Field Symbols Declarations
FIELD-SYMBOLS: <gfs_line>,<gfs_line1>,
<gfs_dyn_table> TYPE STANDARD TABLE,
<fs1>.
* 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.
wa_zdemo-vend = V100.
wa_zdemo-month = JAN.
wa_zdemo-amt = 100.

APPEND wa_zdemo TO it_zdemo.


wa_zdemo-vend = V100.
wa_zdemo-month = FEB.
wa_zdemo-amt = 200.
APPEND wa_zdemo TO it_zdemo.
wa_zdemo-vend = V200.
wa_zdemo-month = FEB.
wa_zdemo-amt = 200.
APPEND wa_zdemo TO it_zdemo.
wa_zdemo-vend = V300.
wa_zdemo-month = FEB.
wa_zdemo-amt = 150.
APPEND wa_zdemo TO it_zdemo.
wa_zdemo-vend = V200.
wa_zdemo-month = MAR.
wa_zdemo-amt = 250.
APPEND wa_zdemo TO it_zdemo.
wa_zdemo-vend = V300.
wa_zdemo-month = MAR.
wa_zdemo-amt = 300.
APPEND wa_zdemo TO it_zdemo.
wa_zdemo-vend = V100.
wa_zdemo-month = APR.
wa_zdemo-amt = 200.
APPEND wa_zdemo TO it_zdemo.
wa_zdemo-vend = V100.
wa_zdemo-month = MAY.
wa_zdemo-amt = 100.
APPEND wa_zdemo TO it_zdemo.
wa_zdemo-vend = V200.
wa_zdemo-month = MAY.
wa_zdemo-amt = 50.
APPEND wa_zdemo TO it_zdemo.
wa_zdemo-vend = V300.
wa_zdemo-month = MAY.
wa_zdemo-amt = 125.
APPEND wa_zdemo TO it_zdemo.

wa_zdemo-vend = V400.
wa_zdemo-month = MAY.
wa_zdemo-amt = 475.
APPEND wa_zdemo TO it_zdemo.
Write : / Initial Internal Table.
WRITE :/.
write :/(6) Vendor.
write : (12) Month .
write : (3) Amt .
LOOP AT it_zdemo INTO wa_zdemo.
WRITE :/ wa_zdemo-vend, wa_zdemo-month, wa_zdemo-amt.
ENDLOOP.
** This would create structure Vendor Jan13 Feb13 Mar13 etc .
gv_pos = gv_pos + 1.
gw_dyn_fcat-fieldname = VEND.
gw_dyn_fcat-outputlen = 5.
gw_dyn_fcat-tabname
= IT_DEMO.
gw_dyn_fcat-coltext
= VENDOR.
gw_dyn_fcat-col_pos
= gv_pos.
gw_dyn_fcat-key = X.
gw_dyn_fcat-key_sel = X.
APPEND gw_dyn_fcat TO gt_dyn_fcat.
clear gw_dyn_fcat.
* Loop through the internal table creating a column for every
distinct month in the internal table
LOOP AT it_zdemo INTO wa_zdemo.
gv_pos = gv_pos + 1.
CONCATENATE wa_zdemo-month 13 INTO fname.
read table gt_dyn_fcat into gw_dyn_fcat with key fieldname =
wa_zdemo-month.
if sy-subrc NE 0.
gw_dyn_fcat-fieldname = wa_zdemo-month.
gw_dyn_fcat-tabname
= IT_DEMO.
gw_dyn_fcat-coltext
= fname.
gw_dyn_fcat-outputlen = 10.
gw_dyn_fcat-col_pos
= gv_pos.
APPEND gw_dyn_fcat TO gt_dyn_fcat.
endif.

clear gw_dyn_fcat.
ENDLOOP.
** Create a dynamic internal table with this structure.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
i_style_table
= X
it_fieldcatalog
= gt_dyn_fcat
IMPORTING
ep_table
= gt_dyn_table
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS
= 2.
IF sy-subrc EQ 0.
* Assign the new table to field symbol
ASSIGN gt_dyn_table->* TO <gfs_dyn_table>.
* Create dynamic work area for the dynamic table
CREATE DATA gw_line LIKE LINE OF <gfs_dyn_table>.
CREATE DATA gw_line1 LIKE LINE OF <gfs_dyn_table>.
ASSIGN gw_line->* TO <gfs_line>.
ASSIGN gw_line1->* TO <gfs_line1>.
ENDIF.
* Populate the dynamic table
LOOP AT it_zdemo INTO wa_zdemo.
* Avoid duplicate entries for key field PART.
READ TABLE <gfs_dyn_table> INTO <gfs_line1> WITH KEY (VEND) =
wa_zdemo-vend.
IF sy-subrc = 0.
CONTINUE.
ENDIF.
ASSIGN COMPONENT VEND OF STRUCTURE <gfs_line> TO <fs1>.
<fs1> = wa_zdemo-vend.
UNASSIGN <fs1>.
LOOP AT gt_dyn_fcat INTO gw_dyn_fcat.
IF gw_dyn_fcat-fieldname = VEND.
CONTINUE.

ENDIF.
READ TABLE it_zdemo WITH KEY vend = wa_zdemo-vend month =
gw_dyn_fcat-fieldname INTO wa_zdemo1.
IF sy-subrc = 0.
ASSIGN COMPONENT gw_dyn_fcat-fieldname OF STRUCTURE <gfs_line> TO
<fs1>.
<fs1> = wa_zdemo1-amt.
UNASSIGN <fs1>.
ENDIF.
clear : wa_zdemo1.
ENDLOOP.
APPEND <gfs_line> TO <gfs_dyn_table>.
CLEAR: <gfs_line>.
clear: wa_zdemo, wa_zdemo1.
ENDLOOP.
WRITE :/.
Write : / Dynamic Internal Table.
WRITE :/.
LOOP AT gt_dyn_fcat INTO gw_dyn_fcat.
WRITE (10) : gw_dyn_fcat-coltext.
ENDLOOP.
WRITE :/.
LOOP AT <gfs_dyn_table> INTO <gfs_line>.
LOOP AT gt_dyn_fcat INTO gw_dyn_fcat.
ASSIGN COMPONENT gw_dyn_fcat-fieldname OF STRUCTURE <gfs_line> TO
<fs1>.
WRITE : <fs1>.
ENDLOOP.
WRITE :/
.
ENDLOOP.
Output

Just a minor addition to the above program. Now if you want to display this as an ALV grid, you
need to create another field catalog. Since the field catalog created above is not compatible with
the field catalog passed as parameter for ALV display.
data : gw_alv_fieldcat type slis_fieldcat_alv,
gt_alv_fieldcat type slis_t_fieldcat_alv.
data: lv_pos type i.
loop at gt_dyn_fcat into gw_dyn_fcat.
lv_pos = lv_pos + 1.
gw_alv_fieldcat-fieldname = gw_dyn_fcat-fieldname.
gw_alv_fieldcat-tabname
= gw_dyn_fcat-tabname.
gw_alv_fieldcat-seltext_l = gw_dyn_fcat-coltext.
gw_alv_fieldcat-outputlen = gw_dyn_fcat-outputlen.
gw_alv_fieldcat-col_pos
= lv_pos.
gw_alv_fieldcat-do_sum
= gw_dyn_fcat-do_sum.
gw_alv_fieldcat-emphasize = gw_dyn_fcat-emphasize.
gw_alv_fieldcat-key
= gw_dyn_fcat-key.
gw_alv_fieldcat-no_out
= gw_dyn_fcat-no_out.
append gw_alv_fieldcat to gt_alv_fieldcat.

endloop.
call function REUSE_ALV_GRID_DISPLAY
exporting
i_callback_program = sy-repid
it_fieldcat
= gt_alv_fieldcat
i_default
= X
i_save
= A
tables
t_outtab
= <gfs_dyn_table>.

Create dynamic internal table using RTTS

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.

Lets see our scenario again.

We have an internal table IT_DEMO containing three columns vendor name (vend),
Month(month), Amount Due(amt).

TYPES : BEGIN OF gfirst_typ,


vend(6) TYPE c,
month(5) TYPE c,
amt
TYPE i.
TYPES : END OF gfirst_typ.
* Input table declarations
DATA :
it_zdemo TYPE TABLE OF gfirst_typ,
wa_zdemo LIKE LINE OF it_zdemo.

VENDOR

MONTH

AMOUNT
DUE

V100

Jan

100

V100

Feb

250

V200

Feb

216

V300

Feb

550

V200

Mar

200

V300

Mar

310

V100

Apr

145

V100

May

350

V200

May

600

V300

May

200

V400

May

800

We need to create something like a transpose for this table dynamically. The output should look
like this.
VENDO
R

JAN1
3

FEB1
3

V100

100

250

MAR1
3

APR1
3

MAY1
3

145

350

V200

216

200

600

V300

550

310

200

V400

800

Step 1 Create Structure.


The RTTI contains attributes and methods that are used in this hierarchy of classes to get the
property of variables whether it is a field or structure or table or an object instance of a class, at
run time.

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.
ls_component-type ?= cl_abap_datadescr=>describe_by_data( wa_zdemo-vend ).

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 :
ls_component

TYPE cl_abap_structdescr=>component,

gt_component

TYPE cl_abap_structdescr=>component_table.

ls_component-name = VEND.
ls_component-type ?= cl_abap_datadescr=>describe_by_data( wa_zdemo-vend ).
APPEND ls_component TO gt_component.

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.
DATA :

gr_struct_typ

gr_struct_typ

TYPE REF TO

cl_abap_datadescr.

?= cl_abap_structdescr=>create( p_components = gt_component ).

Step 2 Create Table.

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.

DATA :

gr_dyntable_typ TYPE REF TO

cl_abap_tabledescr.

gr_dyntable_typ = cl_abap_tabledescr=>create( p_line_type = gr_struct_typ ).

Once the table type is created, we instantiate a reference variable to point to data object of this
table type.

** Dynamic Table Declarations


DATA : gt_dyn_table
TYPE REF TO data,
gw_dyn_line
TYPE REF TO data,

CREATE DATA gt_dyn_table TYPE HANDLE gr_dyntable_typ.

Similarly a reference variable of the structure is also created from the structure created as a work
area of the table.

CREATE DATA

gw_dyn_line TYPE HANDLE gr_struct_typ.

Step 3 Populate internal 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.

Populate the dynamic table


LOOP AT it_zdemo INTO wa_zdemo.
* Avoid duplicate entries for key field Vendor.
READ TABLE <gfs_dyn_table> INTO <gfs_line1> WITH KEY (VEND) = wa_zdemovend.
IF sy-subrc = 0.
*if <gfs_line1> is ASSIGNED.
* UNASSIGN <gfs_line1>.
CONTINUE.
ENDIF.
ASSIGN COMPONENT VEND OF STRUCTURE <gfs_line> TO <fs1>.
*if <fs1> is ASSIGNED.
<fs1> = wa_zdemo-vend.
UNASSIGN <fs1>.
**endif.
LOOP AT gt_component INTO ls_component.
IF ls_component-name = VEND.
CONTINUE.
ENDIF.
READ TABLE it_zdemo WITH KEY vend = wa_zdemo-vend month = ls_component-name
INTO wa_zdemo1.
IF sy-subrc = 0.
ASSIGN COMPONENT ls_component-name OF STRUCTURE <gfs_line> TO <fs1>.
IF <fs1> IS ASSIGNED.
<fs1> = wa_zdemo1-amt.
UNASSIGN <fs1>.
ENDIF.
ENDIF.
CLEAR : wa_zdemo1.
ENDLOOP.
APPEND <gfs_line> TO <gfs_dyn_table>.
CLEAR: <gfs_line>.
CLEAR: wa_zdemo, wa_zdemo1.
ENDLOOP.

Display the contents of dynamic table in ALV

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.
* Input table declarations
DATA :
it_zdemo TYPE TABLE OF gfirst_typ,
wa_zdemo LIKE LINE OF it_zdemo,
wa_zdemo1 LIKE LINE OF it_zdemo.
** Dynamic Table Declarations
DATA : gt_dyn_table
TYPE REF TO data,
gw_dyn_line
TYPE REF TO data,
gw_dyn_line1
TYPE REF TO data.
* Field Symbold Declaration
FIELD-SYMBOLS: <gfs_line>,<gfs_line1>,
<gfs_dyn_table> TYPE STANDARD TABLE,
<fs1>.
* 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.
WRITE : / Initial Internal Table, /.
WRITE :/(6) Vendor,(12) Month , (3) Amt .
LOOP AT it_zdemo INTO wa_zdemo.
WRITE :/ wa_zdemo-vend, wa_zdemo-month, wa_zdemo-amt.
ENDLOOP.
* Create structure of dynamic internal table Vendor Jan13 Feb13 Mar13 .
ls_component-name = VEND.
ls_component-type ?= cl_abap_datadescr=>describe_by_data( wa_zdemo-vend ).
APPEND ls_component TO gt_component.
*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.
gr_struct_typ ?= cl_abap_structdescr=>create( p_components = gt_component ).
gr_dyntable_typ = cl_abap_tabledescr=>create( p_line_type = gr_struct_typ ).
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.
ASSIGN gt_dyn_table->* TO <gfs_dyn_table>.
ASSIGN gw_dyn_line->* TO <gfs_line>.
ASSIGN gw_dyn_line1->* TO <gfs_line1>.
*
** Populate the dynamic table
*
LOOP AT it_zdemo INTO wa_zdemo.
* Avoid duplicate entries for key field Vendor.
READ TABLE <gfs_dyn_table> INTO <gfs_line1> WITH KEY (VEND) = wa_zdemovend.
IF sy-subrc = 0.
*if <gfs_line1> is ASSIGNED.
* UNASSIGN <gfs_line1>.
CONTINUE.
ENDIF.
ASSIGN COMPONENT VEND OF STRUCTURE <gfs_line> TO <fs1>.
*if <fs1> is ASSIGNED.
<fs1> = wa_zdemo-vend.
UNASSIGN <fs1>.
**endif.
LOOP AT gt_component INTO ls_component.
IF ls_component-name = VEND.
CONTINUE.
ENDIF.
READ TABLE it_zdemo WITH KEY vend = wa_zdemo-vend month = ls_component-name
INTO wa_zdemo1.
IF sy-subrc = 0.
ASSIGN COMPONENT ls_component-name OF STRUCTURE <gfs_line> TO <fs1>.
IF <fs1> IS ASSIGNED.
<fs1> = wa_zdemo1-amt.
UNASSIGN <fs1>.
ENDIF.
ENDIF.
CLEAR : wa_zdemo1.
ENDLOOP.
APPEND <gfs_line> TO <gfs_dyn_table>.
CLEAR: <gfs_line>.
CLEAR: wa_zdemo, wa_zdemo1.
ENDLOOP.
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table
= lo_salv_table
CHANGING
t_table
= <gfs_dyn_table>
).

CATCH cx_salv_msg .
ENDTRY.
* get columns object
lo_cols = lo_salv_table->get_columns( ).
*Individual Column Names
LOOP AT gt_component INTO ls_component.
TRY.
col_name = ls_component-name.
lo_column = lo_cols->get_column( col_name ). < <
IF col_name = VEND.
col_desc = Vendor.
ELSE.
CONCATENATE col_name 13 INTO col_desc.
ENDIF.
lo_column->set_medium_text( col_desc ).
lo_column->set_output_length( 10 ).
CATCH cx_salv_not_found.
ENDTRY.
ENDLOOP.
* display table
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.

Output

FILL_TABLE

#EC NO_HANDLER

Acknowledgements
The figures of RTTS class hierarchy were taken from the TAW study material.

Vous aimerez peut-être aussi