Dynamic ALV List Generation

By Swarna S, Tata Consultancy Services

* 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.

Output: 

 

 

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image