这里介绍三种类型的颜色:列的背景色,单元格的背景色,单元格文本的颜色。
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 + .
IF l_cell_design > .
l_cell_design = .
ENDIF.
* cell background color
CASE l_cell_design.
WHEN .
ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-standard.
WHEN .
ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-negative.
WHEN .
ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-positive.
WHEN .
ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-badvalue_dark.
WHEN .
ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-badvalue_medium.
WHEN .
ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-badvalue_light.
WHEN .
ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-criticalvalue_dark.
WHEN .
ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-criticalvalue_medium.
WHEN .
ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-criticalvalue_light.
WHEN .
ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-goodvalue_dark.
WHEN .
ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-goodvalue_medium.
WHEN .
ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-goodvalue_light.
WHEN .
ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-key_medium.
WHEN .
ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-group_level1.
WHEN .
ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-group_level2.
WHEN .
ls_retab-cell_backg = cl_wd_abstr_master_table_col=>e_cell_design-group_level3.
ENDCASE. * text colors
l_semantic_color = l_semantic_color + .
IF l_semantic_color > .
l_semantic_color = .
ENDIF.
CASE l_semantic_color.
WHEN .
ls_retab-text_backg = cl_wd_text_view=>e_semantic_color-standard.
WHEN .
ls_retab-text_backg = cl_wd_text_view=>e_semantic_color-diminished.
WHEN .
ls_retab-text_backg = cl_wd_text_view=>e_semantic_color-positive.
WHEN .
ls_retab-text_backg = cl_wd_text_view=>e_semantic_color-critical.
WHEN .
ls_retab-text_backg = cl_wd_text_view=>e_semantic_color-negative.
WHEN .
ls_retab-text_backg = cl_wd_text_view=>e_semantic_color-marked1.
WHEN .
ls_retab-text_backg = cl_wd_text_view=>e_semantic_color-marked2.
ENDCASE.
MODIFY lt_retab FROM ls_retab.
ENDLOOP.
好了,加颜色的代码已经完成,看看效果吧。