메뉴 건너뛰기

SAP 한국 커뮤니티



REPORT z_alv_dynamic_data

노름마치 2007.11.05 21:43 조회 수 : 3107

REPORT z_alv_dynamic_data.
*>*********************************************************************
* This report displays data from SAP tables, views (like SE16)        *
* FM : REUSE_ALV_GRID_DISPLAY                                         *
*---------------------------------------------------------------------*
* Author : Michel PIOUD                                               *
* Email :   HomePage : http://www.geocities.com/mpioud *
*>*********************************************************************
*    __o      __o      __o      __o      __o      __o      __o      _ *
*  _(<._   _(<._   _(<._   _(<._   _(<._   _(<._   _(<._   _( *
* (_)/ (_) (_)/ (_) (_)/ (_) (_)/ (_) (_)/ (_) (_)/ (_) (_)/ (_) (_)/ *
***********************************************************************
CONSTANTS :
  c_x VALUE 'X'.
*---------------------------------------------------------------------*
DATA:
  g_tabclass TYPE tabclass.            " Table category
*---------------------------------------------------------------------*
SELECTION-SCREEN :
BEGIN OF LINE, COMMENT 1(35) v_1 FOR FIELD p_table.         "#EC NEEDED
PARAMETERS p_table TYPE dd03l-tabname OBLIGATORY.
SELECTION-SCREEN END OF LINE.


SELECTION-SCREEN :
BEGIN OF LINE, COMMENT 1(35) v_2 FOR FIELD p_max.           "#EC NEEDED
PARAMETERS p_max(2) TYPE n DEFAULT '20' OBLIGATORY.
SELECTION-SCREEN END OF LINE.
*---------------------------------------------------------------------*
AT SELECTION-SCREEN.


* Read table category
  SELECT SINGLE tabclass
           INTO g_tabclass
           FROM dd02l
          WHERE tabname  = p_table
            AND as4local = 'A'
            AND as4vers  = '0000'.
  IF sy-subrc NE 0.
*   Table & is not active in the Dictionary
    MESSAGE e402(mo) WITH p_table.
  ELSEIF g_tabclass = 'INTTAB'.
*   & is a structure, not a table
    MESSAGE e403(mo) WITH p_table.
  ENDIF.


*---------------------------------------------------------------------*
INITIALIZATION.


  v_1 = 'Table'.
  v_2 = 'Maximum of records'.


  GET PARAMETER ID 'DTB' FIELD p_table.
  IF p_table IS INITIAL.
    p_table = 'SPFLI'.
  ENDIF.


*---------------------------------------------------------------------*
START-OF-SELECTION.


  PERFORM f_display_data.


*---------------------------------------------------------------------*
*      Form  F_DISPLAY_DATA
*---------------------------------------------------------------------*
FORM f_display_data.


  TYPE-POOLS: slis.                    " ALV Global Types


* Macro definition
  DEFINE m_sort.
    add 1 to ls_sort-spos.
    ls_sort-fieldname = &1.
    ls_sort-up = c_x.
    append ls_sort to lt_sort.
  END-OF-DEFINITION.


  DATA:
    ls_dd03l    TYPE dd03l,            " Table Fields
    lt_dd03l    TYPE TABLE OF dd03l,   " Table Fields
    lp_struct   TYPE REF TO data,
    lp_table    TYPE REF TO data,      " Pointer to dynamic table
    of_sdescr   TYPE REF TO cl_abap_structdescr,
    ls_layout   TYPE slis_layout_alv,
    ls_lvc_cat  TYPE lvc_s_fcat,
    lt_lvc_cat  TYPE lvc_t_fcat,       " Field catalog
    ls_sort     TYPE slis_sortinfo_alv,
    lt_sort     TYPE slis_t_sortinfo_alv,  " Sort table
    ls_fieldcat TYPE slis_fieldcat_alv,
    lt_fieldcat TYPE slis_t_fieldcat_alv.  " Field catalog


  FIELD-SYMBOLS :
    <fieldcat>   TYPE slis_fieldcat_alv,
    <lt_data>    TYPE STANDARD TABLE,      " Data to display
    <fs>         TYPE ANY,
    <components> TYPE abap_compdescr.


* Dynamic creation of a structure
  CREATE DATA lp_struct TYPE (p_table).
  ASSIGN lp_struct->* TO <fs>.


* Fields Structure
  of_sdescr ?= cl_abap_typedescr=>describe_by_data( <fs> ).


  LOOP AT of_sdescr->components ASSIGNING <components>.
*   Build Fieldcatalog
    ls_lvc_cat-fieldname = <components>-name.
    ls_lvc_cat-ref_table = p_table.
    APPEND ls_lvc_cat TO lt_lvc_cat.
*   Build Fieldcatalog
    ls_fieldcat-fieldname = <components>-name.
    ls_fieldcat-ref_tabname = p_table.
    APPEND ls_fieldcat TO lt_fieldcat.
  ENDLOOP.


* Add checkbox
  CLEAR ls_lvc_cat.
  ls_lvc_cat-fieldname = 'CHECKBOX'.
  APPEND ls_lvc_cat TO lt_lvc_cat.


* Create internal table
  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = lt_lvc_cat
    IMPORTING
      ep_table        = lp_table.


  ASSIGN lp_table->* TO <lt_data>.


* Read data
  SELECT * FROM (p_table) UP TO p_max ROWS
    INTO CORRESPONDING FIELDS OF TABLE <lt_data>
   ORDER BY PRIMARY KEY.


  IF <lt_data> IS INITIAL.
*   No table entries found for specified key
    MESSAGE i429(mo).
    EXIT.
  ENDIF.


* Read key field to Build Sort Table
  SELECT * FROM dd03l INTO TABLE lt_dd03l
                     WHERE tabname  = p_table
                       AND fieldname <> '.INCLUDE'
                       AND as4vers  = '0000'
                       AND as4local = 'A'
                       AND keyflag = c_x.


  SORT lt_dd03l BY position.


* Build Sort Table
  LOOP AT lt_dd03l INTO ls_dd03l.
    READ TABLE lt_fieldcat ASSIGNING <fieldcat>
                            WITH KEY fieldname = ls_dd03l-fieldname.
    CHECK sy-subrc EQ 0.
*   Build Sort Table
    m_sort ls_dd03l-fieldname.
    <fieldcat>-key = c_x.
  ENDLOOP.


* Layout
  ls_layout-zebra = c_x.
  ls_layout-colwidth_optimize = c_x.
  ls_layout-group_change_edit = c_x.
  ls_layout-box_fieldname = 'CHECKBOX'.


  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      is_layout   = ls_layout
      it_fieldcat = lt_fieldcat
      it_sort     = lt_sort
    TABLES
      t_outtab    = <lt_data>.


ENDFORM.                               " F_DISPLAY_DATA
*************** END OF PROGRAM Z_ALV_DYNAMIC_DATA *********************