ABAP-动态ALV

1.参数定义

"ALV
type-pools:slis,rsds,vrm.
data:gt_fieldcat type lvc_t_fcat with header line,
     gt_events type slis_t_event with header line,
     gs_layout type lvc_s_layo,
     g_repid like sy-repid.
data:wa_sort type slis_sortinfo_alv,
     it_sort type lvc_t_sort,
     lw_alv_title type lvc_title.
data:l_grid type ref to cl_gui_alv_grid,
     ls_stable type lvc_s_stbl.

data:struct_type type ref to cl_abap_structdescr,
     itab_type   type ref to cl_abap_tabledescr,
     elem_type   type ref to cl_abap_elemdescr,
     comp_type   type cl_abap_structdescr=>component_table,
     comp_field  type cl_abap_structdescr=>component,
     comp_line   like line of comp_type,
     dref        type ref to data,
     dline       type ref to data.

field-symbols:<ztab> type standard table,
              <itab> type any,
              <fild> type any,
              <frat> type any.

"dynamic alv parameters
data:ztab type standard table of tab with header line,
     mtab type standard table of tab with header line.

2.结构生成

form structure_data.

  struct_type ?= cl_abap_typedescr=>describe_by_name( 'TAB' ).
  comp_type = struct_type->get_components( ).

  perform zdtb_ms using:
    'DATAB'       '日期从',
    'DATBI'       '日期至'.

  "****** 增加字段 vkorg+fkdat+ekgrp+kunnr+vkbur+ktgrm+matnr+matkl+maabc+kosch+labor+groes

  perform struc_xz tables s_vkorg using 'VKORG'  '销售组织'   'VBRK-VKORG'  'VTEXT'  'TVKOT-VTEXT'.
  "perform struc_xz tables s_fkdat using 'FKDAT'  '开票日期'   'VBRK-FKDAT'  ''       ''.
  perform struc_xz tables s_fkart using 'FKART'  '订单类型'   'VBRK-FKART'  'FKTXT'  'TVFKT-VTEXT'.
  perform struc_xz tables s_ekgrp using 'EKGRP'  '品牌'       'MARC-EKGRP'  'EKNAM'  'T024-EKNAM'.
  perform struc_xz tables s_kunnr using 'KUNAG'  '客户编码'   'VBRK-KUNAG'  'NAME1'  'KNA1-NAME1'.
  perform struc_xz tables s_vkbur using 'VKBUR'  '产品线'     'KNVV-VKBUR'  'BEZEI'  'TVKBT-BEZEI'.
  perform struc_xz tables s_ktgrm using 'KTGRM'  '科目设置组' 'VBRP-KTGRM'  'KMEXT'  'TVKMT-VTEXT'.
  perform struc_xz tables s_matnr using 'MATNR'  '物料号'     'VBRP-MATNR'  'MAKTX'  'MAKT-MAKTX'.

  perform struc_xz tables s_matkl using 'MATKL'  '物料组'     'MARA-MATKL'  'WGBEZ'  'T023T-WGBEZ'.
  perform struc_xz tables s_maabc using 'MAABC'  '等级'       'MARC-MAABC'  'TMABC'  'TMABCT-TMABC'.
  perform struc_xz tables s_kosch using 'KOSCH'  '产品分配'   'MARA-KOSCH'  'KOTXT'  'T190ST-VTEXT'.
  perform struc_xz tables s_labor using 'LABOR'  '产品档次'   'MARA-LABOR'  'LBTXT'  'T024X-LBTXT'.
  perform struc_xz tables s_groes using 'GROES'  '规格'       'MARA-GROES'  ''       ''.

  "*****************************

  perform zdtb_ms using:
    'FKIMG'       '数量',
    'NETWR'       '金额',
    'VOLUM'       '面积',
    'NTGEW'       '重量',
    'VOLTJ'       '体积',
    'SLRAT'       '数量占比(%)',
    'JERAT'       '金额占比(%)',
    'WAVWR'       '成本',
    'MLNET'       '毛利',
    'MLRAT'       '毛利率(%)'.

  struct_type = cl_abap_structdescr=>create( comp_type ).
  itab_type   = cl_abap_tabledescr=>create( struct_type ).

  create data dref type handle itab_type.
  assign dref->* to <ztab>.

endform.

form field_app using zd zelem.
  clear:comp_line,elem_type.
  comp_line-name = zd.
  elem_type ?= cl_abap_elemdescr=>describe_by_name( zelem ).
  comp_line-type = elem_type.
  append comp_line to comp_type.
endform.

form zdtb_ms using zd zdms.
  zdtb-zd = zd.
  zdtb-zdms = zdms.
  append zdtb.
  clear:zdtb.
endform.

3.赋值

 create data dline like line of <ztab>.
  assign dline->* to <itab>.

  loop at mtab.
    move-corresponding mtab to <itab>.
    append <itab> to <ztab>.
    clear:ztab,<itab>.
  endloop.

  clear:mtab. refresh:mtab.

  loop at <ztab> assigning <itab>.
    clear:mtab.
    move-corresponding <itab> to mtab.
    move-corresponding mtab to <itab>.
    clear:mtab.
  endloop.
原文地址:https://www.cnblogs.com/ricoo/p/10169872.html