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. 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
标签:gt,内表,sflight,cat,实例,alv,table,动态,ref From: https://www.cnblogs.com/freeandeasy/p/16850835.html