Tree (group by field relations)
xtt->MERGE( IV_BLOCK_NAME = ‘R’ IS_BLOCK =
{"TITLE"=>"Title", "T"=>[{"DIR"=>"R:", "PAR_DIR"=>"", "LEVEL"=>0, "SUM"=>2446345.0, "HAS_CHILDREN"=>"X"}, {"DIR"=>"R:\\docProps", "PAR_DIR"=>"R:", "LEVEL"=>1, "SUM"=>597144.0, "HAS_CHILDREN"=>""}, {"DIR"=>"R:\\xl", "PAR_DIR"=>"R:", "LEVEL"=>1, "SUM"=>1804679.0, "HAS_CHILDREN"=>"X"}, {"DIR"=>"R:\\xl\\printerSettings", "PAR_DIR"=>"R:\\xl", "LEVEL"=>2, "SUM"=>502146.0, "HAS_CHILDREN"=>""}, {"DIR"=>"R:\\xl\\theme", "PAR_DIR"=>"R:\\xl", "LEVEL"=>2, "SUM"=>227675.0, "HAS_CHILDREN"=>""}, {"DIR"=>"R:\\xl\\worksheets", "PAR_DIR"=>"R:\\xl", "LEVEL"=>2, "SUM"=>771720.0, "HAS_CHILDREN"=>"X"}, {"DIR"=>"R:\\xl\\worksheets\\_rels", "PAR_DIR"=>"R:\\xl\\worksheets", "LEVEL"=>3, "SUM"=>771720.0, "HAS_CHILDREN"=>""}, {"DIR"=>"R:\\xl\\_rels", "PAR_DIR"=>"R:\\xl", "LEVEL"=>2, "SUM"=>303138.0, "HAS_CHILDREN"=>""}, {"DIR"=>"R:\\_rels", "PAR_DIR"=>"R:", "LEVEL"=>1, "SUM"=>44522.0, "HAS_CHILDREN"=>""}], "C"=>[{"LEVEL"=>0, "SUB_NODES"=>[{"NAME"=>"R:\\docProps", "ATTR"=>{"LEVEL"=>1, "SUB_NODES"=>[], "DATA"=>{"DIR"=>"R:\\docProps", "PAR_DIR"=>"R:", "LEVEL"=>1, "SUM"=>597144.0, "HAS_CHILDREN"=>""}}}, {"NAME"=>"R:\\xl", "ATTR"=>{"LEVEL"=>1, "SUB_NODES"=>[{"NAME"=>"R:\\xl\\printerSettings", "ATTR"=>{"LEVEL"=>2, "SUB_NODES"=>[], "DATA"=>{"DIR"=>"R:\\xl\\printerSettings", "PAR_DIR"=>"R:\\xl", "LEVEL"=>2, "SUM"=>502146.0, "HAS_CHILDREN"=>""}}}, {"NAME"=>"R:\\xl\\theme", "ATTR"=>{"LEVEL"=>2, "SUB_NODES"=>[], "DATA"=>{"DIR"=>"R:\\xl\\theme", "PAR_DIR"=>"R:\\xl", "LEVEL"=>2, "SUM"=>227675.0, "HAS_CHILDREN"=>""}}}, {"NAME"=>"R:\\xl\\worksheets", "ATTR"=>{"LEVEL"=>2, "SUB_NODES"=>[{"NAME"=>"R:\\xl\\worksheets\\_rels", "ATTR"=>{"LEVEL"=>3, "SUB_NODES"=>[], "DATA"=>{"DIR"=>"R:\\xl\\worksheets\\_rels", "PAR_DIR"=>"R:\\xl\\worksheets", "LEVEL"=>3, "SUM"=>771720.0, "HAS_CHILDREN"=>""}}}], "DATA"=>{"DIR"=>"R:\\xl\\worksheets", "PAR_DIR"=>"R:\\xl", "LEVEL"=>2, "SUM"=>771720.0, "HAS_CHILDREN"=>"X"}}}, {"NAME"=>"R:\\xl\\_rels", "ATTR"=>{"LEVEL"=>2, "SUB_NODES"=>[], "DATA"=>{"DIR"=>"R:\\xl\\_rels", "PAR_DIR"=>"R:\\xl", "LEVEL"=>2, "SUM"=>303138.0, "HAS_CHILDREN"=>""}}}], "DATA"=>{"DIR"=>"R:\\xl", "PAR_DIR"=>"R:", "LEVEL"=>1, "SUM"=>1804679.0, "HAS_CHILDREN"=>"X"}}}, {"NAME"=>"R:\\_rels", "ATTR"=>{"LEVEL"=>1, "SUB_NODES"=>[], "DATA"=>{"DIR"=>"R:\\_rels", "PAR_DIR"=>"R:", "LEVEL"=>1, "SUM"=>44522.0, "HAS_CHILDREN"=>""}}}], "DATA"=>{"DIR"=>"R:", "PAR_DIR"=>"", "LEVEL"=>0, "SUM"=>2446345.0, "HAS_CHILDREN"=>"X"}}]}
).
Maybe trees based on subtotals are more common, but in sap there are some other hierarchical data like WBS elements or HR organizational units.
For this kind of data when number of sublevels are unknown and there is only relation parent - child
you can call the method TREE_CREATE_RELAT
.
TREE_CREATE_RELAT method
Folders hierarchy
BEGIN OF ts_tree_06,
" Folders hierarchy
dir TYPE string,
par_dir TYPE string,
" Empty field. Filled in on_prepare_tree_06
level TYPE i,
END OF ts_tree_06,
tt_tree_06 TYPE STANDARD TABLE OF ts_tree_06 WITH DEFAULT KEY,
" Document structure
BEGIN OF ts_root,
title TYPE string,
t TYPE REF TO data, " <-- Table of trees (better to use general REF TO)
END OF ts_root.
After filling table lt_folders (dir & par_dir) just pass fields’ names to TREE_CREATE_RELAT
method.
GET REFERENCE OF lt_folders INTO lr_table.
ls_root-t = zcl_xtt_replace_block=>tree_create_relat(
it_table = lr_table " from 7.5 REF #(lt_folders)
iv_node_key = 'DIR'
iv_relat_key = 'PAR_DIR' ).
All subtotals also filled in prepare_tree
handler. For demonstration purpose in the example only filled LEVEL
field.
METHOD on_prepare_tree_06.
FIELD-SYMBOLS:
<ls_data> TYPE ts_tree_06.
" Cast to specefic data
ASSIGN ir_data->* TO <ls_data>.
<ls_data>-level = ir_tree->level.
ENDMETHOD.
Outline level also would copy to subitems