動態ABAP內表
阿新 • • 發佈:2019-01-03
REPORT ZLEIY_ABAP_TEST.
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
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 =
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
注:建立完的IT_STRUCTURE是這樣的
WA_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.
參考
http://www.cnblogs.com/VerySky/articles/2514436.html,都能產生類似的效果,其中REPORT ZCL_TEST028
最為簡單