ALV GRID Dynamic Table – Displaying ALV Grid Using Dynamic Table

Today I want to teach you how to display ALV grid using a dynamic internal table, in this program sample, you will create one input parameter for the transparent table that you want to show in the ALV grid.

Now execute tcode SE38 and just create an ABAP program called Z_DYNAMIC_ALV_GRID_TABLE.

Copy this ABAP code below.

REPORT Z_DYNAMIC_ALV_GRID_TABLE.

* Display ALV Grid Using Dynamic Internal Table
* www.freesaptutorial.com



TYPE-POOLS: SLIS.

DATA:
LD_ST_TABLE TYPE REF TO DATA,
ITAB TYPE REF TO DATA,
ST_DESC TYPE REF TO CL_ABAP_STRUCTDESCR,
IS_LAYOUT TYPE SLIS_LAYOUT_ALV,
L_FIELDCAT TYPE LVC_S_FCAT,
T_FIELDCAT TYPE LVC_T_FCAT,
LS_FIELDCAT TYPE SLIS_FIELDCAT_ALV,
LT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV.

*Declare ALV Grid Tilte
DATA :
Title1(30),
Title2(10),
Title3(50).

*Declare Field Symbols
FIELD-SYMBOLS :
TYPE STANDARD TABLE,
TYPE ANY,
TYPE ABAP_COMPDESCR.

*The parameter selection where we put the transparent
*table name

PARAMETERS :
P_ITAB LIKE DD02L-TABNAME.

INITIALIZATION.

START-OF-SELECTION.

*texts for grid title
Title1 = 'Dynamic ALV Grid Table'.
Title2 = P_ITAB.

CONCATENATE Title1 Title2 INTO Title3 SEPARATED BY SPACE.

* Create structure for dynamic table
CREATE DATA LD_ST_TABLE TYPE (P_ITAB).
ASSIGN LD_ST_TABLE->* TO .

* Dynamic table Fields Structure
ST_DESC ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_DATA( ).

LOOP AT ST_DESC->COMPONENTS ASSIGNING .
* Build the Fieldcatalog
L_FIELDCAT-FIELDNAME = -NAME.
L_FIELDCAT-REF_TABLE = P_ITAB.
APPEND L_FIELDCAT TO T_FIELDCAT.
* Build the Fieldcatalog
LS_FIELDCAT-FIELDNAME = -NAME.
LS_FIELDCAT-REF_TABNAME = P_ITAB.
APPEND LS_FIELDCAT TO LT_FIELDCAT.
ENDLOOP.

* ALV Layout
IS_LAYOUT-ZEBRA = 'X'.
IS_LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
IS_LAYOUT-WINDOW_TITLEBAR = Title3.

* Here we create the dynamic table
CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
EXPORTING
IT_FIELDCATALOG = T_FIELDCAT
IMPORTING
EP_TABLE = ITAB.

ASSIGN ITAB->* TO .

* Read data from the table selected.

SELECT * FROM (P_ITAB)
INTO CORRESPONDING FIELDS OF TABLE .

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
is_layout = IS_LAYOUT
IT_FIELDCAT = LT_FIELDCAT
i_bypassing_buffer = 'X'
i_callback_program = sy-repid
i_save = 'A'
TABLES
t_outtab = . "internal table

IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

Click F8 to test the program. Now just type in any transparent table in the input box and click EXECUTE (F8)

dit02-01
Here’s the ALV GRID display using a dynamic internal table.
dit02-02

Subscribe in a reader

Comments

0 Responses to "ALV GRID Dynamic Table – Displaying ALV Grid Using Dynamic Table"

Post a Comment

Stay Updated