REPORT ZTEST.
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." 从动态内表中取数,并写到屏幕
*&---------------------------------------------------------------------*
*& Form CREATE_STRUCTURE
*----------------------------------------------------------------------*
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>