动态内表 cl_alv_table_create=>create_dynamic_table

cl_alv_table_create=>create_dynamic_table  

report ztest16.

tables: sflight.
data:gt_sflight like table of sflight with header line.

data:lt_alv_cat type table of lvc_s_fcat,
     ls_alv_cat like line of  lt_alv_cat.
data:i_layout_lvc type lvc_s_layo .

data:ref_itab   type ref to data,
     ref_struct type ref to data. "引用变量存储的是某个对象的地址
field-symbols:<itab>   type table,
              <struct>.

end-of-selection.
  perform pf_get_data.
  perform pf_set_fieldcat.
  perform pf_create_innertable. "给动态内表赋值
  perform pf_alv_display.

form pf_get_data .
  select * from sflight into table gt_sflight.
endform.                    " PF_GET_DATA

form pf_set_fieldcat .
  define set_fieldcat.
    ls_alv_cat-fieldname = &1.
    ls_alv_cat-scrtext_s = &2.
    ls_alv_cat-datatype = &3.
    ls_alv_cat-intlen = &4.
    ls_alv_cat-ref_table = &5.
    ls_alv_cat-ref_field = &6.
    append ls_alv_cat to lt_alv_cat.
    clear ls_alv_cat.
  end-of-definition.

  set_fieldcat:'CARRID' '航线代码' 'CHAR' '3' 'SFLIGHT' 'CARRID',
                'CONNID' '航班连接 Id' 'NUMC' '4' '' '',
                'FLDATE' '航班日期' 'CHAR' '3' '' '',
                'PRICE' '航空运费' 'CURR' '15' 'SFLIGHT' 'PRICE',
                'PAYMENTSUM' '当前预定总数' 'CURR' '17' 'SFLIGHT' 'PAYMENTSUM',
                'CURRENCY' '航班的本地货币' 'CUKY' '5' '' ''.


*额外新增自定义字段
  set_fieldcat:'ZSUM' '本次航班总收入' 'CURR' '17' 'SFLIGHT' 'PAYMENTSUM'.

*layout
  i_layout_lvc-zebra       = 'X' ." 使ALV界面呈现颜色交替
  i_layout_lvc-cwidth_opt       = 'X' ." 自动优化列宽
  i_layout_lvc-detailinit      = 'X' ." 是否出现细节屏幕
endform.                    " PF_SET_FIELDCAT

form pf_create_innertable .
  field-symbols <fs_value>.

***创建动态字段内表
  call method cl_alv_table_create=>create_dynamic_table
    exporting
      it_fieldcatalog = lt_alv_cat
    importing
      ep_table        = ref_itab.

*指定生成的内表到字段符号
  assign ref_itab->* to <itab>.

*创建动态工作区结构
  create data ref_struct like line of <itab>.   "根据表对象创建行对象
  assign ref_struct->* to <struct>.             " 将行对象分配给到字段符号 


  loop at gt_sflight.
    move-corresponding gt_sflight to <struct>.
*    assign component 'CARRID' of structure <struct> to <fs_value>.  ":改变<fs_value>值就是改变对应字段值
*    <fs_value> = gt_sflight-carrid.
*    assign component 'CONNID' of structure <struct> to <fs_value>.
*    <fs_value> = gt_sflight-connid.
*    assign component 'FLDATE' of structure <struct> to <fs_value>.
*    <fs_value> = gt_sflight-fldate.
*    assign component 'PRICE' of structure <struct> to <fs_value>.
*    <fs_value> = gt_sflight-price.
*    assign component 'PAYMENTSUM' of structure <struct> to <fs_value>.
*    <fs_value> = gt_sflight-paymentsum.
*    assign component 'CURRENCY' of structure <struct> to <fs_value>.
*    <fs_value> = gt_sflight-currency.

    assign component 'ZSUM' of structure <struct> to <fs_value>.    "给自定义字段赋值
    <fs_value> = gt_sflight-paymentsum * gt_sflight-price.

    append <struct> to <itab>.
  endloop.
endform.                    " PF_CREATE_INNERTABLE

form pf_alv_display .
*显示内表中的数据
  call function 'REUSE_ALV_GRID_DISPLAY_LVC'
    exporting
      i_callback_program = sy-repid          " 当前程序
      is_layout_lvc      = i_layout_lvc      " 布局属性
      it_fieldcat_lvc    = lt_alv_cat  " 列明内表
    tables
      t_outtab           = <itab>             " 数据内表
    exceptions
      program_error      = 1
      others             = 2.

endform.                    " PF_ALV_DISPLAY

 

posted @ 2022-11-02 14:00  肆意点  阅读(235)  评论(0编辑  收藏  举报