在GUI Status加入自定义按钮后,可以通过注册事件(event) 'added_function',并且在对应的handler method中写入相关逻辑,来实现点击按钮后的逻辑。
事件added_function是在类cl_salv_events中,
事件注册代码:
1
2
3
4
5
6
7
8
9
|
*... §6 register to the events of cl_salv_table
DATA: lo_events TYPE REF TO cl_salv_events.
lo_events = o_alv->get_event( ).
CREATE OBJECT o_events.
*... §6.1 register to the event USER_COMMAND
SET HANDLER o_events->on_user_command FOR lo_events.
|
handler 方法的代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
* 事件class
CLASS lcl_handle_events DEFINITION.
PUBLIC SECTION.
METHODS:
on_user_command FOR EVENT added_function OF cl_salv_events
IMPORTING e_salv_function.
ENDCLASS. "lcl_handle_events DEFINITION
CLASS lcl_handle_events IMPLEMENTATION.
METHOD on_user_command.
IF e_salv_function = '&DIS'.
MESSAGE 'dipslay button' TYPE 'I'.
ENDIF.
ENDMETHOD. "on_user_command
ENDCLASS.
|
完整代码如下:
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
*&---------------------------------------------------------------------*
*& 一个简单的通过类CL_SALV_TABLE生成SALV的例子
*&---------------------------------------------------------------------*
REPORT z_salv_1.
* 事件class
CLASS lcl_handle_events DEFINITION.
PUBLIC SECTION.
METHODS:
on_user_command FOR EVENT added_function OF cl_salv_events
IMPORTING e_salv_function.
ENDCLASS. "lcl_handle_events DEFINITION
CLASS lcl_handle_events IMPLEMENTATION.
METHOD on_user_command.
IF e_salv_function = '&DIS'.
MESSAGE 'dipslay button' TYPE 'I'.
ENDIF.
ENDMETHOD. "on_user_command
ENDCLASS.
*----------------------------------------------------------------------*
* CLASS lcl_report DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_report DEFINITION.
*
PUBLIC SECTION.
*
* 定义SALV输出内表
TYPES: BEGIN OF ty_vbak,
vbeln TYPE vbak-vbeln,
erdat TYPE erdat,
auart TYPE auart,
kunnr TYPE kunnr,
END OF ty_vbak.
*
DATA: t_vbak TYPE STANDARD TABLE OF ty_vbak.
*
* ALV reference
DATA: o_alv TYPE REF TO cl_salv_table.
*
DATA: o_events TYPE REF TO lcl_handle_events.
METHODS:
* 抽取数据
get_data,
*
* 生成输出内容
generate_output.
*
*$*$*.....CODE_ADD_1 - Begin..................................1..*$*$*
*
* 定义private method来设定SALV不同特性
PRIVATE SECTION.
METHODS:
set_pf_status CHANGING co_alv TYPE REF TO cl_salv_table.
*$*$*.....CODE_ADD_1 - End....................................1..*$*$*
ENDCLASS. "lcl_report DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_report IMPLEMENTATION
*----------------------------------------------------------------------*
* 类实现
CLASS lcl_report IMPLEMENTATION.
*
METHOD get_data.
* 抽取数据
SELECT vbeln
erdat
auart
kunnr
INTO TABLE t_vbak
FROM vbak
UP TO 20 ROWS.
*
ENDMETHOD. "get_data
*.......................................................................
METHOD generate_output.
* New ALV instance
* We are calling the static Factory method which will give back
* the ALV object reference.
*
* exception class
DATA: lx_msg TYPE REF TO cx_salv_msg.
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = o_alv
CHANGING
t_table = t_vbak ).
CATCH cx_salv_msg INTO lx_msg.
ENDTRY.
*
*$*$*.....CODE_ADD_2 - Begin..................................2..*$*$*
*
* In this area we will call the methods which will set the
* different properties to the ALV
* 调用status设置方法
CALL METHOD set_pf_status
CHANGING
co_alv = o_alv.
*$*$*.....CODE_ADD_2 - End....................................2..*$*$*
*
*
*... §6 register to the events of cl_salv_table
DATA: lo_events TYPE REF TO cl_salv_events.
lo_events = o_alv->get_event( ).
CREATE OBJECT o_events.
*... §6.1 register to the event USER_COMMAND
SET HANDLER o_events->on_user_command FOR lo_events.
* 调用Display方法将数据输出到屏幕上
o_alv->display( ).
*
ENDMETHOD. "generate_output
*
*$*$*.....CODE_ADD_3 - Begin..................................3..*$*$*
*
* In this area we will implement the methods which are defined in
* the class definition
* status设置方法实现
METHOD set_pf_status.
* 调用自定义pf status
co_alv->set_screen_status(
pfstatus = 'SALV_STANDARD'
report = 'Z_SALV_1'
set_functions = co_alv->c_functions_all ).
ENDMETHOD.
*$*$*.....CODE_ADD_3 - End....................................3..*$*$*
*
ENDCLASS. "lcl_report IMPLEMENTATION
START-OF-SELECTION.
DATA: lo_report TYPE REF TO lcl_report.
CREATE OBJECT lo_report.
* 抽取数据
lo_report->get_data( ).
* 生成SALV
lo_report->generate_output( ).
|
运行结果:
以上。