Thursday, October 7, 2010

Dynamic Internal Table

REPORT zdynamic_table.
* To create Dynamic Internal Table
TYPE-POOLS: slis.
TABLES zctp_ch_spare.

DATA: BEGIN OF itab OCCURS 0,
      t_mctype LIKE zctp_ch_spare-t_mctype,
      END OF itab.

FIELD-SYMBOLS: <t_dyntable> TYPE STANDARD TABLE,
               <fs_dyntable>,
               <fs_fldval> TYPE ANY.

DATA:   t_newtable TYPE REF TO data,
        t_newline  TYPE REF TO data,
        t_fldcat   TYPE lvc_t_fcat,
        wa_it_fldcat TYPE lvc_s_fcat,
        wa_colno(2) TYPE n,
        wa_flname(12) TYPE c.

DATA:   wa_fieldcat TYPE slis_fieldcat_alv        ,
        it_fieldcat TYPE slis_t_fieldcat_alv      .

DATA: fieldname(20) TYPE c.
DATA: fieldvalue(10) TYPE c.
DATA: index(3) TYPE c.

START-OF-SELECTION.

  SELECT t_mctype FROM zctp_ch_spare INTO TABLE itab.

* Prepare fieldcatalog to create Dynamic table.
  LOOP AT itab.
    CLEAR wa_it_fldcat.
    wa_flname = itab-t_mctype.
    wa_it_fldcat-fieldname = wa_flname.
    wa_it_fldcat-datatype = 'CHAR'.
    wa_it_fldcat-intlen = 10.
    APPEND wa_it_fldcat TO t_fldcat.
  ENDLOOP.

  CLEAR wa_it_fldcat.
  wa_flname = 'ERFUSER'.
  wa_it_fldcat-fieldname = wa_flname.
  wa_it_fldcat-datatype = 'CHAR'.
  wa_it_fldcat-intlen = 20.
  APPEND wa_it_fldcat TO t_fldcat.

  CLEAR wa_it_fldcat.
  wa_flname = 'ERFDATE'.
  wa_it_fldcat-fieldname = wa_flname.
  wa_it_fldcat-datatype = 'DATS'.
  wa_it_fldcat-intlen = 10.
  APPEND wa_it_fldcat TO t_fldcat.

* Create dynamic internal table and assign to FS
  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = t_fldcat
    IMPORTING
      ep_table        = t_newtable.

  ASSIGN t_newtable->* TO <t_dyntable>.

* Create dynamic work area and assign to FS
  CREATE DATA t_newline LIKE LINE OF <t_dyntable>.
  ASSIGN t_newline->* TO <fs_dyntable>.
* Append values for dynamically created fields
  LOOP AT itab.
    wa_flname = itab-t_mctype.
    fieldvalue = 'value'.

    ASSIGN COMPONENT  wa_flname
        OF STRUCTURE <fs_dyntable> TO <fs_fldval>.

    <fs_fldval> =  fieldvalue.
  ENDLOOP.

  wa_flname = 'ERFUSER'.
  fieldvalue = sy-uname.

  ASSIGN COMPONENT  wa_flname
      OF STRUCTURE <fs_dyntable> TO <fs_fldval>.

  <fs_fldval> =  fieldvalue.

  wa_flname = 'ERFDATE'.
  fieldvalue = sy-datum.

  ASSIGN COMPONENT  wa_flname
      OF STRUCTURE <fs_dyntable> TO <fs_fldval>.

  <fs_fldval> =  fieldvalue.
* Append to the dynamic internal table
  APPEND <fs_dyntable> TO <t_dyntable>.
*To display the Dynamic internal table Data in ALV
  LOOP AT itab.
    CLEAR wa_fieldcat.
    wa_flname = itab-t_mctype.
    wa_fieldcat-fieldname = wa_flname.
    wa_fieldcat-seltext_s = wa_flname.
    wa_fieldcat-outputlen = '10'.
    APPEND wa_fieldcat TO it_fieldcat.
  ENDLOOP.

  CLEAR wa_fieldcat.
  wa_flname = 'ERFUSER'.
  wa_fieldcat-fieldname = wa_flname.
  wa_fieldcat-seltext_s = 'CREATED BY'.
  wa_fieldcat-outputlen = '15'.
  APPEND wa_fieldcat TO it_fieldcat.

  CLEAR wa_fieldcat.
  wa_flname = 'ERFDATE'.
  wa_fieldcat-fieldname = wa_flname.
  wa_fieldcat-seltext_s = 'CREATED DATE'.
  wa_fieldcat-outputlen = '15'.
  APPEND wa_fieldcat TO it_fieldcat.
 
* Call ABAP List Viewer (ALV)
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      it_fieldcat = it_fieldcat
    TABLES
      t_outtab    = <t_dyntable>.

To create DYNAMIC ALV REPORT for Given Transparent table :
Dynamically create internal table for given transparent table.

REPORT  ZDYNAMIC_INTERNAL_TABLE                 .

TYPE-POOLS : abap.

field-symbols: <t_dyntable> type standard table,
               <fs_dyntable>,
               <fs_fldval> type any.

DATA: it_table TYPE REF TO data,
it_line TYPE REF TO data,
xfc TYPE lvc_s_fcat,
ifc TYPE lvc_t_fcat.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
PARAMETERS: p_table(30) TYPE c DEFAULT 'T001'.
SELECTION-SCREEN END OF BLOCK b1.

START-OF-SELECTION.

PERFORM get_structure.
PERFORM create_dynamic_itab.
PERFORM get_data.
PERFORM write_out.
*&---------------------------------------------------------------------*
*& Form get_structure
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM get_structure.

DATA : idetails TYPE abap_compdescr_tab,
xdetails TYPE abap_compdescr.

DATA : ref_table_des TYPE REF TO cl_abap_structdescr.
* Get the structure of the table.
ref_table_des ?= cl_abap_typedescr=>describe_by_name( p_table ).
idetails[] = ref_table_des->components[].

LOOP AT idetails INTO xdetails.
CLEAR xfc.
xfc-fieldname = xdetails-name .
xfc-datatype = xdetails-type_kind.
xfc-inttype = xdetails-type_kind.
xfc-intlen = xdetails-length.
xfc-decimals = xdetails-decimals.
APPEND xfc TO ifc.
ENDLOOP.
ENDFORM. "get_structure
*&---------------------------------------------------------------------*
*& Form create_dynamic_itab
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM create_dynamic_itab.
* Create dynamic internal table and assign to FS
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = ifc
IMPORTING
ep_table = it_table.

ASSIGN it_table->* TO <t_dyntable>.
* Create dynamic work area and assign to FS
CREATE DATA it_line LIKE LINE OF <t_dyntable>.
ASSIGN it_line->* TO <fs_dyntable>.
ENDFORM. "create_dynamic_itab
*&---------------------------------------------------------------------*
*& Form get_data
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM get_data.
* Select Data from table.
SELECT * INTO TABLE <t_dyntable> FROM (p_table).
ENDFORM. "get_data

*&---------------------------------------------------------------------*
*& Form write_out
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM write_out.
* Print data from table.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
 EXPORTING
    I_CALLBACK_PROGRAM                = sy-repid
    I_STRUCTURE_NAME                  = p_table
    I_DEFAULT                         = 'X'
    I_SAVE                            = 'A'
  TABLES
    t_outtab                          = <t_dyntable>
* 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.
ENDFORM. "write_out

No comments: