这里介绍三种类型的颜色:列的背景色,单元格的背景色,单元格文本的颜色。
1.给ALV结构添加颜色控制字段:
三个字段都是同一类型:WDY_UIE_LIBRARY_ENUM_TYPE
COL_BACKG:列背景色
CELL_BACKG:单元格背景色
TEXT_BACKG:单元格文本背景色。
2.初始化ALV时,设置对应的字段和颜色控制
CASE ls_column-id. WHEN 'POSTING_DATE'. ls_column-r_column->set_cell_design( cl_wd_abstr_master_table_col=>e_cell_design-positive ). WHEN 'OBJECT_ID'. ls_column-r_column->set_cell_design_fieldname( 'CELL_BACKG' ). WHEN 'STAT'. DATA lr_cell_editor TYPE REF TO cl_salv_wd_uie. DATA lr_text_view TYPE REF TO cl_salv_wd_uie_text_view. lr_cell_editor = ls_column-r_column->get_cell_editor( ). IF lr_cell_editor->editor = if_salv_wd_c_column_settings=>cell_editor_text_view. lr_text_view ?= lr_cell_editor. lr_text_view->set_semantic_color_fieldname( 'TEXT_BACKG' ). ENDIF. WHEN OTHERS. ENDCASE.
日期列设置列背景色,单号设置单元格不同,状态列设置文本色。
3.查询数据时,控制颜色字段属性,设置颜色。
LOOP AT lt_retab INTO ls_retab. * 16 colors are available l_cell_design = l_cell_design + 1. IF l_cell_design > 17. l_cell_design = 1. ENDIF. * cell background color CASE l_cell_design. WHEN 1. ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-standard. WHEN 2. ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-negative. WHEN 3. ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-positive. WHEN 4. ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-badvalue_dark. WHEN 5. ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-badvalue_medium. WHEN 6. ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-badvalue_light. WHEN 7. ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-criticalvalue_dark. WHEN 8. ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-criticalvalue_medium. WHEN 9. ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-criticalvalue_light. WHEN 10. ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-goodvalue_dark. WHEN 11. ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-goodvalue_medium. WHEN 12. ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-goodvalue_light. WHEN 13. ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-key_medium. WHEN 14. ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-group_level1. WHEN 15. ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-group_level2. WHEN 16. ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-group_level3. ENDCASE. * text colors l_semantic_color = l_semantic_color + 1. IF l_semantic_color > 7. l_semantic_color = 1. ENDIF. CASE l_semantic_color. WHEN 1. ls_retab-text_backg = cl_wd_text_view=>e_semantic_color-standard. WHEN 2. ls_retab-text_backg = cl_wd_text_view=>e_semantic_color-diminished. WHEN 3. ls_retab-text_backg = cl_wd_text_view=>e_semantic_color-positive. WHEN 4. ls_retab-text_backg = cl_wd_text_view=>e_semantic_color-critical. WHEN 5. ls_retab-text_backg = cl_wd_text_view=>e_semantic_color-negative. WHEN 6. ls_retab-text_backg = cl_wd_text_view=>e_semantic_color-marked1. WHEN 7. ls_retab-text_backg = cl_wd_text_view=>e_semantic_color-marked2. ENDCASE. MODIFY lt_retab FROM ls_retab. ENDLOOP.
好了,加颜色的代码已经完成,看看效果吧。