Type Template Report
Excel Download template Download report
Generate this example Choose a template and create a fresh report with the demo service.

xtt->merge( iv_block_name = 'R' is_block =

JSON
{"TITLE":"Title","T":[{"GROUP":"GRP A","CAPTION":"<Caption 1 />","DATE":"2020-10-21","SUM1":5971.44,"SUM2":5021.46},{"GROUP":"GRP C","CAPTION":"<Caption 2 />","DATE":"2020-10-19","SUM1":2276.75,"SUM2":7717.2},{"GROUP":"GRP B","CAPTION":"<Caption 3 />","DATE":"2020-10-20","SUM1":3031.38,"SUM2":445.22},{"GROUP":"GRP A","CAPTION":"<Caption 4 />","DATE":"2020-10-21","SUM1":9476.79,"SUM2":3683.27},{"GROUP":"GRP A","CAPTION":"<Caption 5 />","DATE":"2020-10-21","SUM1":4604.0,"SUM2":5525.85},{"GROUP":"GRP C","CAPTION":"<Caption 6 />","DATE":"2020-10-19","SUM1":2159.71,"SUM2":2260.94},{"GROUP":"GRP B","CAPTION":"<Caption 7 />","DATE":"2020-10-20","SUM1":2490.3,"SUM2":6388.81},{"GROUP":"GRP A","CAPTION":"<Caption 8 />","DATE":"2020-10-21","SUM1":5476.81,"SUM2":406.18},{"GROUP":"GRP D","CAPTION":"<Caption 9 />","DATE":"2020-10-18","SUM1":689.66,"SUM2":518.6},{"GROUP":"GRP C","CAPTION":"<Caption 10 />","DATE":"2020-10-19","SUM1":5451.87,"SUM2":4061.85},{"GROUP":"GRP D","CAPTION":"<Caption 11 />","DATE":"2020-10-18","SUM1":4573.55,"SUM2":5436.0},{"GROUP":"GRP C","CAPTION":"<Caption 12 />","DATE":"2020-10-19","SUM1":1196.35,"SUM2":522.46},{"GROUP":"GRP C","CAPTION":"<Caption 13 />","DATE":"2020-10-19","SUM1":7686.95,"SUM2":2727.98},{"GROUP":"GRP D","CAPTION":"<Caption 14 />","DATE":"2020-10-18","SUM1":978.18,"SUM2":295.55},{"GROUP":"GRP D","CAPTION":"<Caption 15 />","DATE":"2020-10-18","SUM1":71.12,"SUM2":8878.56}]}

).


Generate demo 050

Tree group by fields

Request preview

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: