不触发 ALV 树节点事件

ALV Tree node events are not fired

我在未启用项目选择的情况下使用 ALV 树时遇到问题。在这种情况下,我无法接收节点上下文菜单请求的事件。 当我打开项目选择时,一切正常。 我获得了节点和项目的上下文菜单请求事件,但我不需要为我的应用程序启用项目选择。

这是我可以分享的一些代码:

DATA: g_tree TYPE REF TO cl_gui_alv_tree.
* create container for alv-tree
DATA: l_tree_container_name(30) TYPE c,
      l_custom_container        TYPE REF TO cl_gui_custom_container.

l_tree_container_name = 'TREE'.

CREATE OBJECT l_custom_container
 EXPORTING
  container_name              = l_tree_container_name.

* create tree control
CREATE OBJECT g_tree
 EXPORTING
  parent                      = l_custom_container
  node_selection_mode         = cl_gui_column_tree=>node_sel_mode_single
  item_selection              = abap_true "WOULD LIKE TO HAVE THIS SET TO FALSE
  no_html_header              = abap_true
  no_toolbar                  = ''.

我在这里注册事件:

DATA: lt_events TYPE cntl_simple_events,
      l_event   TYPE cntl_simple_event.

l_event-eventid = cl_gui_column_tree=>eventid_node_context_menu_req.
APPEND l_event TO lt_events.
l_event-eventid = cl_gui_column_tree=>eventid_item_context_menu_req.
APPEND l_event TO lt_events.

CALL METHOD g_tree->set_registered_events
  EXPORTING
    events                    = lt_events
  EXCEPTIONS
    cntl_error                = 1
    cntl_system_error         = 2
    illegal_event_combination = 3.

DATA: l_event_receiver TYPE REF TO tree_event_receiver.
CREATE OBJECT l_event_receiver.

 SET HANDLER l_event_receiver->handle_node_ctmenu_request FOR g_tree.
 SET HANDLER l_event_receiver->handle_node_ctmenu_selected FOR g_tree.
 SET HANDLER l_event_receiver->handle_item_ctmenu_request FOR g_tree.
 SET HANDLER l_event_receiver->handle_item_ctmenu_selected FOR g_tree.

这是我的处理人员:

METHOD handle_node_ctmenu_request.
  CALL METHOD menu->add_function
      EXPORTING
        fcode = 'DELETE_ITEM'
        text  = 'delete'.
ENDMETHOD.
METHOD handle_item_ctmenu_request.
CALL METHOD menu->add_function
      EXPORTING
        fcode = 'DELETE_ITEM'
        text  = 'delete'.
ENDMETHOD.

因此,当 item_selection 设置为 true 时,这两种方法都有效。当 item_selection 设置为 false 时,它​​们都不起作用。但是,我原以为 handle_node_ctmenu_request 在这种情况下会被解雇。

诀窍在于 ALV 树模型中不同类型事件的互连。
官方 SAP Control Framework documentation 指出:

If you set the parameter item_selection = 'X' when you created the instance, you can also react to the following events:
...
ITEM_CONTEXT_MENU_REQUEST
ITEM_CONTEXT_MENU_SELECT
...

但是,不幸的是,它没有说明(暗示,就像 SAP 世界中的许多其他内容一样)控制框架处理项目和节点事件在池.
因此,如果您使用禁用的 item_selection 参数注册与项目相关的事件,则与节点相关的事件 将无法正常工作 .
也就是说,不要注册物品相关事件,如果你想实现对你的节点相关事件的反应,或者将这个参数设置为"Enabled"。