我有一个 CL_SIMPLE_TREE_MODEL
的实例,我设法触发和处理 CONTEXT_MENU_REQUEST
事件,然后构建上下文菜单。
我按照我想要的方式添加了我的功能。
问题是,当我从上下文菜单中选择其中一个选项时,什么也不会发生。换句话说,程序流不在CONTEXT_MENU_SELECT
的处理程序内部。
当我单击上下文菜单中的功能时,我当然假设此事件是触发的。
我找到了官方文档,但仅适用于您使用Shift F10访问的"默认上下文菜单",该程序需要该程序中的某些子例程才能启动。
我做了什么:
- 我确实定义并实现了该事件处理程序的方法。
- 我确实将事件的处理程序设置在我为
CONTEXT_MENU_REQUEST
事件设置处理程序的同一地点 - 我没有在
SET_REGISTERED_EVENTS
上注册该事件,因为该事件的类属性中没有定义ID,例如CONTEXT_MENU_REQUEST
事件。
代码:
REPORT.
CLASS lcl_tree_handler DEFINITION.
PUBLIC SECTION.
METHODS:
pbo,
on_ctx_menu_request FOR EVENT node_context_menu_request OF cl_simple_tree_model
IMPORTING node_key menu sender, "TYPE TM_NODEKEY CL_CTMENU
on_ctx_menu_select FOR EVENT node_context_menu_select OF cl_simple_tree_model
IMPORTING node_key fcode. "TYPE TM_NODEKEY SY-UCOMM
DATA:
po_tree_model TYPE REF TO cl_simple_tree_model,
gt_tree TYPE TABLE OF treemsnodt,
control TYPE REF TO cl_gui_control.
ENDCLASS.
CLASS lcl_tree_handler IMPLEMENTATION.
METHOD pbo.
DATA: lt_events TYPE cntl_simple_events,
ls_event TYPE cntl_simple_event.
FIELD-SYMBOLS <gs_tree> TYPE treemsnodt.
CHECK po_tree_model IS NOT BOUND.
CREATE OBJECT po_tree_model
EXPORTING
node_selection_mode = po_tree_model->node_sel_mode_single.
APPEND INITIAL LINE TO gt_tree ASSIGNING <gs_tree>.
<gs_tree>-node_key = 'Node key 1'.
<gs_tree>-text = 'First node'.
<gs_tree>-isfolder = 'X'.
APPEND INITIAL LINE TO gt_tree ASSIGNING <gs_tree>.
<gs_tree>-node_key = 'Node key 2'.
<gs_tree>-relatkey = 'Node key 1'.
<gs_tree>-relatship = cl_tree_model=>relat_last_child.
<gs_tree>-text = 'First child'.
po_tree_model->add_nodes(
node_table = gt_tree ).
ls_event-eventid = cl_simple_tree_model=>eventid_node_context_menu_req.
ls_event-appl_event = 'X'. "tried with space too
APPEND ls_event TO lt_events.
CALL METHOD po_tree_model->set_registered_events
EXPORTING
events = lt_events.
SET HANDLER on_ctx_menu_request FOR po_tree_model.
SET HANDLER on_ctx_menu_select FOR po_tree_model.
po_tree_model->create_tree_control(
EXPORTING
parent = cl_gui_container=>screen0
IMPORTING
control = control ).
ENDMETHOD.
METHOD on_ctx_menu_request. "I initialize the context menu object here.
DATA: lt_chidren_keys TYPE treemnotab,
ls_child_key TYPE tm_nodekey,
lv_text TYPE gui_text.
CALL METHOD sender->node_get_children
EXPORTING
node_key = node_key
IMPORTING
node_key_table = lt_chidren_keys
EXCEPTIONS
OTHERS = 2.
LOOP AT lt_chidren_keys INTO ls_child_key.
lv_text = ls_child_key.
CALL METHOD menu->add_function
EXPORTING
fcode = 'ONE'
text = lv_text
ftype = 'B'.
ENDLOOP.
menu->add_separator( ).
CALL METHOD menu->add_function
EXPORTING
fcode = 'ALL'
text = 'All the work groups'
ftype = 'W'.
ENDMETHOD.
METHOD on_ctx_menu_select.
BREAK-POINT. "tried actual code here too.
ENDMETHOD.
ENDCLASS.
DATA: go_tree_handler TYPE REF TO lcl_tree_handler.
PARAMETERS dummy.
INITIALIZATION.
CREATE OBJECT go_tree_handler.
AT SELECTION-SCREEN OUTPUT.
go_tree_handler->pbo( ).
AT SELECTION-SCREEN ON EXIT-COMMAND.
go_tree_handler->control->free( ).
展示树,在右键上单击上下文菜单。
,但是当我选择菜单项时,什么都没有发射。我想念什么吗?
不支持您指定的功能类型(ftype = 'W'
和'B'
)(检查基础域CUA_FUNTYP
的固定值)。在这种情况下,什么都没有发生。
经典解决方案是使用ftype = ' '
(正常函数):
LOOP AT lt_chidren_keys INTO ls_child_key.
lv_text = ls_child_key.
CALL METHOD menu->add_function
EXPORTING
fcode = 'ONE'
text = lv_text
ftype = ' '.
ENDLOOP.
menu->add_separator( ).
CALL METHOD menu->add_function
EXPORTING
fcode = 'ALL'
text = 'All the work groups'
ftype = ' '.
可能的ftype
值是(来源:域CUA_FUNTYP
):
- '':正常功能
- 'H':帮助功能(帮助请求处理)
- 's':系统功能(直接由Dynp处理)
- 't':交易呼叫(交易离开)
- 'e':``at exit命令''的访问模块 ->/e作为前缀
- 'i':包括菜单(在运行时替换 - 不支持)
- 'n':'在exit命令'函数,> dynp>/n作为前缀
备注:问题的第一个版本中的代码缺少第二个事件处理程序SET HANDLER go_tree_handler->on_ctx_menu_select FOR po_tree_model.
的注册(现在没关系)