Tree ALV in ABAP

How to Create Tree ALV? 

Thu May 26, 2022

Complete Program

There are three 4 major steps:-

            1. data declaration

            2. fetching the data

            3. building the internal table for tree

            4. display the tree using fm

_________________________________________________

report ztreealv.

types : begin of ty_scarr ,

             carrid type scarr-carrid,

              carrname type scarr-carrname,
end of ty_scarr.
types : begin of ty_spfli,
             carrid type spfli-carrid,
              connid type spfli-connid,
             end of ty_spfli.
types : begin of ty_sflight,
              carrid type sflight-carrid,
              connid type sflight-connid,
               fldate type sflight-fldate,
             end of ty_sflight.
data : it_scarr type table of ty_scarr,
            wa_scarr type ty_scarr,
            it_spfli type table of ty_spfli,
            wa_spfli type ty_spfli,
            it_sflight type table of ty_sflight,
            wa_sflight type ty_sflight,
            it_node type table of snodetext,
            wa_node type snodetext.
start-of-selection.
           perform data-fetch.      "data preparation - fetch the data from table
           perform build_tree.      "build the tree using standard fm
           perform display_tree.   "display tree using standard fm
*-------------------------------------------------------------*
form data-fetch
select carried carrname from scarr into table it_scarr
if it_scarr is not initial.
select carrid connid from spfli into table it_spfli for all entries in it_scarr                     where carrid = it_scarr-carrid.
if it_spfli is not initial.
       select carried   connid  fldate from sflight into table it_sflight for all                                               entries in it_spfli where carrid = it_spfli-carrid and                                                      connid it_spfli-connid.
endif.
endif.
sort : it_scarr by carrid,
it_spfli by carrid connid,
it_sflight by carrid connid fldate.
endform" end  of data-fetch
form build_tree . "begin of building tree

"Build the internal table for building tree
clear : wa_node, it_node.

"tree node - root node - very first node that is why here type is t ( tree ), 

"tleve is '01' i.e. very first node, 
wa_node-type = 'T'.
wa_node-name = 'Airline'.
wa_node-tlevel = '01'.     "very first node
wa_node-nlength = '08'. "length of the first node or root node
wa_node-color = '05'.  " color code 00 to 07 | 0 - background colour,1 - blue,2 -                                                "gray,3 - yellow,4 - blue/gray,5 - green,6 - red,7 - orange
wa_node-text = 'CODE'.   "text for the root node - will appear as a new column
wa_node-tlength = '04'.   "text length
wa_node-tcolor = '05'.     "text color
append wa_node to it_node. "populate internal table with root node                                                                            "information
clear wa_node.
loop at it_scarr into wa_scarr. "1st level loop
            wa_node-name = 'CARRID'.
            wa_node-tlevel = '02'.
            wa_node-nlength = '15'. "node length
            wa_node-color = '06'.
            wa_node-text = wa_scarr-carrid.
            wa_node-tlength = '20'. " text length
            wa_node-tcolor = '06'.
            wa_node-text1 = wa_scarr-carrname. " first column name
            wa_node-tlength1 = '04'. "first column length
            wa_node-tcolor1 = '06'.   "first column color
      append wa_node to it_node.
       loop at it_spfli into wa_spfli. " 2nd level loop - for each carrier id get the                                                                       "connection id
               wa_node-name = 'CONNID'. "node name
               wa_node-tlevel = '03'.
               wa_node-nlength = '06'.
               wa_node-color = '04'.
               wa_node-text = wa_spfli-connid. "made value
               wa_node-tlength = '04'
               wa_node-tcolor = '04'.
               append wa_node to it_node.
                clear wa_node.
          loop at it_sflight into wa_sflight. "3rd level loop - get the date of of                                                                                       "each cnnid( connecting flight )
                 wa_node-name = 'fldate'.
                 wa_node-tlevel = '04'.
                 wa_node-color = '04'.
                 wa_node-text = wa_sflight-fldate.
                 wa_node-tlength = '10'.
                 wa_node-tcolor = '04'.
              append wa_node to it_node.
                clear wa_node.
            endloop.
     endloop.
endloop.
endform. "build_tree

*-------------------------------------------------------------*
form display_tree .
call function 'RS_TREE_CONSTRUCT'
tables
nodetab = it_node.
call function 'RS_TREE_LIST_DISPLAY'
exporting  callback_program = sy-cprog.
endform. " DISPLAY_TREE

OUTPUT -

Pankaj Kumar
Pankaj kumar is a passionate trainer.  He has created many youtube videos and provided training on ABAP, OOPS ABAP, ABAP ON HANA, ODATA, ALED IDOCS, etc.

Youtube Link: www.youtube.com/pankajkumaram

Launch your GraphyLaunch your Graphy
100K+ creators trust Graphy to teach online
𝕏
SAP TECH MADE EASY 2024 Privacy policy Terms of use Contact us Refund policy