Saturday 22 February 2014

Dynamic Internal Table with Cl_ALV_TABLE_CREATE

Scenario: Creating a run time table design by using standard class : CL_ALV_TABLE_CREATE

Step1. Create a report in SE38.
_________________________________________________________________________________
*&---------------------------------------------------------------------*
*& Report  ZDEMO4_DYNAMIC_TABLE_CREATION
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zdemo4_dynamic_table_creation.

PARAMETERS : tab_name TYPE dd02l-tabname OBLIGATORY.
DATA : ls_tab TYPE dd02l,
              lo_type TYPE REF TO cl_abap_typedescr,
              lo_struc TYPE REF TO cl_abap_structdescr,
              lt_comp TYPE abap_compdescr_tab,
             ls_comp TYPE  abap_compdescr,
             lo_grid TYPE REF TO cl_gui_alv_grid,
             lt_fcat TYPE lvc_t_fcat,
             ls_fcat TYPE lvc_s_fcat.
DATA : dr_tab TYPE REF TO data.
FIELD-SYMBOLS : <fs_tab> TYPE STANDARD TABLE.

START-OF-SELECTION.

  SELECT SINGLE * FROM dd02l INTO ls_tab WHERE tabname = tab_name.
  IF sy-subrc <> 0.
    MESSAGE 'Table does not Exist.' TYPE 'S'.
    LEAVE LIST-PROCESSING.
  ENDIF.

  CALL METHOD cl_abap_typedescr=>describe_by_name
    EXPORTING
      p_name      = tab_name
    RECEIVING
      p_descr_ref = lo_type.

  lo_struc ?= lo_type.
  lt_comp = lo_struc->components[].
  LOOP AT lt_comp INTO ls_comp .
    ls_fcat-fieldname = ls_comp-name.
    CASE ls_comp-type_kind .
      WHEN 'C'.
        ls_fcat-datatype = 'CHAR'.
      WHEN 'N'.
        ls_fcat-datatype = 'NUMC'.
      WHEN 'D'.
        ls_fcat-datatype = 'DATE'.
      WHEN 'P'.
        ls_fcat-datatype = 'PACK'.
      WHEN 'T'.
        ls_fcat-datatype = 'TIME'.
      WHEN OTHERS.
        ls_fcat-datatype = ls_comp-type_kind.
    ENDCASE.
    ls_fcat-inttype = ls_comp-type_kind.
    ls_fcat-intlen = ls_comp-length.
    ls_fcat-decimals = ls_comp-decimals.

    APPEND ls_fcat TO lt_fcat.
    CLEAR ls_fcat.
  ENDLOOP.

  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog           = lt_fcat
      I_LENGTH_IN_BYTE          = 'X'
  IMPORTING
   ep_table                  = dr_tab.

  ASSIGN dr_tab->* TO <fs_tab>.

  SELECT * FROM (tab_name) INTO TABLE <fs_tab>.

END-OF-SELECTION.

  CREATE OBJECT lo_grid
    EXPORTING
      i_parent          = cl_gui_container=>default_screen.

  CALL METHOD lo_grid->set_table_for_first_display
    EXPORTING
      i_structure_name = tab_name
    CHANGING
      it_outtab        = <fs_tab>.

  CALL SCREEN 9999.
_________________________________________________________________________________

Step2. Run the program.












































_________________________________________________________________________________

No comments:

Comments system

Disqus Shortname