闲暇之余琢磨了下sap的tree control,做过asp.netd的都比较熟悉这个控件,现在来说说如何在abap oo中实现这个效果,这是鄙人第一次写东西,请各位多多指指教。
注明:为更好的演示,假设场景是要显示每个销售区域下的每个客户的销售订单信息,最后的结果截图如下。
图1
1.首先在屏幕中添加两个container控件(也可以添加一个,在程序中用spliter分开就好了),如图。
图2
在属性栏中主要是对屏幕的一个说明。
图3
pbo和pai。
添加完控件之后的元素清单如上图3,exit控件可以不加,这个只是方便测试。
2.在代码中声明变量,包括屏幕元素,数据结构,内表和class。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 report zdemo_tree_control_so_data.
2
3 tables vbap .
4
5 data ok_code type sy-ucomm .
6 data save_ok type sy-ucomm .
7
8 class lcl_application definition deferred .
9 data custom_container type ref to cl_gui_custom_container .
10 data gcontainer type ref to cl_gui_custom_container .
11 data tree_control type ref to cl_gui_simple_tree .
12 data grid type ref to cl_gui_alv_grid .
13
14 data gs_layout type lvc_s_layo.
15 data gt_fieldcat type standard table of lvc_s_fcat .
16 data gw_fieldcat like line of gt_fieldcat .
17
18 data node_table like table of mtreesnode .
19 data node1 type mtreesnode .
20
21 types begin of st_vbak .
22 types vkorg type vbak-vkorg .
23 types vtweg type vbak-vtweg .
24 types kunnr type vbak-kunnr .
25 types vbeln type vbak-vbeln .
26 types end of st_vbak .
27
28 data t_vbak type table of st_vbak .
29 data w_vbak like line of t_vbak .
30
31 types begin of st_vbap .
32 types vbeln type vbap-vbeln .
33 types posnr type vbap-posnr .
34 types matnr type vbap-matnr .
35 types netwr type vbap-netwr .
36 types werks type vbap-werks .
37 types lsmeng type vbap-lsmeng .
38 types end of st_vbap .
39 data t_vbap type standard table of st_vbap .
40 data w_vbap like line of t_vbap .
41
42
43 data g_application type ref to lcl_application .
44 *----------------------------------------------------------------------*
45 * CLASS LCL_APPLICATION DEFINITION
46 *----------------------------------------------------------------------*
47 *
48 *----------------------------------------------------------------------*
49 class lcl_application definition .
50 public section .
51 methods handle_selection_changed
52 for event selection_changed of cl_gui_simple_tree
53 importing node_key .
54 endclass .
55
56 class lcl_application implementation .
57 method handle_selection_changed .
58 read table node_table with key node_key = node_key into node1 .
59 perform get_data_grid using node_key node1-relatkey.
60 call method grid->refresh_table_display .
61 endmethod . "handle_SELECTION_CHANGED
62 endclass . "LCL_APPLICATION IMPLEMENTATION
注意:在class的执行定义中需要调用 refresh_table_display方法刷新alv。
3.获取数据,包括节点数据和alv显示的数据,第一次进到界面的时候显示全部销售订单信息数据。
4.循环节点控制内表(销售组织,分销渠道和客户名称),设置节点属性,填充节点表中node_table。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 loop at t_vbak into w_vbak.
2 clear l_vkorg .
3 clear l_vtweg .
4 "在创建根节点的同时需要创建下级以及次级子节点
5 if vkorg ne w_vbak-vkorg .
6 clear node1 .
7 node1-node_key = w_vbak-vkorg .
8 "FIRST LAYER
9 clear node1-relatkey .
10 clear node1-relatship .
11 "SET THE ICON AS FOLDER
12 node1-hidden = '' .
13 node1-disabled = '' .
14 node1-isfolder = 'X' .
15
16 "CLEAR THE ICON OF THE NODE
17 clear node1-n_image .
18 clear node1-exp_image .
19 node1-expander = 'X'.
20
21 node1-text = l_vkorg .
22 append node1 to node_table .
23 clear node1 .
24 concatenate w_vbak-vkorg w_vbak-vtweg into node1-node_key .
25 node1-relatkey = w_vbak-vkorg .
26 node1-relatship = cl_gui_simple_tree=>relat_last_child .
27 node1-hidden = '' .
28 node1-disabled = '' .
29 node1-isfolder = 'X' .
30
31 clear node1-n_image .
32 clear node1-exp_image .
33 node1-expander = 'X'.
34
35 "DISPLAY THE NODE VTWEG
36 node1-text = l_vtweg .
37 append node1 to node_table .
38
39 clear node1 .
40 node1-node_key = w_vbak-kunnr .
41 concatenate w_vbak-vkorg w_vbak-vtweg into str1 .
42 node1-relatkey = str1 .
43 node1-relatship = cl_gui_simple_tree=>relat_last_child .
44 node1-hidden = '' .
45 node1-disabled = '' .
46 node1-isfolder = 'X' .
47 node1-n_image = '@5B@' .
48 node1-exp_image = '@5B@' .
49 node1-expander = 'X'.
50 node1-text = w_vbak-kunnr .
51 append node1 to node_table .
52 endif.
53
54 "在创建次级节点的同时需要创建其字节点
55 if ( vkorg eq w_vbak-vkorg ) and ( vtweg <>
56 w_vbak-vtweg ).
57 clear node1 .
58 concatenate w_vbak-vkorg w_vbak-vtweg into node1-node_key .
59 node1-relatkey = w_vbak-vkorg .
60 node1-relatship = cl_gui_simple_tree=>relat_last_child .
61 node1-hidden = '' .
62 node1-disabled = '' .
63 node1-isfolder = 'X' .
64
65 clear node1-n_image .
66 clear node1-exp_image .
67 node1-expander = 'X'.
68 node1-text = l_vtweg .
69 append node1 to node_table .
70
71 clear node1 .
72 node1-node_key = w_vbak-kunnr .
73 concatenate w_vbak-vkorg w_vbak-vtweg into str1 .
74 node1-relatkey = str1 .
75 node1-relatship = cl_gui_simple_tree=>relat_last_child .
76 node1-hidden = '' .
77 node1-disabled = '' .
78 node1-isfolder = 'X' .
79 node1-n_image = '@5B@' .
80 node1-exp_image = '@5B@' .
81 node1-expander = 'X'.
82 node1-text = w_vbak-kunnr .
83 append node1 to node_table .
84 endif.
85
86 if vkorg eq w_vbak-vkorg and vtweg eq w_vbak-vtweg and kunnr <>
87 w_vbak-kunnr.
88 clear node1 .
89 node1-node_key = w_vbak-kunnr .
90 concatenate w_vbak-vkorg w_vbak-vtweg into str1 .
91 node1-relatkey = str1 .
92 node1-relatship = cl_gui_simple_tree=>relat_last_child .
93 node1-hidden = '' .
94 node1-disabled = '' .
95 node1-isfolder = 'X' .
96 node1-n_image = '@5B@' .
97 node1-exp_image = '@5B@' .
98 node1-expander = 'X'.
99 node1-text = w_vbak-kunnr .
100 append node1 to node_table .
101 endif .
102 vkorg = w_vbak-vkorg .
103 vtweg = w_vbak-vtweg .
104 vbeln = w_vbak-vbeln .
105 kunnr = w_vbak-kunnr .
106 endloop.
5.添加节点数据到控件并注册事件
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 call method tree_control->add_nodes
2 exporting
3 table_structure_name = 'MTREESNODE'
4 node_table = node_table
5 exceptions
6 error_in_node_table = 1
7 failed = 2
8 dp_error = 3
9 table_structure_name_not_found = 4
10 others = 5.
11
12 event1-eventid = 21 .
13 event1-appl_event = 'X' .
14 append event1 to events .
15
16 call method tree_control->set_registered_events
17 exporting
18 events = events
19 exceptions
20 cntl_error = 1
21 cntl_system_error = 2
22 illegal_event_combination = 3
23 others = 4.
24
25 set handler g_application->handle_selection_changed for
26 tree_control .
附件:完整代码
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 *&---------------------------------------------------------------------*
2 *& REPORT ZDEMO_TREE_CONTROL_20120331
3 *&
4 *&---------------------------------------------------------------------*
5 *&THIS A DEMO FOR CREATE A TREE CONTROL
6 *&
7 *&---------------------------------------------------------------------*
8
9 report zdemo_tree_control_so_data.
10
11 tables vbap .
12
13 data ok_code type sy-ucomm .
14 data save_ok type sy-ucomm .
15
16 class lcl_application definition deferred .
17 data custom_container type ref to cl_gui_custom_container .
18 data gcontainer type ref to cl_gui_custom_container .
19 data tree_control type ref to cl_gui_simple_tree .
20 data grid type ref to cl_gui_alv_grid .
21
22 data gs_layout type lvc_s_layo.
23 data gt_fieldcat type standard table of lvc_s_fcat .
24 data gw_fieldcat like line of gt_fieldcat .
25
26 data node_table like table of mtreesnode .
27 data node1 type mtreesnode .
28
29 types begin of st_vbak .
30 types vkorg type vbak-vkorg .
31 types vtweg type vbak-vtweg .
32 types kunnr type vbak-kunnr .
33 types vbeln type vbak-vbeln .
34 types end of st_vbak .
35
36 data t_vbak type table of st_vbak .
37 data w_vbak like line of t_vbak .
38
39 types begin of st_vbap .
40 types vbeln type vbap-vbeln .
41 types posnr type vbap-posnr .
42 types matnr type vbap-matnr .
43 types netwr type vbap-netwr .
44 types werks type vbap-werks .
45 types lsmeng type vbap-lsmeng .
46 types end of st_vbap .
47 data t_vbap type standard table of st_vbap .
48 data w_vbap like line of t_vbap .
49
50
51 data g_application type ref to lcl_application .
52 *----------------------------------------------------------------------*
53 * CLASS LCL_APPLICATION DEFINITION
54 *----------------------------------------------------------------------*
55 *
56 *----------------------------------------------------------------------*
57 class lcl_application definition .
58 public section .
59 methods handle_selection_changed
60 for event selection_changed of cl_gui_simple_tree
61 importing node_key .
62 endclass . "LCL_APPLICATION DEFINITION
63
64 *----------------------------------------------------------------------*
65 * CLASS LCL_APPLICATION IMPLEMENTATION
66 *----------------------------------------------------------------------*
67 *
68 *----------------------------------------------------------------------*
69 class lcl_application implementation .
70 method handle_selection_changed .
71 read table node_table with key node_key = node_key into node1 .
72 perform get_data_grid using node_key node1-relatkey.
73 call method grid->refresh_table_display .
74 endmethod . "handle_SELECTION_CHANGED
75 endclass . "LCL_APPLICATION IMPLEMENTATION
76
77 selection-screen begin of block a with frame title text-001 .
78 parameters p_vkorg type tvkot-vkorg obligatory default '2030'.
79 parameters p_vtweg type tvtwt-vtweg obligatory default '11'.
80 selection-screen end of block a .
81
82 start-of-selection .
83 perform get_data .
84 create object g_application .
85 call screen 0100 .
86
87 *----------------------------------------------------------------------*
88 * MODULE STATUS_0100 OUTPUT
89 *----------------------------------------------------------------------*
90 *
91 *----------------------------------------------------------------------*
92 module status_0100 output .
93 if tree_control is initial.
94 perform create_tree .
95 endif.
96 endmodule . "STATUS_0100 OUTPUT
97
98 *----------------------------------------------------------------------*
99 * MODULE USER_COMMAND_0100 INPUT
100 *----------------------------------------------------------------------*
101 *
102 *----------------------------------------------------------------------*
103 module user_command_0100 input .
104 save_ok = ok_code .
105 clear ok_code .
106 case save_ok.
107 when 'EXIT'.
108 leave program .
109 when others.
110 endcase.
111 endmodule . "USER_COMMAND_0100 INPUT
112 *&---------------------------------------------------------------------*
113 *& FORM CREATE_TREE
114 *&---------------------------------------------------------------------*
115 * TEXT
116 *----------------------------------------------------------------------*
117 * --> P1 TEXT
118 * <-- P2 TEXT
119 *----------------------------------------------------------------------*
120 form create_tree .
121 data events type cntl_simple_events .
122 data event1 type cntl_simple_event .
123 data vkorg like vbak-vkorg .
124 data vtweg like vbak-vtweg .
125 data vbeln like vbak-vbeln .
126 data kunnr like vbak-kunnr .
127 data l_vkorg like tvkot-vtext.
128 data l_vtweg like tvtwt-vtext .
129 data str1(255) .
130 create object custom_container
131 exporting
132 container_name = 'TREE'.
133
134 create object tree_control
135 exporting
136 parent = custom_container
137 node_selection_mode = cl_gui_simple_tree=>node_sel_mode_single
138 .
139
140
141 create object gcontainer
142 exporting
143 container_name = 'GCONTAINER'.
144
145 create object grid
146 exporting
147 i_parent = gcontainer.
148 perform get_field_header_title .
149 gs_layout-grid_title = 'SD分销渠道销售订单数据' .
150 gs_layout-cwidth_opt = 'X' .
151 gs_layout-zebra = 'X' .
152
153 concatenate p_vkorg p_vtweg into node1-node_key .
154 node1-relatkey = p_vkorg .
155 perform get_data_grid using node1-node_key node1-relatkey .
156
157 call method grid->set_table_for_first_display
158 exporting
159 is_layout = gs_layout
160 changing
161 it_fieldcatalog = gt_fieldcat
162 it_outtab = t_vbap.
163
164
165
166 loop at t_vbak into w_vbak.
167 clear l_vkorg .
168 clear l_vtweg .
169 select single
170 vtext
171 into l_vkorg
172 from tvkot
173 where vkorg = w_vbak-vkorg and spras = '1' .
174 select single
175 vtext
176 into l_vtweg
177 from tvtwt
178 where vtweg = w_vbak-vtweg and spras = '1' .
179
180 "在创建根节点的同时需要创建下级以及次级子节点
181 if vkorg ne w_vbak-vkorg .
182
183 clear node1 .
184 node1-node_key = w_vbak-vkorg .
185 "FIRST LAYER
186 clear node1-relatkey .
187 clear node1-relatship .
188 "SET THE ICON AS FOLDER
189 node1-hidden = '' .
190 node1-disabled = '' .
191 node1-isfolder = 'X' .
192
193 "CLEAR THE ICON OF THE NODE
194 clear node1-n_image .
195 clear node1-exp_image .
196 node1-expander = 'X'.
197
198 node1-text = l_vkorg .
199 append node1 to node_table .
200 clear node1 .
201 concatenate w_vbak-vkorg w_vbak-vtweg into node1-node_key .
202 node1-relatkey = w_vbak-vkorg .
203 node1-relatship = cl_gui_simple_tree=>relat_last_child .
204 node1-hidden = '' .
205 node1-disabled = '' .
206 node1-isfolder = 'X' .
207
208 clear node1-n_image .
209 clear node1-exp_image .
210 node1-expander = 'X'.
211
212 "DISPLAY THE NODE VTWEG
213 node1-text = l_vtweg .
214 append node1 to node_table .
215
216 clear node1 .
217 node1-node_key = w_vbak-kunnr .
218 concatenate w_vbak-vkorg w_vbak-vtweg into str1 .
219 node1-relatkey = str1 .
220 node1-relatship = cl_gui_simple_tree=>relat_last_child .
221 node1-hidden = '' .
222 node1-disabled = '' .
223 node1-isfolder = 'X' .
224 node1-n_image = '@5B@' .
225 node1-exp_image = '@5B@' .
226 node1-expander = 'X'.
227 node1-text = w_vbak-kunnr .
228 append node1 to node_table .
229 endif.
230
231 "在创建次级节点的同时需要创建其字节点
232 if ( vkorg eq w_vbak-vkorg ) and ( vtweg <>
233 w_vbak-vtweg ).
234 clear node1 .
235 concatenate w_vbak-vkorg w_vbak-vtweg into node1-node_key .
236 node1-relatkey = w_vbak-vkorg .
237 node1-relatship = cl_gui_simple_tree=>relat_last_child .
238 node1-hidden = '' .
239 node1-disabled = '' .
240 node1-isfolder = 'X' .
241
242 clear node1-n_image .
243 clear node1-exp_image .
244 node1-expander = 'X'.
245 node1-text = l_vtweg .
246 append node1 to node_table .
247
248 clear node1 .
249 node1-node_key = w_vbak-kunnr .
250 concatenate w_vbak-vkorg w_vbak-vtweg into str1 .
251 node1-relatkey = str1 .
252 node1-relatship = cl_gui_simple_tree=>relat_last_child .
253 node1-hidden = '' .
254 node1-disabled = '' .
255 node1-isfolder = 'X' .
256 node1-n_image = '@5B@' .
257 node1-exp_image = '@5B@' .
258 node1-expander = 'X'.
259 node1-text = w_vbak-kunnr .
260 append node1 to node_table .
261 endif.
262
263 if vkorg eq w_vbak-vkorg and vtweg eq w_vbak-vtweg and kunnr <>
264 w_vbak-kunnr.
265 clear node1 .
266 node1-node_key = w_vbak-kunnr .
267 concatenate w_vbak-vkorg w_vbak-vtweg into str1 .
268 node1-relatkey = str1 .
269 node1-relatship = cl_gui_simple_tree=>relat_last_child .
270 node1-hidden = '' .
271 node1-disabled = '' .
272 node1-isfolder = 'X' .
273 node1-n_image = '@5B@' .
274 node1-exp_image = '@5B@' .
275 node1-expander = 'X'.
276 node1-text = w_vbak-kunnr .
277 append node1 to node_table .
278 endif .
279 vkorg = w_vbak-vkorg .
280 vtweg = w_vbak-vtweg .
281 vbeln = w_vbak-vbeln .
282 kunnr = w_vbak-kunnr .
283 endloop.
284
285 call method tree_control->add_nodes
286 exporting
287 table_structure_name = 'MTREESNODE'
288 node_table = node_table
289 exceptions
290 error_in_node_table = 1
291 failed = 2
292 dp_error = 3
293 table_structure_name_not_found = 4
294 others = 5.
295
296 * event1-eventid = cl_gui_simple_tree=>eventid_node_double_click .
297 * event1-appl_event = 'X' .
298 * append event1 to events .
299
300 event1-eventid = 21 .
301 event1-appl_event = 'X' .
302 append event1 to events .
303
304 call method tree_control->set_registered_events
305 exporting
306 events = events
307 exceptions
308 cntl_error = 1
309 cntl_system_error = 2
310 illegal_event_combination = 3
311 others = 4.
312
313 set handler g_application->handle_selection_changed for
314 tree_control .
315 endform. " CREATE_TREE
316 *&---------------------------------------------------------------------*
317 *& FORM GET_DATA
318 *&---------------------------------------------------------------------*
319 * TEXT
320 *----------------------------------------------------------------------*
321 * --> P1 TEXT
322 * <-- P2 TEXT
323 *----------------------------------------------------------------------*
324 form get_data .
325 select
326 vkorg vtweg kunnr vbeln
327 into table t_vbak
328 from vbak
329 where vkorg eq p_vkorg
330 and vtweg eq p_vtweg
331 .
332
333 sort t_vbak by vkorg vtweg kunnr vbeln .
334
335 endform. " GET_DATA
336 *&---------------------------------------------------------------------*
337 *& Form GET_DATA_GRID
338 *&---------------------------------------------------------------------*
339 * text
340 *----------------------------------------------------------------------*
341 * -->P_NODEKEY text
342 *----------------------------------------------------------------------*
343 form get_data_grid using p_nodekey p_relatkey.
344
345 if p_relatkey is initial.
346 * select
347 * vbap~vbeln
348 * vbap~posnr
349 * vbap~matnr
350 * vbap~netwr
351 * vbap~werks
352 * vbap~lsmeng
353 * into table t_vbap
354 * from vbap
355 * inner join vbak on vbap~vbeln = vbak~vbeln
356 * where vbak~vkorg = p_vkorg
357 * .
358 elseif p_relatkey eq p_vkorg .
359 select
360 vbap~vbeln
361 vbap~posnr
362 vbap~matnr
363 vbap~netwr
364 vbap~werks
365 vbap~lsmeng
366 into table t_vbap
367 from vbap
368 inner join vbak on vbap~vbeln = vbak~vbeln
369 where vbak~vkorg = p_vkorg
370 and vbak~vtweg = p_vtweg
371 and vbak~auart ne 'Z001'
372 .
373 else .
374 select
375 vbap~vbeln
376 vbap~posnr
377 vbap~matnr
378 vbap~netwr
379 vbap~werks
380 vbap~lsmeng
381 into table t_vbap
382 from vbap
383 inner join vbak on vbap~vbeln = vbak~vbeln
384 where vbak~vkorg = p_vkorg
385 and vbak~vtweg = p_vtweg
386 and vbak~kunnr eq p_nodekey
387 and vbak~auart ne 'Z001'
388 .
389 endif .
390
391
392 loop at t_vbap into w_vbap.
393 call function 'CONVERSION_EXIT_ALPHA_OUTPUT'
394 exporting
395 input = w_vbap-vbeln
396 importing
397 output = w_vbap-vbeln.
398
399 call function 'CONVERSION_EXIT_ALPHA_OUTPUT'
400 exporting
401 input = w_vbap-matnr
402 importing
403 output = w_vbap-matnr.
404 modify t_vbap from w_vbap .
405 endloop.
406 endform. " GET_DATA_GRID
407 *&---------------------------------------------------------------------*
408 *& Form GET_FIELD_HEADER_TITLE
409 *&---------------------------------------------------------------------*
410 * text
411 *----------------------------------------------------------------------*
412 * --> p1 text
413 * <-- p2 text
414 *----------------------------------------------------------------------*
415 form get_field_header_title .
416 clear gw_fieldcat .
417 gw_fieldcat-fieldname = 'VBELN' .
418 gw_fieldcat-scrtext_l = '销售订单' .
419 append gw_fieldcat to gt_fieldcat .
420
421 clear gw_fieldcat .
422 gw_fieldcat-fieldname = 'POSNR' .
423 gw_fieldcat-scrtext_l = '销售订单项目' .
424 append gw_fieldcat to gt_fieldcat .
425
426 clear gw_fieldcat .
427 gw_fieldcat-fieldname = 'MATNR' .
428 gw_fieldcat-scrtext_l = '物料编码' .
429 append gw_fieldcat to gt_fieldcat .
430
431 clear gw_fieldcat .
432 gw_fieldcat-fieldname = 'NETWR' .
433 gw_fieldcat-scrtext_l = '净价' .
434 append gw_fieldcat to gt_fieldcat .
435
436 clear gw_fieldcat .
437 gw_fieldcat-fieldname = 'WERKS' .
438 gw_fieldcat-scrtext_l = '工厂' .
439 append gw_fieldcat to gt_fieldcat .
440
441 clear gw_fieldcat .
442 gw_fieldcat-fieldname = 'LSMENG' .
443 gw_fieldcat-scrtext_l = '销售订单数量' .
444 append gw_fieldcat to gt_fieldcat .
445
446 endform. " GET_FIELD_HEADER_TITLE