ABAP随笔-动态内表添加颜色控制列
当我们做动态内表相关开发时,有种方法时根据Filedcat生成内表,如果想往内表中加入颜色/可编辑等控制型的字段,是不好直接加的,因为Fieldcat无法指定表类型字段。
这是我们可以通过指针的方式向动态内表里增加表结构类型的字段
1.通过指定类型,指定字段名的方式增加字段
DATA:dy_table TYPE REF TO data, dy_line TYPE REF TO data, itab_type TYPE REF TO cl_abap_tabledescr, struct_type TYPE REF TO cl_abap_structdescr, comp_tab TYPE cl_abap_structdescr=>component_table WITH HEADER LINE. "#根据Fieldcat创建动态内表 CALL METHOD cl_alv_table_create=>create_dynamic_table EXPORTING it_fieldcatalog = gt_fieldcat IMPORTING ep_table = dy_table. ASSIGN dy_table->* TO <dyn_table>. CREATE DATA dy_line LIKE LINE OF <dyn_table>. ASSIGN dy_line->* TO <dyn_wa>. IF dy_table IS BOUND. ASSIGN dy_table->* TO <dyn_table>. itab_type ?= cl_abap_tabledescr=>describe_by_data( <dyn_table> ). struct_type ?= itab_type->get_table_line_type( ). comp_tab[] = struct_type->get_components( ). comp_tab-name = 'COLOR'. struct_type ?= cl_abap_typedescr=>describe_by_name( 'LVC_S_SCOL' ). comp_tab-type = cl_abap_tabledescr=>create( struct_type ). INSERT comp_tab INTO comp_tab INDEX 1. struct_type = cl_abap_structdescr=>create( p_components = comp_tab[] p_strict = abap_false ). itab_type = cl_abap_tabledescr=>create( struct_type ). CREATE DATA dy_table TYPE HANDLE itab_type. ASSIGN dy_table->* TO <dyn_table>. CREATE DATA dy_line LIKE LINE OF <dyn_table>. ASSIGN dy_line->* TO <dyn_wa>. ENDIF.
2.向内表中相应控制字段赋值
"#当日产能盈余 lv_fieldname = |DAY{ lv_index }-LEFT|. ASSIGN COMPONENT lv_fieldname OF STRUCTURE <dyn_wa> TO <fs_field>. IF sy-subrc EQ 0. <fs_field> = gs_alv-zdaily_capacity - gs_data_collect-zso_qty. ls_color-fname = lv_fieldname. IF <fs_field> GT 0. ls_color-color = VALUE #( col = '5' int = '0' inv = '0' ). ELSEIF <fs_field> LT 0. ls_color-color = VALUE #( col = '6' int = '0' inv = '0' ). ELSE. ls_color-color = VALUE #( col = '3' int = '0' inv = '0' ). ENDIF. INSERT ls_color INTO TABLE lt_color. ASSIGN COMPONENT 'COLOR' OF STRUCTURE <dyn_wa> TO FIELD-SYMBOL(<fs_color>). IF sy-subrc EQ 0. <fs_color> = lt_color. ENDIF.
3.同样要设定Layout里控制字段名
gs_layout-ctab_fname = 'COLOR'.