动态内部表+ALV

  *&---------------------------------------------------------------------*
*& Report  ZTEST001
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZTEST001.


TYPE-POOLS: slis.
DATA:      l_events  TYPE slis_t_event,
           gt_list_top_of_page  TYPE slis_t_listheader,
           gs_layout  TYPE slis_layout_alv,
           wa_fields  type slis_fieldcat_alv ,
           fieldcat  TYPE slis_t_fieldcat_alv  WITH  HEADER  LINE,
           eventcat  TYPE slis_t_event  WITH  HEADER  LINE,
           it_sort  TYPE slis_t_sortinfo_alv  WITH  HEADER  LINE,
            prg  LIKE sy-repid.

DATA: l_variant  TYPE  TABLE  OF disvariant  WITH  HEADER  LINE.

FIELD-SYMBOLS:<DYN_TABLE>  TYPE  STANDARD  TABLE,

              <DYN_WA>,

              <DYN_FIELD>.

DATA: DY_TABLE  TYPE  REF  TO  DATA,

      DY_LINE   TYPE  REF  TO  DATA,

      IT_STRUCTURE  TYPE LVC_T_FCAT,

      WA_STRUCTURE  TYPE LVC_S_FCAT.

START- OF-SELECTION.

   PERFORM CREATE_STRUCTURE. " 定义内表的结构

   PERFORM CREATE_DYNAMIC_TABLE. " 按照定义的内表结构,产生一个内表

   PERFORM WRITE_DATA_TO_DYNTABLE. " 向动态内表中写数

   PERFORM OUTPUT_DYNTABLE_DATA. " 从动态内表中取数,并写到屏幕


   PERFORM prepare_list_val.
   PERFORM display_val.

*&---------------------------------------------------------------------*

*&      Form  CREATE_STRUCTURE

*------------------------------------------------sy



FORM CREATE_STRUCTURE.

  WA_STRUCTURE-FIELDNAME =  'COL1'. " 第列名

  WA_STRUCTURE-COL_POS =  1.          " 表示第三列 ---

  WA_STRUCTURE-INTTYPE =  'C'.         " 数据类型

  WA_STRUCTURE-INTLEN  =  6.          " 长度

   APPEND WA_STRUCTURE  TO IT_STRUCTURE.

  WA_STRUCTURE-FIELDNAME =  'COL2'. " 第 列名

  WA_STRUCTURE-COL_POS =  2.          " 表示第三列 ---

  WA_STRUCTURE-INTTYPE =  'C'.         " 数据类型

  WA_STRUCTURE-INTLEN  =  6.          " 长度

   APPEND WA_STRUCTURE  TO IT_STRUCTURE.

  WA_STRUCTURE-FIELDNAME =  'COL3'. " 第三列名

  WA_STRUCTURE-COL_POS =  3.          " 表示第三列 ---

  WA_STRUCTURE-INTTYPE =  'C'.         " 数据类型

  WA_STRUCTURE-INTLEN  =  6.          " 长度

   APPEND WA_STRUCTURE  TO IT_STRUCTURE.

ENDFORM.                     "CREATE_STRUCTURE

*&---------------------------------------------------------------------*

*&      Form  CREATE_DYNAMIC_TABLE

*----------------------------------------------------------------------*

FORM CREATE_DYNAMIC_TABLE.

   CALL  METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
     EXPORTING
      IT_FIELDCATALOG = IT_STRUCTURE
     IMPORTING
      EP_TABLE        = DY_TABLE.

   ASSIGN DY_TABLE->*  TO <DYN_TABLE>.

   "表类型指针 <dyn_table> 指向 数据对象的内容.

ENDFORM.   " create_dynamic_table

*&---------------------------------------------------------------------*

*&      Form  write_data_to_dyntable

*&---------------------------------------------------------------------*

FORM WRITE_DATA_TO_DYNTABLE .

   DATA:WA_NEW_LINE  TYPE  REF  TO  DATA.

   DATA: I  TYPE N.

   DATA:J  TYPE N.

   CREATE  DATA WA_NEW_LINE  LIKE  LINE  OF <DYN_TABLE>.

