在上一篇中讲学习了如何来实现一个simple tree,在这篇中将学习如何实现simple tree中的double click事件。
simple tree中的double事件与alvgrid中的double click事件基本上一样。在Repository Brower中选择class/interface下的CL_SIMPLE_TREE_MODEL,展开Events-〉Inherited Events,将看到一个NODE_DOUBLE_CLICK的事件,该事件有一个NODE_KEY的参数。了解了这些之后,进行具体实现;
一,定义一个用来实现Node_double_click事件的class,代码如下:
CLASS grid_event_receiver DEFINITION.
PUBLIC SECTION.
METHODS:
handler_double_click FOR EVENT NODE_DOUBLE_CLICK of cl_simple_tree_model
IMPORTING NODE_KEY.
ENDCLASS. "grid_event_receiver DEFINITION
二,实现handler_double_click方法。在handler_double_click事件中首先根据传进来的Node_key,调用 NODE_GET_PARENT 方法来获取其父节点,调用 NODE_GET_CHILDREN方法来获取孩子节点。然后根据父节点子节点来判断我们在右边的alvgrid中将要显示什么数据。最后创建alvgrid对象grid1的实例,然后显示数据。具体代码如下:
DATA: parent_key TYPE TM_NODEKEY,
Child_table TYPE TREEMNOTAB,
KEY_VALE(8) TYPE C.
CALL METHOD simple_tree->NODE_GET_PARENT
EXPORTING
NODE_KEY = P_NODE_KEY
IMPORTING
PARENT_NODE_KEY = parent_key.
CALL METHOD simple_tree->NODE_GET_CHILDREN
EXPORTING
NODE_KEY = P_NODE_KEY
IMPORTING
NODE_KEY_TABLE = Child_table.
IF parent_key is INITIAL.
KEY_VALE = P_NODE_KEY.
ELSEIF Child_table is INITIAL.
KEY_VALE = P_NODE_KEY.
ELSE.
CONCATENATE parent_key P_NODE_KEY INTO KEY_VALE.
ENDIF.
REFRESH g_sflight_Detail.
LOOP AT g_sflight.
if g_sflight-fldate CS P_KEY_VALE.
move-CORRESPONDING g_sflight to g_sflight_Detail.
APPEND g_sflight_Detail.
CLEAR g_sflight_Detail.
ENDIF.
ENDLOOP.
if grid1 is INITIAL.
CREATE OBJECT grid1
EXPORTING
I_PARENT = w_body.
endif.
CALL METHOD grid1->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
I_STRUCTURE_NAME = 'SFLIGHT'
CHANGING
IT_OUTTAB = g_sflight_Detail[].
CALL METHOD grid1->REFRESH_TABLE_DISPLAY.
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
call method cl_gui_cfw=>flush.
三,将handler_double_click 事件注册到simple tree上去。具体代码为:
DATA: event TYPE cntl_simple_event,
events TYPE cntl_simple_events. ,
event-eventid = cl_simple_tree_model=>eventid_node_double_click.
event-appl_event = 'X'.
APPEND event TO events.
CALL METHOD simple_tree->SET_REGISTERED_EVENTS
EXPORTING
EVENTS = events.
CREATE OBJECT event_receiver.
SET HANDLER event_receiver->handler_double_click FOR simple_tree.
OK.在simple tree中添加double click事件就学习完了,在该篇中没有详细描述,因为在前面的文章中已经描述了如何在ALVGRID中添加事件,大概都类似。
该事例的完整代码如下: