关键技巧:
1.创建动态内表:
a.动态内表的结构的定义
动态内表结构的定义必须使用表结构与table type:lvc_t_fcat一样的内表。
一般情况下,我们把内表的所有列定义成字符型。
b.根据表结构生成内表。
系统提供了一个标准的method来产生动态表,使用方法见代码。
2.动态内表的赋值
a.获取指定的字段
b.给指定的字段赋值
3.读取动态内表的值
a.获取指定的字段
b.读取指定的字段值
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
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
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.
CLEAR wa_structure.
wa_structure-fieldname = 'COL2'.
wa_structure-col_pos = 2.
wa_structure-inttype = 'C'.
wa_structure-intlen = 6.
APPEND wa_structure TO it_structure.
CLEAR wa_structure.
wa_structure-fieldname = 'COL3'.
wa_structure-col_pos = 3.
wa_structure-inttype = 'C'.
wa_structure-intlen = 6.
APPEND wa_structure TO it_structure.
CLEAR wa_structure.
wa_structure-fieldname = 'COL4'.
wa_structure-col_pos = 4.
wa_structure-inttype = 'C'.
wa_structure-intlen = 6.
APPEND wa_structure TO it_structure.
CLEAR wa_structure.
ENDFORM. "create_structure
*&---------------------------------------------------------------------*
*& Form CREATE_DYNAMIC_TABLE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
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>?
因为下面要取其行结构,用dy_table->*是通不过语法的
ENDFORM. "CREATE_DYNAMIC_TABLE
*&---------------------------------------------------------------------*
*& Form write_data_to_dyntable
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
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>.
CONCATENATE i j INTO <dyn_field>. "给指定的对象赋值
ENDLOOP.
APPEND <dyn_wa> TO <dyn_table>.
ENDDO.
ENDFORM. "write_data_to_dyntable
*&---------------------------------------------------------------------*
*& Form
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
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>.
WRITE: <dyn_field>.
ENDLOOP.
ENDLOOP.
ENDFORM. "output_dyntable_data