*建立一个与动态内表结构相同的数据对象,且数据对象为是一个结构

   ASSIGN WA_NEW_LINE->*  TO <DYN_WA>. " 用<dyn_wa>指针指向该结构

   DO  3  TIMES.

     I =  I +  1.

     CLEAR J.

     LOOP  AT IT_STRUCTURE  INTO WA_STRUCTURE.

      J = J +  1.

       ASSIGN  COMPONENT WA_STRUCTURE-FIELDNAME  OF  STRUCTURE <DYN_WA>

       TO <DYN_FIELD>.

*   " 用指针<DYN_FIELD>指向工作区<DYN_WA>中的一个字段,字段名为WA_STRUCTURE-FIELDNAME.

       CONCATENATE  I J  INTO <DYN_FIELD>. " 给指针指向的字段赋值

     ENDLOOP.

     APPEND <DYN_WA>  TO <DYN_TABLE>.

   ENDDO.

ENDFORM.                     "WRITE_DATA_TO_DYNTABLE

*&---------------------------------------------------------------------*

*&      Form  OUTPUT_DYNTABLE_DATA

*----------------------------------------------------------------------*

FORM OUTPUT_DYNTABLE_DATA.

*  LOOP AT IT_STRUCTURE INTO WA_STRUCTURE.
*
*    WRITE: WA_STRUCTURE-FIELDNAME(5).
*
*  ENDLOOP.
*
*  LOOP AT <DYN_TABLE> INTO <DYN_WA>.
*
*    WRITE: / .
*
*    LOOP AT IT_STRUCTURE INTO WA_STRUCTURE.
*
*      ASSIGN COMPONENT WA_STRUCTURE-FIELDNAME OF STRUCTURE <DYN_WA>  TO <DYN_FIELD>.
*
**     用指针<DYN_FIELD>指向工作区<DYN_WA>中的一个字段,字段名为WA_STRUCTURE-FIELDNAME.
*
*      WRITE : <DYN_FIELD>.
*
*    ENDLOOP.
*
*  ENDLOOP.

ENDFORM.                     "OUTPUT_DYNTABLE_DATA





*&---------------------------------------------------------------------*
*& Form PREPARE_LIST_VAL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM prepare_list_val.
  prg = sy-repid.
  l_variant- report = sy-repid.
   PERFORM eventtab_build  CHANGING l_events.
   PERFORM comment_build  CHANGING gt_list_top_of_page.
   PERFORM set_layout.
ENDFORM" PREPARE_LIST_VAL
*&---------------------------------------------------------------------*
*& Form EVENTTAB_BUILD
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM eventtab_build  CHANGING p_events  TYPE slis_t_event.

ENDFORM" EVENTTAB_BUILD
*---------------------------------------------------------------------*
* FORM COMMENT_BUILD *
*---------------------------------------------------------------------*
* --> GT_TOP_OF_PAGE *
*---------------------------------------------------------------------*
FORM comment_build  CHANGING gt_top_of_page  TYPE slis_t_listheader.

ENDFORM" COMMENT_BUILD
*&---------------------------------------------------------------------*
*& Form SET_LAYOUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM set_layout.
  gs_layout-zebra =  'X'.
  gs_layout-f2code =  '&ETA'.
  gs_layout-detail_popup =  'X'.
  gs_layout-get_selinfos =  'X'.
ENDFORM" SET_LAYOUT
*&---------------------------------------------------------------------*
*& Form DISPLAY_VAL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM display_val.
   PERFORM get_fieldcat.
   PERFORM get_eventcat.
   PERFORM get_it_sort.
   PERFORM start_list_val.
ENDFORM" DISPLAY_VAL
*&---------------------------------------------------------------------*
*& Form GET_FIELDCAT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM get_fieldcat.

*CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
*    EXPORTING
*      i_program_name         = prg
*      i_internal_tabname     = 'gt_vbap'
*      i_inclname             = prg
*    CHANGING
*      ct_fieldcat            = fieldcat[]
*    EXCEPTIONS
*      inconsistent_interface = 1
*      program_error          = 2
*      OTHERS                 = 3.
*IF sy-subrc <> 0.
*    WRITE: 'SY-SUBRC: ', sy-subrc, 'REUSE_ALV_FIELDCATALOG_MERGE'.
*ENDIF.

   PERFORM init_fieldcat .
