Search code examples
abapalv

ALV Tree node events are not fired


I experience a problem when I use ALV Tree without item selection enabled. In this case I am not able to receive an event for node context menu request. When I turn item selection on, everything works fine. I get the events for the context menu request for nodes as well as items, but i don't need item selection enabled for my application.

Here is some code I can share:

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                  = ''.

And here I register the events:

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.

And here are my handlers:

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.

So both methods are working when item_selection is set to true. Neither of them is working, when item_selection is set to false. However, I would have expected the handle_node_ctmenu_request would be fired in this case.


Solution

  • The trick is in the interconnection of different types of events in ALV Tree Model.
    Official SAP Control Framework documentation states that:

    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
    ...

    But, unfortunately, it doesn't state (it is implied, like many else in SAP world) that Control Framework treats item and node events in pool.
    So, if you register item-related events with disabled item_selection parameter, the node-related events will not work as well.
    In other words, do not register item-related events, if you want to achieve reaction to your node-related events, or set this parameter to "Enabled".