动态内表的创建和使用主要有以下几点:
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,
<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.“lvc_t_fact的工作区。
it_structure TYPE lvc_t_fcat,”存储动态内表列的内表,其中的每一行代表动态内表的一列。
wa_structure TYPE lvc_s_fcat.“lvc_t_fact的工作区。
②通过lvc_t_fact构建动态内表的列:
FORM setstructrue USING value(p_fname) TYPE char6
value(p_col) TYPE int4
value(p_tab) TYPE char10
value(p_fld) TYPE char6.
wa_structure-fieldname = p_fname." 列名
wa_structure-col_pos = p_col.“列的顺序
wa_structure-ref_table = p_tab.”参照表
wa_structure-ref_field = p_fldtabletable.“参照字段
APPEND wa_structure TO it_structure.
ENDFORM. "setstructrue
value(p_col) TYPE int4
value(p_tab) TYPE char10
value(p_fld) TYPE char6.
wa_structure-fieldname = p_fname." 列名
wa_structure-col_pos = p_col.“列的顺序
wa_structure-ref_table = p_tab.”参照表
wa_structure-ref_field = p_fldtabletable.“参照字段
APPEND wa_structure TO it_structure.
ENDFORM. "setstructrue
③通过lvc_t_fact构建创建动态内表给dy_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>.
ENDFORM. " 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>.
ENDFORM. " CREATE_DYNAMIC_TABLE
④给动态内表赋值:
FORM setdata .
DATA:wa_new_line TYPE REF TO data.
DATA lv_tol TYPE wertv12.
CREATE DATA wa_new_line LIKE LINE OF <dyn_table>.
DATA:wa_new_line TYPE REF TO data.
DATA lv_tol TYPE wertv12.
CREATE DATA wa_new_line LIKE LINE OF <dyn_table>.
ASSIGN wa_new_line->* TO <dyn_wa>.”让<dyn_wa>代表指向动态内表的工作区
LOOP AT gt_coep2.
CLEAR <dyn_wa>.
READ TABLE it_structure INTO wa_structure INDEX 4.“获取it_structure的第四行,即动态内表的第四列
ASSIGN COMPONENT wa_structure-fieldname OF STRUCTURE <dyn_wa> TO <dyn_field>.”当<dyn_field>指向工作区中的第四个字段
PERFORM removezero USING gt_coep2-kstar.
<dyn_field> = gt_coep2-kstar.“通过给field-symbol赋值来达到给工作区中第四个字段赋值的作用。
PERFORM addzero USING gt_coep2-kstar.
READ TABLE it_structure INTO wa_structure INDEX 5.
ASSIGN COMPONENT wa_structure-fieldname OF STRUCTURE <dyn_wa> TO <dyn_field>.
<dyn_field> = gt_coep2-txt50.
LOOP AT gt_coep2.
CLEAR <dyn_wa>.
READ TABLE it_structure INTO wa_structure INDEX 4.“获取it_structure的第四行,即动态内表的第四列
ASSIGN COMPONENT wa_structure-fieldname OF STRUCTURE <dyn_wa> TO <dyn_field>.”当<dyn_field>指向工作区中的第四个字段
PERFORM removezero USING gt_coep2-kstar.
<dyn_field> = gt_coep2-kstar.“通过给field-symbol赋值来达到给工作区中第四个字段赋值的作用。
PERFORM addzero USING gt_coep2-kstar.
READ TABLE it_structure INTO wa_structure INDEX 5.
ASSIGN COMPONENT wa_structure-fieldname OF STRUCTURE <dyn_wa> TO <dyn_field>.
<dyn_field> = gt_coep2-txt50.
READ TABLE it_structure INTO wa_structure INDEX 6.
ASSIGN COMPONENT wa_structure-fieldname OF STRUCTURE <dyn_wa> TO <dyn_field>.
PERFORM getksttol USING gt_coep2-kstar
lv_tol.
<dyn_field> = lv_tol.
LOOP AT gt_coep1 WHERE kstar = gt_coep2-kstar.
ASSIGN COMPONENT gt_coep1-objnr6 OF STRUCTURE <dyn_wa> TO <dyn_field>.
<dyn_field> = gt_coep1-wkgbtr.
ENDLOOP.
APPEND <dyn_wa> TO <dyn_table>.”将工作区中的数据append到动态内表中去
ENDLOOP.
ENDFORM.
ASSIGN COMPONENT wa_structure-fieldname OF STRUCTURE <dyn_wa> TO <dyn_field>.
PERFORM getksttol USING gt_coep2-kstar
lv_tol.
<dyn_field> = lv_tol.
LOOP AT gt_coep1 WHERE kstar = gt_coep2-kstar.
ASSIGN COMPONENT gt_coep1-objnr6 OF STRUCTURE <dyn_wa> TO <dyn_field>.
<dyn_field> = gt_coep1-wkgbtr.
ENDLOOP.
APPEND <dyn_wa> TO <dyn_table>.”将工作区中的数据append到动态内表中去
ENDLOOP.
ENDFORM.
⑤动态内表ALV展示时构建fieldcatalog,通过循环it_structure来实现。
FORM build_fieldcat1 .
DATA: lv_name TYPE string,
lv_fname TYPE prctr,
lv_sum,
lv_key.
CLEAR gt_fieldcat[].
LOOP AT it_structure INTO wa_structure.
IF wa_structure-fieldname <> 'MARK'.
CLEAR lv_name.
PERFORM fill_fieldcat1 USING wa_structure-fieldname
wa_structure-col_pos
lv_name
lv_key
lv_sum.
ENDIF.
DATA: lv_name TYPE string,
lv_fname TYPE prctr,
lv_sum,
lv_key.
CLEAR gt_fieldcat[].
LOOP AT it_structure INTO wa_structure.
IF wa_structure-fieldname <> 'MARK'.
CLEAR lv_name.
PERFORM fill_fieldcat1 USING wa_structure-fieldname
wa_structure-col_pos
lv_name
lv_key
lv_sum.
ENDIF.
ENDLOOP.
ENDFORM. "build_fieldcat1
ENDFORM. "build_fieldcat1
FORM fill_fieldcat1 USING value(p_fname)
value(p_col)
value(p_txt)
value(p_key)
value(p_sum).
gs_fieldcat-tabname = '<dyn_table>'.
gs_fieldcat-seltext_m = p_txt.
gs_fieldcat-fieldname = p_fname.
gs_fieldcat-col_pos = p_col.
gs_fieldcat-key = p_key.
gs_fieldcat-do_sum = p_sum.
APPEND gs_fieldcat TO gt_fieldcat.
ENDFORM. "fill_fieldcat1
value(p_col)
value(p_txt)
value(p_key)
value(p_sum).
gs_fieldcat-tabname = '<dyn_table>'.
gs_fieldcat-seltext_m = p_txt.
gs_fieldcat-fieldname = p_fname.
gs_fieldcat-col_pos = p_col.
gs_fieldcat-key = p_key.
gs_fieldcat-do_sum = p_sum.
APPEND gs_fieldcat TO gt_fieldcat.
ENDFORM. "fill_fieldcat1
⑤ALV展示动态内表
FORM excute_alv_function1.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
i_callback_pf_status_set = 'SET_STATUS'
i_structure_name = '<dyn_wa>'
is_layout = gs_layout_alv
it_fieldcat = gt_fieldcat[]
it_sort = gt_sort[]
it_events = gt_events[] "IT_SORT = GT_SORT
TABLES
t_outtab = <dyn_table>[].
IF sy-subrc <> 0.
ENDIF.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
i_callback_pf_status_set = 'SET_STATUS'
i_structure_name = '<dyn_wa>'
is_layout = gs_layout_alv
it_fieldcat = gt_fieldcat[]
it_sort = gt_sort[]
it_events = gt_events[] "IT_SORT = GT_SORT
TABLES
t_outtab = <dyn_table>[].
IF sy-subrc <> 0.
ENDIF.
ENDFORM. " EXCUTE_REUSE_ALV_GRID_DISPLAY
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论