Sunteți pe pagina 1din 3

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 po
pulated at run time, which raises the requirement of dynamic table. The table co
ntents 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 t
he 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 t
he amount field of internal table.
ls_component-name = wa_zdemo-month.
ls_component-type ?= cl_abap_datadescr=>describe_by_data( wa_zdemo-amt ). " To
define data length
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_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_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. "#EC NO_HANDLER
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. "FILL_TABLE

S-ar putea să vă placă și