ABAP-动态内表
动态内表的创建和使用主要有以下几点: 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