Dynamic ALV Generation

* AS : Dynamic ALV generation with data
* stored in a table,DB table as input.
* Author : Swarna.S.
REPORT Z_DYNALV .
*Type pools declaration for ALV
TYPE-POOLS: slis. " ALV Global Types
*data declaration for dynamic internal table and alv
DATA: l_structure TYPE REF TO data,
l_table TYPE REF TO data,
struc_desc TYPE REF TO cl_abap_structdescr,
lt_layout TYPE slis_layout_alv,
ls_lvc_fieldcatalogue TYPE lvc_s_fcat,
lt_lvc_fieldcatalogue TYPE lvc_t_fcat,
ls_fieldcatalogue TYPE slis_fieldcat_alv,
lt_fieldcatalogue TYPE slis_t_fieldcat_alv.
*field symbols declaration
FIELD-SYMBOLS :
<it_table> TYPE STANDARD TABLE,
<dyn_str> TYPE ANY,
<str_comp> TYPE abap_compdescr.
*declarations for grid title
DATA : T1(30),
T2(10),
T3(50).
*selection screen declaration for table input
parameters : p_table like dd02l-tabname.
*initialization event
INITIALIZATION.
*start of selection event
START-OF-SELECTION.
*texts for grid title
T1 = 'Dynamic ALV display for table'.
T2 = p_table.
CONCATENATE T1 T2 INTO T3 SEPARATED BY SPACE.
* Dynamic creation of a structure
CREATE DATA l_structure TYPE (p_table).
ASSIGN l_structure->* TO <dyn_str>.
* Fields Structure
struc_desc ?= cl_abap_typedescr=>describe_by_data( <dyn_str> ).
LOOP AT struc_desc->components ASSIGNING <str_comp>.
* Build Fieldcatalog
ls_lvc_fieldcatalogue-fieldname = <str_comp>-name.
ls_lvc_fieldcatalogue-ref_table = p_table.
APPEND ls_lvc_fieldcatalogue TO lt_lvc_fieldcatalogue.
* Build Fieldcatalog
ls_fieldcatalogue-fieldname = <str_comp>-name.
ls_fieldcatalogue-ref_tabname = p_table.
APPEND ls_fieldcatalogue TO lt_fieldcatalogue.
ENDLOOP.
 
* Create internal table dynamic
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = lt_lvc_fieldcatalogue
IMPORTING
ep_table = l_table.
ASSIGN l_table->* TO <it_table>.
* Read data from the table selected.
SELECT * FROM (p_table)
INTO CORRESPONDING FIELDS OF TABLE <it_table>.
* ALV Layout
lt_layout-zebra = 'X'.
lt_layout-colwidth_optimize = 'X'.
lt_layout-window_titlebar = T3.
*ALV output
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
IS_LAYOUT = lt_layout
IT_FIELDCAT = lt_fieldcatalogue
TABLES
T_OUTTAB = <it_table>
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2
.
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

More ALV tutorials.

Comments