SALV 的颜色设置包括行、列和单元格的颜色设置,本篇将对 ALV 颜色设置的方法进行简明扼要的介绍。
ALV 颜色表示方法
SAP 用 LVC_S_COLO 结构来表达颜色,格式为<Color><Intensity><Inverted>
- COL: 颜色
- INT: intensity, 高亮强度(1=高亮,0=普通)
- INV: inverted, 反转颜色(0=不反转)
其中,颜色的值可以是以下数字:
设置列颜色
设置列的颜色最简单,只需要获取列的引用 (cl_salv_column_table 类型),调用 set_color() 方法即可。
report z_alv_test.
types: begin of ty_sflight.
include type sflight.
types: color type lvc_t_scol, " color 字段保存颜色设置
end of ty_sflight.
data gt_sflight type standard table of ty_sflight.
data lo_column_price type ref to cl_salv_column_table.
start-of-selection.
select *
into corresponding fields of table gt_sflight
from sflight.
try.
cl_salv_table=>factory(
importing r_salv_table = data(lo_salv)
changing t_table = gt_sflight
).
catch cx_salv_msg.
endtry.
" 设置工具栏
lo_salv->get_functions( )->set_all( ).
try.
lo_column_price ?= lo_salv->get_columns( )->get_column( 'PRICE' ).
lo_column_price->set_color( value lvc_s_colo( col = 3 int = 1 inv = 0 ) ).
catch cx_salv_not_found.
endtry.
lo_salv->display( ).
效果如下:
设置单元格颜色
设置单元格颜色需要进行以下几步操作:
- 输出内表中添加一个专门保存颜色的字段,类型为 lvc_t_scol
2. 定义一个结构 lvc_s_scol,用于存放颜色设置,并将符合条件的字段名和颜色设置赋值给结构(ls_color),然后赋值给输出内表添加的字段中(color字段):
上述代码可以用新语法简写:
3. 调用 cl_salv_columns_table 的 set_color_column() 方法,将颜色设置传给 cl_salv_table
lo_salv->get_columns( )->set_color_column( 'COLOR' ).
显示效果:
单元格颜色设置的完整代码:
report z_alv_test.
types: begin of ty_sflight.
include type sflight.
types: color type lvc_t_scol, " color 字段保存颜色设置
end of ty_sflight.
data gt_sflight type standard table of ty_sflight.
data lo_column_price type ref to cl_salv_column_table.
start-of-selection.
select *
into corresponding fields of table gt_sflight
from sflight.
try.
cl_salv_table=>factory(
importing r_salv_table = data(lo_salv)
changing t_table = gt_sflight
).
catch cx_salv_msg.
endtry.
" 设置工具栏
lo_salv->get_functions( )->set_all( ).
lo_salv->get_display_settings( )->set_list_header( '航班' ).
loop at gt_sflight assigning field-symbol(<fs_sflight>).
if <fs_sflight>-price > 1000.
append
value lvc_s_scol( fname = 'PRICE' color-col = 5 color-int = 1 color-inv = 0 )
to <fs_sflight>-color.
endif.
endloop.
lo_salv->get_columns( )->set_color_column( 'COLOR' ).
lo_salv->display( ).
行颜色设置
与单元格颜色设置方法类似,只是不用指定列名即可。
显示效果: