I create an ALV Tree with the class cl_salv_tree and want to expand a single node which is the parent to all other nodes and simultaneously the first one to be created.
I use the following code:
REPORT.
TYPES: BEGIN OF ty_kennzahlen,
beber TYPE t357-beber,
fing TYPE t357-fing,
END OF ty_kennzahlen.
DATA: go_tree TYPE REF TO cl_salv_tree,
gr_node TYPE REF TO cl_salv_node,
gr_nodes TYPE REF TO cl_salv_nodes,
gr_main_key TYPE lvc_nkey,
gt_kennzahlen TYPE STANDARD TABLE OF ty_kennzahlen,
lv_bebertext TYPE t357.
PARAMETERS dummy.
AT SELECTION-SCREEN OUTPUT.
IF go_tree IS NOT BOUND.
CALL METHOD cl_salv_tree=>factory
EXPORTING
r_container = cl_gui_container=>screen0
IMPORTING
r_salv_tree = go_tree
CHANGING
t_table = gt_kennzahlen.
gr_nodes = go_tree->get_nodes( ).
gr_node = gr_nodes->add_node(
related_node = '' " Schlüssel zum verwanten Knoten
relationship = cl_gui_column_tree=>relat_last_child " Knotenrelationen im Tree
collapsed_icon = CONV #( icon_closed_folder )
expanded_icon = CONV #( icon_open_folder )
row_style = if_salv_c_tree_style=>intensified
text = CONV #( 'Gesamt'(012) ) ). " ALV-Control: Zelleninhalt
gr_main_key = gr_node->get_key( ).
*Children of the parentnode:
DATA(lt_kennzahlen) = VALUE string_table( ( `Child 1` ) ( `Child 2` ) ).
LOOP AT lt_kennzahlen INTO DATA(lv_bebertext).
gr_node = gr_nodes->add_node(
related_node = gr_main_key " Schlüssel zum verwanten Knoten
relationship = cl_gui_column_tree=>relat_last_child " Knotenrelationen im Tree
collapsed_icon = CONV #( icon_closed_folder )
expanded_icon = CONV #( icon_open_folder )
row_style = if_salv_c_tree_style=>intensified
text = CONV #( lv_bebertext ) ). " ALV-Control: Zelleninhalt
ENDLOOP.
gr_nodes = go_tree->get_nodes( ).
gr_node = gr_nodes->get_node( node_key = gr_main_key ).
gr_node->expand( EXPORTING complete_subtree = abap_true ).
go_tree->get_tree_settings( )->set_hierarchy_size( 50 ).
go_tree->display( ).
ENDIF.
Here is my OOTB snippet based on standard tables:
Pay attention on the
WHILEblock at the end of the method: it does the expanding of the (always last) node by the key saved in the previous loop and adds the additional child node just after expanding.This is what I was referring to when saying "you can't just expand the node in the same loop". Your
LOOP AT lt_kennzahlenthat creates the nodes cannot be used for expanding them, 'cause the frontend is not updated immediately. That 's why we need separateWHILEloop.Why it is always last? Because we save it in every iteration so it stores the last value always, to get the root node use the
GET_PARENT( )method.