ABAP-树型ALV程序例子

 

1 PROCESS BEFORE OUTPUT.
2   MODULE pbo .
3 
4 PROCESS AFTER INPUT.
5   MODULE pai .
  1 REPORT y4_b4_test22.
  2 TYPES: BEGIN OF ty_tree_node,
  3          id   TYPE char18,
  4          text TYPE char40,
  5        END OF ty_tree_node.
  6 
  7 DATA: g_alv_tree         TYPE REF TO cl_gui_alv_tree,
  8       g_custom_container TYPE REF TO cl_gui_custom_container.
  9 
 10 DATA: gt_fcat TYPE  lvc_t_fcat,
 11       gs_fcat TYPE  lvc_s_fcat.
 12 
 13 DATA: gs_sflight TYPE sflight ,      "Output-Table
 14       gt_sflight TYPE  TABLE OF sflight,
 15       gt_scala   TYPE STANDARD TABLE OF ty_tree_node,
 16       ok_code    LIKE sy-ucomm,
 17       save_ok    LIKE sy-ucomm.           "OK-Code
 18 
 19 CLASS lcl_tree_event_receiver DEFINITION.
 20 
 21   PUBLIC SECTION.
 22     METHODS handle_node_double_click
 23       FOR EVENT node_double_click OF cl_gui_alv_tree
 24       IMPORTING node_key sender.
 25     METHODS handle_item_double_click
 26       FOR EVENT item_double_click OF cl_gui_alv_tree
 27       IMPORTING fieldname node_key.
 28 
 29 ENDCLASS.
 30 
 31 CLASS lcl_tree_event_receiver IMPLEMENTATION.
 32 
 33   METHOD handle_node_double_click.
 34 
 35     DATA: lt_children TYPE lvc_t_nkey,
 36           ls_children TYPE lvc_nkey.
 37 
 38     "根据点击的结点找出所有的下一个结点
 39     CALL METHOD sender->get_children
 40       EXPORTING
 41         i_node_key  = node_key
 42       IMPORTING
 43         et_children = lt_children.
 44 
 45     IF NOT lt_children IS INITIAL.
 46 
 47       "展开结点
 48       CALL METHOD sender->expand_node
 49         EXPORTING
 50           i_node_key    = node_key
 51           i_level_count = 2.
 52 
 53     ENDIF.
 54 
 55   ENDMETHOD.
 56 
 57   METHOD handle_item_double_click.
 58     DATA: l_value TYPE lvc_value,
 59           l_layi  TYPE lvc_t_layi,
 60           l_layn  TYPE lvc_s_layn.
 61 
 62     CALL METHOD g_alv_tree->get_outtab_line
 63       EXPORTING
 64         i_node_key     = node_key
 65       IMPORTING
 66         e_outtab_line  = gs_sflight
 67         e_node_text    = l_value
 68         et_item_layout = l_layi
 69         es_node_layout = l_layn
 70       EXCEPTIONS
 71         node_not_found = 1
 72         OTHERS         = 2.
 73     IF sy-subrc = 0.
 74       DATA:mseg TYPE string .
 75       mseg = gs_sflight-price .
 76       MESSAGE mseg TYPE 'I' .
 77     ENDIF.
 78 
 79   ENDMETHOD.
 80 
 81 ENDCLASS.
 82 
 83 END-OF-SELECTION.
 84 
 85   CALL SCREEN 100.
 86 
 87 MODULE pbo OUTPUT.
 88 
 89   SET PF-STATUS 'MAIN100'.
 90   SET TITLEBAR 'MAINTITLE' WITH '树型ALV'.
 91 
 92   IF g_alv_tree IS INITIAL.
 93     PERFORM init_tree.
 94 
 95     CALL METHOD cl_gui_cfw=>flush
 96       EXCEPTIONS
 97         cntl_system_error = 1
 98         cntl_error        = 2.
 99     IF sy-subrc NE 0.
100       CALL FUNCTION 'POPUP_TO_INFORM'
101         EXPORTING
102           titel = 'Automation Queue failure'(801)
103           txt1  = 'Internal error:'(802)
104           txt2  = 'A method in the automation queue'(803)
105           txt3  = 'caused a failure.'(804).
106     ENDIF.
107 
108   ENDIF.
109 
110 ENDMODULE.                             " PBO  OUTPUT
111 
112 MODULE pai INPUT.
113   save_ok = ok_code.
114   CLEAR ok_code.
115 
116   CASE save_ok.
117     WHEN '&F03' OR '&F12' OR '&F15'.
118       PERFORM exit_program.
119 
120     WHEN OTHERS.
121       CALL METHOD cl_gui_cfw=>dispatch.
122   ENDCASE.
123 
124   CALL METHOD cl_gui_cfw=>flush.
125 ENDMODULE.                             " PAI  INPUT
126 
127 FORM init_tree.
128   DATA: l_tree_container_name(30) TYPE c.
129   l_tree_container_name = 'CCONTAINER1'.
130 
131   CREATE OBJECT g_custom_container
132     EXPORTING
133       container_name              = l_tree_container_name
134     EXCEPTIONS
135       cntl_error                  = 1
136       cntl_system_error           = 2
137       create_error                = 3
138       lifetime_error              = 4
139       lifetime_dynpro_dynpro_link = 5.
140   IF sy-subrc <> 0.
141     MESSAGE x208(00) WITH 'ERROR'(100).
142   ENDIF.
143 
144   CREATE OBJECT g_alv_tree
145     EXPORTING
146       parent                      = g_custom_container
147 *     node_selection_mode         = cl_gui_column_tree=>node_sel_mode_single   "单选节点
148       node_selection_mode         = cl_gui_column_tree=>node_sel_mode_single "多选节点
149       item_selection              = 'X'  "能否选中节点上的单个元素
150       no_html_header              = 'X'
151       no_toolbar                  = ''
152     EXCEPTIONS
153       cntl_error                  = 1
154       cntl_system_error           = 2
155       create_error                = 3
156       lifetime_error              = 4
157       illegal_node_selection_mode = 5
158       failed                      = 6
159       illegal_column_name         = 7.
160   IF sy-subrc <> 0.
161     MESSAGE x208(00) WITH 'ERROR'.                          "#EC NOTEXT
162   ENDIF.
163 
164   DATA l_hierarchy_header TYPE treev_hhdr.
165   PERFORM build_hierarchy_header CHANGING l_hierarchy_header.
166   PERFORM build_fieldcategory.
167 
168   CALL METHOD g_alv_tree->set_table_for_first_display
169     EXPORTING
170       "这里可以填SE11的表,填了就参考这个表建fieldcat,下面fieldcat就不用输入参数
171 *     i_structure_name    = 'SFLIGHT'
172       is_hierarchy_header = l_hierarchy_header
173     CHANGING
174       it_outtab           = gt_sflight "table must be empty !
175       it_fieldcatalog     = gt_fcat. "fieldcat
176 
177   PERFORM jerry_create_tree.
178 
179   CALL METHOD g_alv_tree->update_calculations.
180   PERFORM register_events.
181 
182   CALL METHOD g_alv_tree->frontend_update.
183 
184 ENDFORM.
185 
186 FORM build_fieldcategory .
187 
188   CLEAR  gs_fcat.
189   gs_fcat-fieldname      =  'PRICE'.
190   gs_fcat-seltext        =  '航空运费'.
191   gs_fcat-coltext        =  '航空运费'.    " 字段输出名称
192   gs_fcat-no_zero = 'X' .
193   gs_fcat-do_sum = 'X' .
194   gs_fcat-h_ftype = 'AVG'.
195   gs_fcat-just           =  'L'.
196   gs_fcat-outputlen      =  '10'.        " 输出长度
197   APPEND gs_fcat TO gt_fcat.
198 
199   CLEAR  gs_fcat.
200   gs_fcat-fieldname      =  'CURRENCY'.
201   gs_fcat-seltext        =  '航线货币'.
202   gs_fcat-coltext        =  '航线货币'.    " 字段输出名称
203   gs_fcat-no_zero = 'X' .
204   gs_fcat-just           =  'L'.
205   gs_fcat-outputlen      =  '10'.        " 输出长度
206   APPEND gs_fcat TO gt_fcat.
207 
208   CLEAR  gs_fcat.
209   gs_fcat-fieldname      =  'PLANETYPE'.
210   gs_fcat-seltext        =  '飞机类型'.
211   gs_fcat-coltext        =  '飞机类型'.    " 字段输出名称
212   gs_fcat-no_zero = 'X' .
213   gs_fcat-just           =  'L'.
214   gs_fcat-outputlen      =  '10'.        " 输出长度
215   APPEND gs_fcat TO gt_fcat.
216 
217   CLEAR  gs_fcat.
218   gs_fcat-fieldname      =  'SEATSMAX'.
219   gs_fcat-seltext        =  '经济舱的最大容量'.
220   gs_fcat-coltext        =  '经济舱的最大容量'.    " 字段输出名称
221   gs_fcat-no_zero = 'X' .
222   gs_fcat-just           =  'L'.
223   gs_fcat-outputlen      =  '10'.        " 输出长度
224   APPEND gs_fcat TO gt_fcat.
225 
226   CLEAR  gs_fcat.
227   gs_fcat-fieldname      =  'SEATSOCC'.
228   gs_fcat-seltext        =  '占据经济舱座位'.
229   gs_fcat-coltext        =  '占据经济舱座位'.    " 字段输出名称
230   gs_fcat-no_zero = 'X' .
231   gs_fcat-just           =  'L'.
232   gs_fcat-outputlen      =  '10'.        " 输出长度
233   APPEND gs_fcat TO gt_fcat.
234 
235   CLEAR  gs_fcat.
236   gs_fcat-fieldname      =  'PAYMENTSUM'.
237   gs_fcat-seltext        =  '总计'.
238   gs_fcat-coltext        =  '总计'.    " 字段输出名称
239   gs_fcat-no_zero = 'X' .
240   gs_fcat-just           =  'L'.
241   gs_fcat-outputlen      =  '10'.        " 输出长度
242   APPEND gs_fcat TO gt_fcat.
243 
244   CLEAR  gs_fcat.
245   gs_fcat-fieldname      =  'SEATSMAX_B'.
246   gs_fcat-seltext        =  '商务舱的最大容量'.
247   gs_fcat-coltext        =  '商务舱的最大容量'.    " 字段输出名称
248   gs_fcat-no_zero = 'X' .
249   gs_fcat-just           =  'L'.
250   gs_fcat-outputlen      =  '10'.        " 输出长度
251   APPEND gs_fcat TO gt_fcat.
252 
253   CLEAR  gs_fcat.
254   gs_fcat-fieldname      =  'SEATSOCC_B'.
255   gs_fcat-seltext        =  '占据商务舱座位'.
256   gs_fcat-coltext        =  '占据商务舱座位'.    " 字段输出名称
257   gs_fcat-no_zero = 'X' .
258   gs_fcat-just           =  'L'.
259   gs_fcat-outputlen      =  '10'.        " 输出长度
260   APPEND gs_fcat TO gt_fcat.
261 
262   CLEAR  gs_fcat.
263   gs_fcat-fieldname      =  'SEATSMAX_F'.
264   gs_fcat-seltext        =  '头等舱的最大容量'.
265   gs_fcat-coltext        =  '头等舱的最大容量'.    " 字段输出名称
266   gs_fcat-no_zero = 'X' .
267   gs_fcat-just           =  'L'.
268   gs_fcat-outputlen      =  '10'.        " 输出长度
269   APPEND gs_fcat TO gt_fcat.
270 
271   CLEAR  gs_fcat.
272   gs_fcat-fieldname      =  'SEATSOCC_F'.
273   gs_fcat-seltext        =  '占据头等舱座位'.
274   gs_fcat-coltext        =  '占据头等舱座位'.    " 字段输出名称
275   gs_fcat-no_zero = 'X' .
276   gs_fcat-just           =  'L'.
277   gs_fcat-outputlen      =  '10'.        " 输出长度
278   APPEND gs_fcat TO gt_fcat.
279 
280 ENDFORM.
281 
282 FORM jerry_create_tree.
283   DATA: p_relat_key TYPE lvc_nkey,
284         p_node_key1 TYPE lvc_nkey,
285         p_node_key2 TYPE lvc_nkey,
286         p_node_key3 TYPE lvc_nkey,
287         l_node_text TYPE lvc_value,
288         lv_line     TYPE i.
289 
290   SELECT *
291     INTO CORRESPONDING FIELDS OF TABLE gt_sflight
292     FROM sflight .
293 
294   SORT gt_sflight BY carrid connid fldate .
295 
296   lv_line = lines( gt_sflight ) .
297 
298   LOOP AT gt_sflight ASSIGNING FIELD-SYMBOL(<fs_sflight>) FROM 0 TO lv_line .
299 
300     AT NEW carrid .
301 
302       CLEAR l_node_text .
303       l_node_text = <fs_sflight>-carrid .
304 *      CALL FUNCTION 'CONVERSION_EXIT_ABPSN_OUTPUT'
305 *        EXPORTING
306 *          input  = <fs_sflight>-carrid
307 *        IMPORTING
308 *          output = l_node_text.
309 
310       CALL METHOD g_alv_tree->add_node
311         EXPORTING
312           i_relat_node_key = p_relat_key
313           i_relationship   = cl_gui_column_tree=>relat_last_child
314           i_node_text      = l_node_text
315 *         is_outtab_line   = <fs_sflight>
316         IMPORTING
317           e_new_node_key   = p_node_key1.
318 
319     ENDAT .
320 
321     AT NEW connid .
322 
323       CLEAR l_node_text .
324       l_node_text = <fs_sflight>-connid .
325 
326       CALL METHOD g_alv_tree->add_node
327         EXPORTING
328           i_relat_node_key = p_node_key1
329           i_relationship   = cl_gui_column_tree=>relat_last_child
330           i_node_text      = l_node_text
331 *         is_outtab_line   = <fs_sflight>
332         IMPORTING
333           e_new_node_key   = p_node_key2.
334 
335     ENDAT .
336 
337     CLEAR l_node_text .
338     l_node_text = <fs_sflight>-fldate .
339 
340     CALL METHOD g_alv_tree->add_node
341       EXPORTING
342         i_relat_node_key = p_node_key2
343         i_relationship   = cl_gui_column_tree=>relat_last_child
344         i_node_text      = l_node_text
345         is_outtab_line   = <fs_sflight>
346       IMPORTING
347         e_new_node_key   = p_node_key3.
348 
349   ENDLOOP .
350 
351 ENDFORM.
352 
353 FORM build_hierarchy_header CHANGING p_hierarchy_header TYPE treev_hhdr.
354 
355   p_hierarchy_header-heading = '航班/航线号/日期'(300)."名称
356   p_hierarchy_header-tooltip = '停留显示'(400)."停留时显示
357   p_hierarchy_header-width = 30.
358   p_hierarchy_header-width_pix = ' '.
359 
360 ENDFORM.
361 
362 FORM exit_program.
363 
364   CALL METHOD g_custom_container->free.
365   LEAVE PROGRAM.
366 
367 ENDFORM.
368 
369 FORM register_events .
370   DATA: lt_events        TYPE cntl_simple_events,
371         l_event          TYPE cntl_simple_event,
372         l_event_receiver TYPE REF TO lcl_tree_event_receiver.
373 
374   CALL METHOD g_alv_tree->get_registered_events
375     IMPORTING
376       events = lt_events.
377 
378   l_event-eventid = cl_gui_column_tree=>eventid_node_double_click.
379   APPEND l_event TO lt_events.
380   l_event-eventid = cl_gui_column_tree=>eventid_item_double_click.
381   APPEND l_event TO lt_events.
382 
383   CALL METHOD g_alv_tree->set_registered_events
384     EXPORTING
385       events                    = lt_events
386     EXCEPTIONS
387       cntl_error                = 1
388       cntl_system_error         = 2
389       illegal_event_combination = 3.
390   IF sy-subrc <> 0.
391     MESSAGE x208(00) WITH 'ERROR'.                          "#EC NOTEXT
392   ENDIF.
393 
394   CREATE OBJECT l_event_receiver.
395   SET HANDLER l_event_receiver->handle_node_double_click FOR g_alv_tree.
396   SET HANDLER l_event_receiver->handle_item_double_click FOR g_alv_tree.
397 
398 ENDFORM.                    " register_events
posted @ 2022-06-28 09:33  TigerMan  阅读(680)  评论(0编辑  收藏  举报