Purpose
Use an ordinary internal table for flat output. Use an XTT tree when the report needs subtotals, group headers, or different layouts at different hierarchy levels. Demo ZCL_XTT_DEMO_050 builds such a tree by grouping fields with TREE_CREATE. For parent-child data, use TREE_CREATE_RELAT instead.
Build a grouped tree
" Document structure
BEGIN OF ts_root,
title TYPE string,
t TYPE REF TO data, " better to use general type than zcl_xtt_replace_block=>ts_tree
END OF ts_root.
ls_root-t = zcl_xtt_replace_block=>tree_create(
it_table = lr_table " from 7.5 REF #(lt_rows)
iv_fields = 'GROUP' ). " Name of the fields delimited by ;
Think of IV_FIELDS as the subtotal definition of an ALV grid:
iv_fields = 'BUKRS' " Subtotals by 1 field
iv_fields = 'BUKRS;WERKS' " Subtotals by 2 fields
Calculate parent values
Register a PREPARE_TREE handler when parent nodes need values calculated from their children:
on_prepare_tree_05 FOR EVENT prepare_tree OF zcl_xtt_replace_block
IMPORTING
ir_tree " Type Ref To ZCL_XTT_REPLACE_BLOCK=>TS_TREE
ir_data " Type Ref To DATA
ir_sub_data, " Type Ref To DATA
Cast the generic data references back to the application row type:
FIELD-SYMBOLS:
<ls_data> TYPE ts_tree_05,
<ls_sub_data> TYPE ts_tree_05.
" Cast to specific data
ASSIGN:
ir_data->* TO <ls_data>,
ir_sub_data->* TO <lt_sub_data>.
Then calculate the required totals:
" And calc sums
LOOP AT <lt_sub_data> ASSIGNING <ls_sub_data>.
<ls_data>-sum1 = <ls_data>-sum1 + <ls_sub_data>-sum1.
<ls_data>-sum2 = <ls_data>-sum2 + <ls_sub_data>-sum2.
ENDLOOP.
Design the template levels
Use ;level= to select a row layout for a hierarchy level. Add ;top=X when a parent row must appear before its children; omit it, or use an empty ;top=, to place the row afterward.
Excel outline levels defined in the template are copied to the generated rows.
The result follows the familiar ALV subtotal structure:
