Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

ALV Tree Create Hierarchy

Former Member
0 Likes
904
form create_hierarchy.

   data: ls_sflight type sflight,
         lt_sflight type sflight occurs 0,
         l_yyyymm(6) type c,            "year and month of sflight-fldate
         l_yyyymm_last(6) type c,
         l_carrid like sflight-carrid,
         l_carrid_last like sflight-carrid.

   data: l_month_key type lvc_nkey,
         l_carrid_key type lvc_nkey,
         l_last_key type lvc_nkey.

* §4a. Select data
  select * from sflight into table lt_sflight up to g_max rows.

* §4b. Sort output table according to your conceived hierarchy
* We sort in this order:
*    year and month (top level nodes, yyyymm of DATS)
*      carrier id (next level)
*         day of month (leaves, dd of DATS)
   sort lt_sflight by fldate+0(6) carrid fldate+6(2).
* Note: The top level nodes do not correspond to a field of the
* output table. Instead we use data of the table to invent another
* hierarchy level above the levels that can be build by sorting.

* §4c. Add data to tree

   loop at lt_sflight into ls_sflight.
* Prerequesite: The table is sorted.
* You add a node everytime the values of a sorted field changes.
* Finally, the complete line is added as a leaf below the last
* node.
     l_yyyymm = ls_sflight-fldate+0(6).
     l_carrid = ls_sflight-carrid.

* Top level nodes:
     if l_yyyymm <> l_yyyymm_last.      "on change of l_yyyymm
       l_yyyymm_last = l_yyyymm.

*Providing no key means that the node is added on top level:
       perform add_month using    l_yyyymm
                                       ''
                              changing l_month_key.
* The month changed, thus, there is no predecessor carrier
       clear l_carrid_last.
     endif.

* Carrier nodes:
* (always inserted as child of the last month
*  which is identified by 'l_month_key')
     if l_carrid <> l_carrid_last.      "on change of l_carrid
       l_carrid_last = l_carrid.
       perform add_carrid_line using    ls_sflight
                                        l_month_key
                               changing l_carrid_key.
     endif.

* Leaf:
* (always inserted as child of the last carrier
*  which is identified by 'l_carrid_key')
     perform add_complete_line using  ls_sflight
                                      l_carrid_key
                             changing l_last_key.
   endloop.

endform.                               " create_hierarchy

This program is BCALV_TREE_01.

Anyone mind explain how create hierarchy works?

How §4b works?? Is that really sorting the whole table?

§4c is adding data to the tree, but I dont understand how it determine which is the leaf node or top level node.

Please advice.

Thanks,

Wong

form create_hierarchy.

   data: ls_sflight type sflight,
         lt_sflight type sflight occurs 0,
         l_yyyymm(6) type c,            "year and month of sflight-fldate
         l_yyyymm_last(6) type c,
         l_carrid like sflight-carrid,
         l_carrid_last like sflight-carrid.

   data: l_month_key type lvc_nkey,
         l_carrid_key type lvc_nkey,
         l_last_key type lvc_nkey.

* §4a. Select data
  select * from sflight into table lt_sflight up to g_max rows.

* §4b. Sort output table according to your conceived hierarchy
* We sort in this order:
*    year and month (top level nodes, yyyymm of DATS)
*      carrier id (next level)
*         day of month (leaves, dd of DATS)
   sort lt_sflight by fldate+0(6) carrid fldate+6(2).
* Note: The top level nodes do not correspond to a field of the
* output table. Instead we use data of the table to invent another
* hierarchy level above the levels that can be build by sorting.

* §4c. Add data to tree

   loop at lt_sflight into ls_sflight.
* Prerequesite: The table is sorted.
* You add a node everytime the values of a sorted field changes.
* Finally, the complete line is added as a leaf below the last
* node.
     l_yyyymm = ls_sflight-fldate+0(6).
     l_carrid = ls_sflight-carrid.

* Top level nodes:
     if l_yyyymm <> l_yyyymm_last.      "on change of l_yyyymm
       l_yyyymm_last = l_yyyymm.

*Providing no key means that the node is added on top level:
       perform add_month using    l_yyyymm
                                       ''
                              changing l_month_key.
* The month changed, thus, there is no predecessor carrier
       clear l_carrid_last.
     endif.

* Carrier nodes:
* (always inserted as child of the last month
*  which is identified by 'l_month_key')
     if l_carrid <> l_carrid_last.      "on change of l_carrid
       l_carrid_last = l_carrid.
       perform add_carrid_line using    ls_sflight
                                        l_month_key
                               changing l_carrid_key.
     endif.

* Leaf:
* (always inserted as child of the last carrier
*  which is identified by 'l_carrid_key')
     perform add_complete_line using  ls_sflight
                                      l_carrid_key
                             changing l_last_key.
   endloop.

endform.                               " create_hierarchy

This program is BCALV_TREE_01.

Anyone mind explain how create hierarchy works?

How §4b works?? Is that really sorting the whole table?

§4c is adding data to the tree, but I dont understand how it determine which is the leaf node or top level node.

Please advice.

Thanks,

Wong

2 REPLIES 2
Read only

Former Member
0 Likes
707

Debug it and try to understand how it works

Read only

Former Member
0 Likes
707

Hi,

Well, everything is described in comments... What is that you don't understand??

* Prerequesite: The table is sorted.

* You add a node everytime the values of a sorted field changes.

* Finally, the complete line is added as a leaf below the last

* node.

* Top level nodes ---> "on change of l_yyyymm

*Providing no key means that the node is added on top level:

* The month changed, thus, there is no predecessor carrier

* Carrier nodes:

* (always inserted as child of the last month

*  which is identified by 'l_month_key')

* Leaf:

* (always inserted as child of the last carrier

*  which is identified by 'l_carrid_key')

Cheers,

Manu.