This reports helps to investigate events fired in ALV tree based on class CL_SALV_TREE.
Just start this report in your system and play with it. It should stop in the debugger when an event is fired.
If you would like to process clicks on hierarchy nodes (simmilar as hotspot clicks on cells in tables)
consider use of event DOUBLE_CLICK or LINK_CLICK (you have to set item for node to use LINK_CLICK).
To investigate KEYPRESS event, focus the item and press F1, F4 or ENTER.
REPORT zkmalve3.
TYPES:
* Types for structure and table of tree data
BEGIN OF ts_data,
node TYPE string,
descript TYPE string,
END OF ts_data,
tt_data TYPE TABLE OF ts_data,
* Types for structure and table of tree hierarchy
BEGIN OF ts_hier,
node TYPE string,
parent TYPE string,
key TYPE salv_de_node_key,
END OF ts_hier,
tt_hier TYPE TABLE OF ts_hier.
DATA:
gt_data TYPE tt_data, " Tree data
gt_hier TYPE tt_hier. " Tree hierarchy
*----------------------------------------------------------------------*
* CLASS cl_event_handler DEFINITION
*----------------------------------------------------------------------*
CLASS cl_event_handler DEFINITION.
PUBLIC SECTION.
CLASS-METHODS on_before_salv_function " BEFORE_SALV_FUNCTION
FOR EVENT if_salv_events_functions~before_salv_function
OF cl_salv_events_tree
IMPORTING e_salv_function.
CLASS-METHODS on_after_salv_function " AFTER_SALV_FUNCTION
FOR EVENT if_salv_events_functions~before_salv_function
OF cl_salv_events_tree
IMPORTING e_salv_function.
CLASS-METHODS on_added_function " ADDED_FUNCTION
FOR EVENT if_salv_events_functions~added_function
OF cl_salv_events_tree
IMPORTING e_salv_function.
CLASS-METHODS on_top_of_page " TOP_OF_PAGE
FOR EVENT if_salv_events_list~top_of_page
OF cl_salv_events_tree
IMPORTING r_top_of_page
page
table_index.
CLASS-METHODS on_end_of_page " END_OF_PAGE
FOR EVENT if_salv_events_list~end_of_page
OF cl_salv_events_tree
IMPORTING r_end_of_page
page.
CLASS-METHODS on_double_click " DOUBLE_CLICK
FOR EVENT if_salv_events_tree~double_click
OF cl_salv_events_tree
IMPORTING node_key
columnname.
CLASS-METHODS on_link_click " LINK_CLICK
FOR EVENT if_salv_events_tree~link_click
OF cl_salv_events_tree
IMPORTING columnname
node_key.
CLASS-METHODS on_keypress " KEYPRESS
FOR EVENT if_salv_events_tree~keypress
OF cl_salv_events_tree
IMPORTING node_key
columnname
key.
CLASS-METHODS on_checkbox_change " CHECKBOX_CHANGE
FOR EVENT if_salv_events_tree~checkbox_change
OF cl_salv_events_tree
IMPORTING columnname
node_key
checked.
CLASS-METHODS on_expand_empty_folder " EXPAND_EMPTY_FOLDER
FOR EVENT if_salv_events_tree~expand_empty_folder
OF cl_salv_events_tree
IMPORTING node_key.
ENDCLASS. "cl_event_handler DEFINITION
*----------------------------------------------------------------------*
* CLASS cl_event_handler IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS cl_event_handler IMPLEMENTATION.
METHOD on_before_salv_function.
BREAK-POINT.
ENDMETHOD. "on_before_salv_function
METHOD on_after_salv_function.
BREAK-POINT.
ENDMETHOD. "on_after_salv_function
METHOD on_added_function.
BREAK-POINT.
ENDMETHOD. "on_added_function
METHOD on_top_of_page.
BREAK-POINT.
ENDMETHOD. "on_top_of_page
METHOD on_end_of_page.
BREAK-POINT.
ENDMETHOD. "on_end_of_page
METHOD on_double_click.
BREAK-POINT.
ENDMETHOD. "on_double_click
METHOD on_link_click.
BREAK-POINT.
ENDMETHOD. "on_link_click
METHOD on_keypress.
BREAK-POINT.
ENDMETHOD. "on_keypress
METHOD on_checkbox_change.
BREAK-POINT.
ENDMETHOD. "on_checkbox_change
METHOD on_expand_empty_folder.
BREAK-POINT.
ENDMETHOD. "on_expand_empty_folder
ENDCLASS. "cl_event_handler IMPLEMENTATION
START-OF-SELECTION.
* Read the tree data and hierarchy
PERFORM scan_hier CHANGING gt_data gt_hier.
* Display tree
PERFORM display_alv_tree.
*&---------------------------------------------------------------------*
*& Form scan_hier
*&---------------------------------------------------------------------*
FORM scan_hier CHANGING pt_data TYPE tt_data
pt_hier TYPE tt_hier.
DATA:
ls_data TYPE ts_data,
ls_hier TYPE ts_hier.
ls_data-node = 'A'. ls_data-descript = 'Root'. APPEND ls_data TO pt_data.
ls_data-node = 'B1'. ls_data-descript = 'Node'. APPEND ls_data TO pt_data.
ls_data-node = 'B2'. ls_data-descript = 'Item'. APPEND ls_data TO pt_data.
ls_data-node = 'C1'. ls_data-descript = 'Item'. APPEND ls_data TO pt_data.
ls_data-node = 'C2'. ls_data-descript = 'Item'. APPEND ls_data TO pt_data.
ls_data-node = 'C3'. ls_data-descript = 'Item'. APPEND ls_data TO pt_data.
ls_data-node = 'C4'. ls_data-descript = 'Item'. APPEND ls_data TO pt_data.
ls_hier-node = 'A'. ls_hier-parent = ''. APPEND ls_hier TO pt_hier.
ls_hier-node = 'B1'. ls_hier-parent = 'A'. APPEND ls_hier TO pt_hier.
ls_hier-node = 'B2'. ls_hier-parent = 'A'. APPEND ls_hier TO pt_hier.
ls_hier-node = 'C1'. ls_hier-parent = 'B1'. APPEND ls_hier TO pt_hier.
ls_hier-node = 'C2'. ls_hier-parent = 'B1'. APPEND ls_hier TO pt_hier.
ls_hier-node = 'C3'. ls_hier-parent = 'B1'. APPEND ls_hier TO pt_hier.
ls_hier-node = 'C4'. ls_hier-parent = 'B1'. APPEND ls_hier TO pt_hier.
ENDFORM. "scan_hier
*&---------------------------------------------------------------------*
*& Form display_alv_tree
*&---------------------------------------------------------------------*
FORM display_alv_tree.
DATA:
lt_empty TYPE tt_data,
ls_data TYPE ts_data,
ls_hier TYPE ts_hier,
lv_text TYPE lvc_value,
lv_key TYPE salv_de_node_key,
lo_tree TYPE REF TO cl_salv_tree,
lo_nodes TYPE REF TO cl_salv_nodes,
lo_node TYPE REF TO cl_salv_node,
lo_item TYPE REF TO cl_salv_item,
lo_columns TYPE REF TO cl_salv_columns,
lo_column TYPE REF TO cl_salv_column,
lo_tree_settings TYPE REF TO cl_salv_tree_settings,
lo_events TYPE REF TO cl_salv_events_tree.
FIELD-SYMBOLS:
<fs_hier> TYPE ts_hier.
TRY.
* 1. Create instance with an empty table
CALL METHOD cl_salv_tree=>factory
IMPORTING
r_salv_tree = lo_tree
CHANGING
t_table = lt_empty.
lo_events = lo_tree->get_event( ).
SET HANDLER cl_event_handler=>on_before_salv_function FOR lo_events.
SET HANDLER cl_event_handler=>on_after_salv_function FOR lo_events.
SET HANDLER cl_event_handler=>on_added_function FOR lo_events.
SET HANDLER cl_event_handler=>on_top_of_page FOR lo_events.
SET HANDLER cl_event_handler=>on_end_of_page FOR lo_events.
SET HANDLER cl_event_handler=>on_double_click FOR lo_events.
SET HANDLER cl_event_handler=>on_link_click FOR lo_events.
SET HANDLER cl_event_handler=>on_keypress FOR lo_events.
SET HANDLER cl_event_handler=>on_checkbox_change FOR lo_events.
SET HANDLER cl_event_handler=>on_expand_empty_folder FOR lo_events.
* Add key ENTER to raise KEYPRESS event, default keys are F1 and F4
* (Attention: item has to be focused to send this event)
CALL METHOD lo_events->add_key_for_keypress( key = if_salv_c_keys=>enter ).
* 2. Add the nodes to the tree and set their relations
lo_nodes = lo_tree->get_nodes( ).
LOOP AT gt_hier ASSIGNING <fs_hier>.
IF <fs_hier>-parent = ''.
* Add the node as root
lo_node = lo_nodes->add_node(
related_node = ''
relationship = if_salv_c_node_relation=>parent ).
ELSE.
* Read the ALV key of parent node and add as a child
READ TABLE gt_hier INTO ls_hier
WITH KEY node = <fs_hier>-parent.
lo_node = lo_nodes->add_node(
related_node = ls_hier-key
relationship = if_salv_c_node_relation=>last_child ).
ENDIF.
* Save the ALV internal key
<fs_hier>-key = lo_node->get_key( ).
* Add the data of the tree
READ TABLE gt_data INTO ls_data
WITH KEY node = <fs_hier>-node.
lo_node->set_data_row( ls_data ).
lv_text = ls_data-node.
lo_node->set_text( lv_text ).
CASE <fs_hier>-node.
WHEN 'C2'.
* Set node as link
* in such case, single click on node fires the event LINK_CLICK
* without this setting, double click on node fires the event DOUBLE_CLICK
lo_item = lo_node->get_hierarchy_item( ).
lo_item->set_type( if_salv_c_item_type=>link ).
WHEN 'C3'.
* Set node as button
lo_item = lo_node->get_hierarchy_item( ).
lo_item->set_type( if_salv_c_item_type=>button ).
WHEN 'C4'.
* Set node as checkbox
lo_item = lo_node->get_hierarchy_item( ).
lo_item->set_type( if_salv_c_item_type=>checkbox ).
ENDCASE.
ENDLOOP.
* Do some final format tasks
lo_columns = lo_tree->get_columns( ).
lo_columns->set_optimize( abap_true ).
lo_column = lo_columns->get_column( 'NODE' ).
lo_column->set_visible( abap_false ).
lo_tree_settings = lo_tree->get_tree_settings( ).
lo_tree_settings->set_hierarchy_header( 'Node' ).
lo_nodes->expand_all( ).
* Display Tree
lo_tree->display( ).
CATCH cx_salv_not_found. " for cl_salv_columns=>get_column
WRITE: / 'Error: ALV Ausnahme CX_SALV_NOT_FOUND'.
STOP.
CATCH cx_salv_error. " for cl_salv_tree=>factory
WRITE: / 'Error: ALV Ausnahme CX_SALV_ERROR'.
STOP.
ENDTRY.
ENDFORM. "display_alv_tree
See other related notes on my website: