- 用户有些需求要求ALV某些数值要用颜色着重的体现出来。
- ALV 颜色设置有行颜色、列颜色、单元格颜色
- 颜色值的设置:C+X(1~7)X(是否加重:1/0)X(是否反色:1/0),附:颜色代码
- 设置列颜色:Fieldcat参数emphasize赋值
** Fieldcat 变量定义 DATA:gt_fieldcat TYPE lvc_t_fcat, gs_fieldcat LIKE LINE OF gt_fieldcat. gs_fieldcat-emphasize = 'C6'."设置列颜色 APPEND gs_fieldcat TO gt_fieldcat.
- 设置行颜色:1.内表增加一个字段,char类型,长度为4位->2.内表赋值->3.设置Layout参数INFO_FNAME赋值,注意:REUSE_ALV_GRID_DISPLAY定义的Layout结构的字段参数是INFO_FIELDNAME
"1.内表结构定义 TYPES:BEGIN OF ty_data, color1 TYPE c LENGTH 4, "颜色字段 END OF ty_data. "2.内表赋值 LOOP AT gt_data INTO gw_data. IF gw_data-score < 60. gw_data-color = 'C6'. ENDIF. MODIFY gt_data FROM gw_data. ENDLOOP. "3.Layout参数字段赋值 gs_layout-info_fname = 'COLOR1'
- 设置单元格颜色:1.内表结构增加一个类型为LVC_T_SCOL内表的字段>2.内表赋值->3.设置Layout参数CTAB_FNAME赋值,注意:REUSE_ALV_GRID_DISPLAY定义的Layout结构的字段参数是CTAB_FIELDNAME
"1.内表结构定义 TYPES:BEGIN OF ty_data, color2 TYPE lvc_t_scol, "内表参数 END OF ty_data. "单元格颜色结构变量定义 DATA gs_color TYPE lvc_S_scol. "2.内表赋值 gs_color-fname = 'SEL'. "单元格颜色设置-选择SEL增加颜色 gs_color-color-col = 6. "单元格颜色设置-底色为红色 gs_color-color-int = 1. "单元格颜色设置-加重 gs_color-color-inv = 0. "单元格颜色设置-不反色 gs_color-NOKEYCOL = '1'."覆盖单元格原色 APPEND gs_color TO gw_data-color2. CLEAR: gs_color. "3.Layout参数赋值 gs_layout-ctab_fname = 'COLOR2'."单元格颜色参数
- 完整代码
TYPES:BEGIN OF ty_data, sel TYPE char1, light TYPE iconname, name TYPE char10, score TYPE p LENGTH 2 DECIMALS 1, remark TYPE c LENGTH 20, color1 TYPE c LENGTH 4, color2 TYPE lvc_t_scol, END OF ty_data. DATA gt_data TYPE TABLE OF ty_data. DATA gw_data TYPE ty_data. ** Layout 变量定义 DATA gs_layout TYPE lvc_s_layo. ** 颜色字段变量定义 DATA gs_color TYPE lvc_S_scol. ** Fieldcat 变量定义 DATA:gt_fieldcat TYPE lvc_t_fcat, gs_fieldcat LIKE LINE OF gt_fieldcat. ** FieldCat的宏 DATA gv_pos TYPE i. DEFINE %%add_fieldcat. gv_pos = gv_pos + 1. gs_fieldcat-col_pos = gv_pos. gs_fieldcat-fieldname = &1. gs_fieldcat-scrtext_m = &2. CASE &1. WHEN 'SEL'. gs_fieldcat-checkbox = 'X'. "当做选择框显示 gs_fieldcat-edit = 'X' . "该字段可以编辑 WHEN 'SCORE'. gs_fieldcat-emphasize = 'C6'."设置列颜色 ENDCASE. APPEND gs_fieldcat TO gt_fieldcat.CLEAR gs_fieldcat. END-OF-DEFINITION. START-OF-SELECTION. PERFORM get_data. PERFORM dis_falv. FORM get_data. gt_data[] = VALUE #( ( sel = '' light = '' name = 'TOM' score = '58') ( sel = '' light = '' name = 'JEERY' score = '98') ( sel = '' light = '' name = 'MASTER' score = '68') ). LOOP AT gt_data INTO gw_data. IF gw_data-score < 60. gw_data-light = icon_red_light. gw_data-remark = '不及格'. gw_data-color1 = 'C6'. ELSEIF gw_data-score >= 60 AND gw_data-score < 85. gw_data-light = ICON_yellow_LIGHT. gw_data-remark = '良好'. ELSEIF gw_data-score >= 85. gw_data-light = ICON_green_LIGHT. gw_data-remark = '优秀'. gs_color-fname = 'SEL'. "单元格颜色设置-选择SEL增加颜色 gs_color-color-col = 6. "单元格颜色设置-底色为红色 gs_color-color-int = 1. "单元格颜色设置-加重 gs_color-color-inv = 0. "单元格颜色设置-不反色 gs_color-NOKEYCOL = '1'."覆盖单元格原色 APPEND gs_color TO gw_data-color2. CLEAR: gs_color. ENDIF. MODIFY gt_data FROM gw_data. ENDLOOP. ENDFORM. FORM dis_falv. ** Fieldcat字段映射处理 %%add_fieldcat:'SEL' '选择', "( 前面字段值不可变 后面字段描述可变 每一行都是) 'LIGHT' '状态', 'REMARK' '备注', 'NAME' '姓名', 'SCORE' '分数'. ** Layout布局处理 gs_layout-cwidth_opt = 'X'. "自动优化列宽 (可以使用**在一行代码面前注释掉 看看ALV会有什么效果) gs_layout-zebra = 'X'. "斑马纹输出 gs_layout-info_fname = 'COLOR1'."行颜色参数 gs_layout-ctab_fname = 'COLOR2'."单元格颜色参数 ** 调用FunctionALV需要传入的参数:展示数据,FieldCat,Layout,Status CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC' EXPORTING i_callback_program = sy-repid i_callback_pf_status_set = 'FRM_SET_STATUS' is_layout_lvc = gs_layout it_fieldcat_lvc = gt_fieldcat TABLES t_outtab = gt_data[] EXCEPTIONS program_error = 1 OTHERS = 2. ENDFORM. FORM frm_set_status USING p_extab TYPE slis_t_extab. SET PF-STATUS 'STANDARD'. ENDFORM.
ABAP - Function ALV 09 颜色设置(行、列、单元格)
最新推荐文章于 2024-08-23 14:43:30 发布