動態內表的創建和使用主要有以下幾點: 1.動態內表的創建,首先要定義動態結構,然后再根據定義的動態結構利用系統一個標准的method:”cl_alv_table_create=>create_dynamic_table“生成動態內表。 2.動態內表的賦值,遍歷動態結構,獲取指定的字段,然后給指定的字段賦值。 3.動態內表的讀取顯示,與賦值相似,遍歷動態結構,獲取指定的字段,然后讀取對應字段的值。 具體實現方法如下所示: 定義: FIELD-SYMBOLS:<dyn_table> TYPE STANDARD TABLE, <dyn_wa> TYPE ANY, <dyn_field> TYPE ANY. 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. 實現步驟: 1.PERFORM create_structure." 定義內表的結構 FORM create_structure. CLEAR it_structure. DATA:l_pos TYPE i. l_pos = 5. wa_structure-fieldname = 'MANUFACTURER'." 第一列名 wa_structure-col_pos = 1. " 表示第1列 --- wa_structure-ref_table = 'PLAF'. ”字段參照的內表名字 wa_structure-ref_field = 'FLIEF'. “字段參照的字段名字 APPEND wa_structure TO it_structure. wa_structure-fieldname = 'NAME'. " 第二 列名 wa_structure-col_pos = 2. " 表示第2列 --- wa_structure-ref_table = 'lfa1'. wa_structure-ref_field = 'NAME1'. APPEND wa_structure TO it_structure. wa_structure-fieldname = 'DESCRIPTION'. " 第三列名 wa_structure-col_pos = 3. " 表示第3列 --- wa_structure-ref_table = 'MAKT'. wa_structure-ref_field = 'MAKTX'. APPEND wa_structure TO it_structure. wa_structure-fieldname = 'MM#'. wa_structure-col_pos = 4. wa_structure-ref_table = 'MAKT'. wa_structure-ref_field = 'MATNR'. APPEND wa_structure TO it_structure. "it_month是根據選擇屏幕的起始時間和結束時間得到的兩個日期之間的月份,以下遍歷所得到的月份並寫入結構。 LOOP AT it_month INTO wa_month. wa_structure-fieldname = wa_month-pedtr. wa_structure-col_pos = l_pos. wa_structure-inttype = 'P'. "wa_structure-intlen = 7. APPEND wa_structure TO it_structure. l_pos = l_pos + 1. ENDLOOP. ENDFORM. "CREATE_STRUCTURE 2.PERFORM 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 3.PERFORM write_data_to_dyntable." 向動態內表中寫數 FORM write_data_to_dyntable . DATA:wa_new_line TYPE REF TO data. DATA:l_index2 TYPE i. CREATE DATA wa_new_line LIKE LINE OF <dyn_table>. *建立一個與動態內表結構相同的數據對象,且數據對象為是一個結構 ASSIGN wa_new_line->* TO <dyn_wa>." 用<dyn_wa>指針指向該結構 SORT it_plaf_out BY flief matnr . LOOP AT it_plaf_out INTO wa_plaf_out. l_index2 = sy-tabix. LOOP AT it_structure INTO wa_structure. READ TABLE it_plaf_out INTO wa_plaf_out INDEX l_index2. IF wa_structure-fieldname = 'MANUFACTURER'. ASSIGN COMPONENT wa_structure-fieldname OF STRUCTURE <dyn_wa> TO <dyn_field>. " 用指針<DYN_FIELD>指向工作區<DYN_WA>中的一個字段,字段名為WA_STRUCTURE-FIELDNAME. <dyn_field> = wa_plaf_out-flief. ”給指針指向的字段賦值 ELSEIF wa_structure-fieldname = 'NAME'. ASSIGN COMPONENT wa_structure-fieldname OF STRUCTURE <dyn_wa> TO <dyn_field>. <dyn_field> = wa_plaf_out-name1. ELSEIF wa_structure-fieldname = 'DESCRIPTION'. ASSIGN COMPONENT wa_structure-fieldname OF STRUCTURE <dyn_wa> TO <dyn_field>. <dyn_field> = wa_plaf_out-maktx. ELSEIF wa_structure-fieldname = 'MM#'. ASSIGN COMPONENT wa_structure-fieldname OF STRUCTURE <dyn_wa> TO <dyn_field>. <dyn_field> = wa_plaf_out-matnr. ELSEIF wa_structure-fieldname = wa_plaf_out-pedtr. ASSIGN COMPONENT wa_structure-fieldname OF STRUCTURE <dyn_wa> TO <dyn_field>. <dyn_field> = wa_plaf_out-gsmng. ENDIF. ENDLOOP. AT END OF matnr. APPEND <dyn_wa> TO <dyn_table>."將所得到的工作區的值寫入內表 CLEAR <dyn_wa>. ENDAT. ENDLOOP. ENDFORM. "WRITE_DATA_TO_DYNTABLE PERFORM 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. 若要通過ALV顯示的話。 利用wa_alv_field定義對應結構的ALV,然后即可顯示 g_repid = sy-repid. wa_layout-colwidth_optimize = 'X'. wa_layout-zebra = 'X'. " lt_layout-box_fieldname = 'SEL'. DATA:l_colpos TYPE i. l_colpos = 5. " lit_fcat wa_alv_field-col_pos = 1. wa_alv_field-fieldname = 'MANUFACTURER'. wa_alv_field-seltext_m = 'MANUFACTURER'. wa_alv_field-no_zero = 'X'. APPEND wa_alv_field TO lit_fcat. wa_alv_field-col_pos = 2. wa_alv_field-fieldname = 'NAME'. wa_alv_field-seltext_m = 'NAME'. APPEND wa_alv_field TO lit_fcat. wa_alv_field-col_pos = 3. wa_alv_field-fieldname = 'DESCRIPTION'. wa_alv_field-seltext_m = 'DESCRIPTION'. APPEND wa_alv_field TO lit_fcat. wa_alv_field-col_pos = 4. wa_alv_field-fieldname = 'MM#'. wa_alv_field-seltext_m = 'MM#'. wa_alv_field-no_zero = 'X'. APPEND wa_alv_field TO lit_fcat. LOOP AT it_month INTO wa_month. wa_alv_field-col_pos = l_colpos. wa_alv_field-fieldname = wa_month-pedtr. wa_alv_field-seltext_m = wa_month-pedtr. APPEND wa_alv_field TO lit_fcat. l_colpos = l_colpos + 1. ENDLOOP. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' 關鍵技巧: 1, 創建動態內表: a, 動態內表的結構的定義. 動態內表表結構的定義必須使用表結構與 table type: lvc_t_fcat一樣的內表. 一般情況下,我們都內表的所有列定義成字符型. b, 根據表結構生成內表. 系統提供了一個標准的method來產生動態表,使用方法如下: 2, 動態內表的賦值: a, 獲取指定的字段 b, 給指定的字段賦值 3, 讀取動態內表的值: a, 獲取指定的字段 b, 讀取指定的字段值 *&---------------------------------------------------------------------* *& Report YDYN_TEST *&---------------------------------------------------------------------* REPORT YDYN_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 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>. *用指針<DYN_FIELD>指向工作區<DYN_WA>中的一個字段,字段名為WA_STRUCTURE-FIELDNAME. WRITE : <DYN_FIELD>. ENDLOOP. ENDLOOP. ENDFORM. "OUTPUT_DYNTABLE_DATA