*LOOP AT fieldcat.
*    CASE fieldcat-fieldname.
*      WHEN 'MATNR'.
*        fieldcat-tabname = 'gt_vbap'.
*        fieldcat-key = 'X'. " Key Column
*        fieldcat-col_pos = 1. " Position on screen
*        fieldcat-hotspot = ''. " Hotspot column
*        fieldcat-seltext_s = 'Name'.
*        fieldcat-ddictxt = 'S'.
*        fieldcat-outputlen = 18.
*      WHEN 'PSMNG'.
*        fieldcat-tabname = 'gt_vbap'.
*        fieldcat-key = ' '. " Key Column
*        fieldcat-col_pos = 2. " Position on screen
*        fieldcat-hotspot = ' '. " Hotspot column
*        fieldcat-seltext_s = 'Money'.
*        fieldcat-ddictxt = 'S'.
*        fieldcat-outputlen = 15.
*        fieldcat-no_zero = 'X'.
*        fieldcat-do_sum = 'X'. "SUM UPON DISPLAY
*    ENDCASE.
*    MODIFY fieldcat.
*ENDLOOP.
ENDFORM" GET_FIELDCAT
*&---------------------------------------------------------------------*
*& Form GET_EVENTCAT
*&---------------------------------------------------------------------*
*   text
*----------------------------------------------------------------------*
FORM get_eventcat.

ENDFORM" GET_EVENTCAT
*&---------------------------------------------------------------------*
*& Form GET_IT_SORT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM get_it_sort.

*  CLEAR it_sort.
*  it_sort-fieldname = 'KOSTL'.
*
*  it_sort-spos = 1.
*  it_sort-up = 'X'.
**it_sort-subtot = 'X'.
*  APPEND it_sort.

* CLEAR IT_SORT.
* IT_SORT-FIELDNAME = 'AUFNR'.
* IT_SORT-SPOS = 2.
* IT_SORT-UP = 'X'.
* IT_SORT-SUBTOT = ''.
* APPEND IT_SORT.

ENDFORM" GET_IT_SORT
*&---------------------------------------------------------------------*
*& Form START_LIST_VAL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM start_list_val.
   CALL  FUNCTION  'REUSE_ALV_GRID_DISPLAY'
     EXPORTING
      i_callback_program = prg
      is_layout          = gs_layout
      is_variant         = l_variant
      it_fieldcat        = fieldcat[]
      it_sort            = it_sort[]
      i_save             =  'A'
      it_events          = eventcat[]
     TABLES
*      t_outtab           = gt_vbap

      t_outtab           = <DYN_TABLE>
     EXCEPTIONS
      program_error      =  1
       OTHERS             =  2.

ENDFORM" START_LIST_VAL




DEFINE ALV_FIELDSCAT.
  wa_fields-fieldname   = & 1.
  fieldcat-seltext_s = & 1.
  wa_fields-seltext_l   = & 2.
  wa_fields-seltext_m   = & 2.
  wa_fields-seltext_s   = & 2.
  wa_fields- key         = & 3.
  wa_fields-outputlen   = & 4.
  wa_fields- edit        = & 5.
  wa_fields- checkbox    = & 6.
  fieldcat-ddictxt =  'S'.
   if & 7 =  'X'.
    wa_fields-do_sum = & 7.
   endif.

   append wa_fields  to fieldcat.
   clear wa_fields.
END- OF- DEFINITION.





*&---------------------------------------------------------------------*
*&      Form  init_fieldcat
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM init_fieldcat .


   LOOP  AT IT_STRUCTURE  INTO WA_STRUCTURE.

      ALV_FIELDSCAT WA_STRUCTURE-FIELDNAME WA_STRUCTURE-FIELDNAME  ''  '20'  ''  ''  ''.

   ENDLOOP.

*

ENDFORM.                     " INIT_FIELDCAT



*&---------------------------------------------------------------------*
*&      Form  CHECKAUTHOR
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM CHECKAUTHOR .
   DATA nums  type  I.


ENDFORM.                     " CHECKAU

你可能感兴趣的:(动态)