메뉴 건너뛰기

SAP 한국 커뮤니티



REPORT z_alv_list_transposed

노름마치 2007.11.05 21:49 조회 수 : 3019

REPORT z_alv_list_transposed.
*---------------------------------------------------------------------*
* This report displays an ALV Grid list transposed. It can be easely  *
* adapted to your needs, just replace SPFLI by another DDIC table     *
*---------------------------------------------------------------------*
* Author : Michel PIOUD                                               *
* Email : mpioud@yahoo.fr  HomePage : http://www.geocities.com/mpioud *
*---------------------------------------------------------------------*
TYPE-POOLS: slis.                      " ALV Global types


SELECTION-SCREEN :
  SKIP, BEGIN OF LINE,COMMENT 5(27) v_1 FOR FIELD p_max.    "#EC NEEDED
PARAMETERS p_max(2) TYPE n DEFAULT '10' OBLIGATORY.
SELECTION-SCREEN END OF LINE.


DATA gt_data TYPE STANDARD TABLE OF spfli WITH HEADER LINE.


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


  v_1 = 'Maximum of records to read'.


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


  PERFORM f_read_data.


  PERFORM f_display_data.


*---------------------------------------------------------------------*
*      Form  f_read_data
*---------------------------------------------------------------------*
FORM f_read_data.


  SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_data
           FROM spfli
             UP TO p_max ROWS.


ENDFORM.                               " F_READ_DATA
*---------------------------------------------------------------------*
*      Form  F_DISPLAY_DATA
*---------------------------------------------------------------------*
FORM f_display_data.


  DATA:
    l_column    TYPE sy-tabix,
    lp_struct   TYPE REF TO data,
    lp_table    TYPE REF TO data,      " Pointer to dynamic table
    ls_lvc_cat  TYPE lvc_s_fcat,
    lt_lvc_cat  TYPE lvc_t_fcat,       " Field catalog
    lt_fcat     TYPE slis_t_fieldcat_alv,  " Field catalog
    ls_fieldcat TYPE slis_fieldcat_alv,
    lt_fieldcat TYPE slis_t_fieldcat_alv,  " Field catalog
    ls_layout   TYPE slis_layout_alv.


  FIELD-SYMBOLS :
    <header>       TYPE ANY,
    <field_header> TYPE ANY,
    <field_spfli>  TYPE ANY,
    <lt_data>      TYPE table.         " Data to display


  ls_lvc_cat-fieldname = 'COLUMNTEXT'.
  ls_lvc_cat-ref_table = 'LVC_S_DETA'.
  APPEND ls_lvc_cat TO lt_lvc_cat.


  ls_fieldcat-fieldname = 'COLUMNTEXT'.
  ls_fieldcat-ref_tabname = 'LVC_S_DETA'.
  ls_fieldcat-key  = 'X'.
  APPEND ls_fieldcat TO lt_fieldcat.


  DESCRIBE TABLE gt_data.


  DO sy-tfill TIMES.
*   For each line, a column 'VALUEx' is created in the fieldcatalog
*   Build Fieldcatalog
    WRITE sy-index TO ls_lvc_cat-fieldname LEFT-JUSTIFIED.
    CONCATENATE 'VALUE' ls_lvc_cat-fieldname
           INTO ls_lvc_cat-fieldname.
    ls_lvc_cat-ref_field = 'VALUE'.
    ls_lvc_cat-ref_table = 'LVC_S_DETA'.
    APPEND ls_lvc_cat TO lt_lvc_cat.
*   Build Fieldcatalog
    CLEAR ls_fieldcat.
    ls_fieldcat-fieldname = ls_lvc_cat-fieldname.
    ls_fieldcat-ref_fieldname = 'VALUE'.
    ls_fieldcat-ref_tabname = 'LVC_S_DETA'.
    APPEND ls_fieldcat TO lt_fieldcat.
  ENDDO.


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


* Create structure = structure of the internal table
  CREATE DATA lp_struct LIKE LINE OF <lt_data>.
  ASSIGN lp_struct->* TO <header>.


* Create field catalog from dictionary structure
  CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
       EXPORTING
            i_structure_name       = 'SPFLI'
       CHANGING
            ct_fieldcat            = lt_fcat
       EXCEPTIONS
            inconsistent_interface = 1
            program_error          = 2
            OTHERS                 = 3.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.


  DESCRIBE TABLE lt_fcat.


* Fill the internal to display <lt_data>
  DO sy-tfill TIMES.
    IF sy-index = 1.
      READ TABLE lt_fcat INTO ls_fieldcat INDEX 1.
      IF ls_fieldcat-fieldname = 'MANDT'.
*       If 1st column is MANDT, it's not displayed
        CONTINUE.
      ENDIF.
    ENDIF.
*   For each field of GT_DATA
    ASSIGN COMPONENT 1 OF STRUCTURE <header> TO <field_header>.
    IF sy-subrc NE 0. EXIT .ENDIF.
    READ TABLE lt_fcat INTO ls_fieldcat INDEX sy-index.
*   Fill 1st column
    <field_header> = ls_fieldcat-seltext_m.
    IF <field_header> IS INITIAL.
      <field_header> = ls_fieldcat-fieldname.
    ENDIF.


    LOOP AT gt_data.
      l_column = sy-tabix + 1.
      ASSIGN COMPONENT sy-index OF STRUCTURE gt_data TO <field_spfli>.
      IF sy-subrc NE 0. EXIT .ENDIF.
      ASSIGN COMPONENT l_column OF STRUCTURE <header> TO <field_header>.
      IF sy-subrc NE 0. EXIT .ENDIF.
      WRITE <field_spfli> TO <field_header> LEFT-JUSTIFIED.
    ENDLOOP.
    APPEND <header> TO <lt_data>.
  ENDDO.


  ls_layout-colwidth_optimize = 'X'.
  ls_layout-no_colhead = 'X'.
  ls_layout-zebra = 'X'.


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


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