图标(ICON)和提示信息(tooltips)会使ALV更加易读,突出重点,下面就介绍一下如何在SALV上添加ICON和tooltips。
本例子在SALV的status列中显示红绿灯图标,并根据图标的颜色显示不同的提示信息,方便用户更清晰的阅读SALV,具体方法如下:
1,将为图标ID赋值到Status列上,图标ID的查询方法参照-->如何查找图标ID(ICON ID)等详细信息
2,设定列值为图标-调用cl_salv_columns->get_column ()得到‘Status’列的类引用,然后通过cl_salv_columns->set_icon ()将列设置为显示图标.
3,调用cl_salv_tooltips->add_tooltip()设置图标的提示信息(tooltips)
完整代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
REPORT Z_SALV_18.
CLASS lcl_main DEFINITION.
PUBLIC SECTION.
DATA o_salv TYPE REF TO cl_salv_table .
TYPES:
BEGIN OF ty_output,
status TYPE char10,
field1 TYPE char30,
END OF ty_output.
DATA: t_output TYPE STANDARD TABLE OF ty_output.
METHODS:
select_data,
generate_alv.
ENDCLASS. "lcl_main DEFINITION
*
START-OF-SELECTION.
DATA: o_main TYPE REF TO lcl_main.
CREATE OBJECT o_main.
o_main->select_data( ).
o_main->generate_alv( ).
*
CLASS lcl_main IMPLEMENTATION.
METHOD select_data.
INCLUDE: <icon>.
DATA: ls_output LIKE LINE OF t_output.
DO 3 TIMES.
ls_output-status = icon_green_light.
ls_output-field1 = sy-abcde.
APPEND ls_output TO t_output.
ls_output-status = icon_yellow_light.
APPEND ls_output TO t_output.
ls_output-status = icon_red_light.
APPEND ls_output TO t_output.
ls_output-status = icon_led_green.
APPEND ls_output TO t_output.
ls_output-status = icon_led_red.
APPEND ls_output TO t_output.
ls_output-status = icon_led_yellow.
APPEND ls_output TO t_output.
ENDDO.
ENDMETHOD. "select_Data
METHOD generate_alv.
DATA: lo_functions TYPE REF TO cl_salv_functions_list.
DATA: lo_functional_settings TYPE REF TO cl_salv_functional_settings.
DATA: lo_tooltips TYPE REF TO cl_salv_tooltips,
lv_value TYPE lvc_value.
DATA: lo_columns TYPE REF TO cl_salv_columns.
DATA: lo_column TYPE REF TO cl_salv_column_table.
INCLUDE: <icon>.
*
* ALV Object
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = o_salv
CHANGING
t_table = t_output ).
CATCH cx_salv_msg. "#EC NO_HANDLER
ENDTRY.
* 设定SALV的工具栏toolbar
lo_functions = o_salv->get_functions( ).
lo_functions->set_all( abap_true ).
*... set the columns
lo_columns = o_salv->get_columns( ).
TRY.
lo_column ?= lo_columns->get_column( 'STATUS' ).
* 将列设置为显示ICON
lo_column->set_icon( if_salv_c_bool_sap=>true ).
* 设置列标签 label
lo_column->set_long_text( 'Hover for Tooltip' ).
lo_column->set_alignment( if_salv_c_alignment=>centered ).
lo_column->set_output_length( 20 ).
CATCH cx_salv_not_found. "#EC NO_HANDLER
ENDTRY.
*...Tooltips,
lo_functional_settings = o_salv->get_functional_settings( ).
lo_tooltips = lo_functional_settings->get_tooltips( ).
TRY.
* 当绿灯时,提示 Everything is Processed
lv_value = icon_green_light.
lo_tooltips->add_tooltip(
TYPE = cl_salv_tooltip=>c_type_icon
VALUE = lv_value
tooltip = 'Everything is Processed' ). "#EC NOTEXT
CATCH cx_salv_existing. "#EC NO_HANDLER
ENDTRY.
TRY.
* 黄灯时提示 Everything is Processed
lv_value = icon_yellow_light.
lo_tooltips->add_tooltip(
TYPE = cl_salv_tooltip=>c_type_icon
VALUE = lv_value
tooltip = 'Partially processed' ). "#EC NOTEXT
CATCH cx_salv_existing. "#EC NO_HANDLER
ENDTRY.
TRY.
* 红灯时提示 Nothing Yet processed
lv_value = icon_red_light.
lo_tooltips->add_tooltip(
TYPE = cl_salv_tooltip=>c_type_icon
VALUE = lv_value
tooltip = 'Nothing Yet processed' ). "#EC NOTEXT
CATCH cx_salv_existing. "#EC NO_HANDLER
ENDTRY.
*... display the table
o_salv->display( ).
ENDMETHOD. "generate_alv
ENDCLASS. "lcl_main IMPLEMENTATION
|
运行后,鼠标悬停在图标(ICON)上会显示相应的提示信息,
以上。