'How to capture expand node event in SALV Tree?

I've created an ALV tree view with use of the class CL_SALV_TREE.

Everything works fine but I'd like to grap the event when a node is expanded, for filling in a function that automatically resizes the tree columns.

My problem is, that the expanding event is the private method HANDLE_EXPAND_NC of class CL_GUI_ALV_TREE and I actually don't know how to capture this event. I'm open for any ideas and thanks for your time.



Solution 1:[1]

As far as I know, you can only be notified if a folder is expanded that does not yet contain children (the _NC part of the method/event name signifies that). This is intended for lazy loading of the tree. The tree displays in the SAP menu or the IMG are a good example for that - you can actually see parts of the tree being loaded when you expand the top-level nodes.

If that is sufficient for you, use the event EXPAND_EMPTY_FOLDER of the interface IF_SALV_EVENTS_TREE, implemented by CL_SALV_EVENTS_TREE. There doesn't seem to be a good demo program for this though.

Solution 2:[2]

Here is a minimal reproducible example which shows that the event EXPAND_EMPTY_FOLDER is triggered when you expand a node which has no children initially, a child node is added during that event:

CLASS lcl_app DEFINITION.
  PUBLIC SECTION.
    METHODS pbo
      RAISING
        cx_salv_error.
  PRIVATE SECTION.
    DATA: salv   TYPE REF TO cl_salv_tree,
          scarrs TYPE STANDARD TABLE OF scarr.
    METHODS on_expand_empty_folder
      FOR EVENT expand_empty_folder
                  OF cl_salv_events_tree
      IMPORTING node_key.
ENDCLASS.

CLASS lcl_app IMPLEMENTATION.
  METHOD pbo.
    IF salv IS NOT BOUND.
      cl_salv_tree=>factory( EXPORTING r_container = cl_gui_container=>screen0
                             IMPORTING r_salv_tree = salv
                             CHANGING  t_table     = scarrs ).
      DATA(lo_settings) = salv->get_tree_settings( ).
      lo_settings->set_hierarchy_size( 30 ).
      DATA(event) = salv->get_event( ).
      salv->get_functions( )->add_function( name = 'NEW' text = 'NEW' tooltip = '' position = 1 ).
      SET HANDLER on_expand_empty_folder FOR event.
      SELECT * FROM scarr INTO TABLE @DATA(local_scarrs).
      LOOP AT local_scarrs REFERENCE INTO DATA(scarr).
        salv->get_nodes( )->add_node(
          related_node = space " (root node)
          relationship = cl_gui_column_tree=>relat_last_child
          text         = |{ scarr->carrid } - { scarr->carrname }|
          data_row     = scarr->*
          folder       = abap_true
          expander     = abap_true ).
      ENDLOOP.
      salv->display( ).
    ENDIF.
    LOOP AT SCREEN.
      screen-active = '0'.
      MODIFY SCREEN.
    ENDLOOP.
  ENDMETHOD.
  METHOD on_expand_empty_folder.
    TRY.
        DATA(scarr) = CAST scarr( salv->get_nodes( )->get_node( node_key )->get_data_row( ) )->*.
        salv->get_nodes( )->add_node(
          related_node = node_key
          relationship = cl_gui_column_tree=>relat_last_child
          text         = |Node added at time of expand below { scarr-carrid }| ).
      CATCH cx_root INTO DATA(lx).
        MESSAGE lx TYPE 'I' DISPLAY LIKE 'E'.
        LEAVE PROGRAM.
    ENDTRY.
  ENDMETHOD.
ENDCLASS.

PARAMETERS dummy.

LOAD-OF-PROGRAM.
  DATA(app) = NEW lcl_app( ).

AT SELECTION-SCREEN OUTPUT.
  TRY.
      app->pbo( ).
    CATCH cx_root INTO DATA(lx).
      MESSAGE lx TYPE 'S' DISPLAY LIKE 'E'.
  ENDTRY.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vwegert
Solution 2 Sandra Rossi