This program generates a Tree Report, with nodes and child nodes, which can be expanded & collapsed.
This program can be used, by adding nodes, or by making any changes in the tree structure.
REPORT ZPROG_TREE_REPORT
LINE-COUNT 65
LINE-SIZE 80
NO STANDARD PAGE HEADING.
*** Data Declarations ***
DATA: BEGIN OF items OCCURS 100,
id(10),
parent_id(10),
text(20),
symbol,
END OF items,
tabix_stack LIKE sy-tabix OCCURS 10 WITH HEADER LINE,
items_show LIKE items OCCURS 100 WITH HEADER LINE.
INCLUDE <symbol>.
*** To Append The Items ***
PERFORM append_item USING:
*** Items Under 1st Level 0 Node
'1' '' 'Drinks',
'4' '1' 'Cold Drinks',
'7' '4' 'Coke',
'8' '4' 'Milk Shakes',
'5' '1' 'Hot Drinks',
'10' '5' 'Hot Coffee',
*** Items Under 2nd Level 0 Node
'2' '' 'Yummys',
'14' '2' 'Puffs',
'15' '14' 'Veg Puff',
'6' '2' 'Pizza',
'18' '6' 'Veg',
'28' '18' 'Special Pizzas',
'33' '28' 'Thick Crust',
'19' '33' 'Medium',
'22' '19' 'Veg Cheesy',
'23' '19' 'Schezwan Treat',
'20' '33' 'Large',
'24' '20' 'Veg Supreme',
'25' '20' 'Pep n Corn',
'34' '28' 'Thin Crust',
'31' '34' 'Medium',
'32' '31' 'Jain Special',
'35' '31' 'Veggie Delite',
'29' '18' 'Indian',
'30' '29' 'Punjabi Paneer Pizza',
'9' '6' 'Non-Veg',
'12' '9' 'Shredded Chicken Pizza',
'13' '9' 'Spicy Chicken Pizza',
*** Items Under 3rd Level 0 Node
'11' '3' 'Payment',
'3' '' 'Bill',
'16' '11' 'By Cash',
'17' '11' 'By Card'.
*** To Show Items At Level 0, Which Are Also Known As Parentless Items ***
LOOP AT items WHERE parent_id = ''.
MOVE-CORRESPONDING items TO items_show.
items_show-symbol = '+'.
APPEND items_show.
ENDLOOP.
PERFORM print_tree TABLES items_show.
*** When A Node Is Expanded / Collapsed / Any Item Is Double Clicked ***
AT LINE-SELECTION.
READ TABLE items WITH KEY parent_id = items_show-id. "see 'hide'
IF sy-subrc = 0. "item has children - expand or collapse
sy-lsind = 0.
PERFORM expand_collapse USING items_show-id.
PERFORM print_tree TABLES items_show.
ELSE. "item has NO children - perform some action
READ TABLE items WITH KEY id = items_show-id.
WRITE: 'Action performed on item "' NO-GAP, items-text NO-GAP,
'", id.', items-id.
ENDIF.
*** To Print Tree ***
FORM print_tree TABLES items STRUCTURE items.
DATA: v_tabix LIKE sy-tabix,
start_tabix LIKE sy-tabix,
v_level LIKE sy-tfill,
v_offset TYPE i,
v_id LIKE items-id,
v_parent_id LIKE items-parent_id,
v_parent_id_for_vline LIKE items-parent_id,
v_prev_level TYPE i,
v_items_count LIKE sy-tfill,
v_vlines_string(200).
CHECK NOT items[] IS INITIAL.
SORT items BY parent_id id.
READ TABLE items INDEX 1.
v_parent_id = items-parent_id.
start_tabix = 1.
REFRESH tabix_stack.
DO.
LOOP AT items FROM start_tabix.
v_tabix = start_tabix = sy-tabix. "to remember current index
v_id = items-id.
v_parent_id_for_vline = items-parent_id.
*** Decrease The Level & Exit The Loop If The Parent Is Not Same As The Previous ***
IF items-parent_id NE v_parent_id.
PERFORM read_from_stack CHANGING start_tabix. "level equals to number of records
READ TABLE items INDEX start_tabix.
v_parent_id = items-parent_id.
ADD 1 TO start_tabix. "next loop starts from parent index + 1
IF v_level > 1.
v_offset = 2 + ( v_level - 2 ) * 3.
IF v_level = 1. v_offset = 1. ENDIF.
v_vlines_string+v_offset = ' '.
ENDIF.
EXIT.
ENDIF.
v_parent_id = items-parent_id.
*** To Write Item ***
FORMAT COLOR OFF.
DESCRIBE TABLE tabix_stack LINES v_level. "level is no of Stack Records
WRITE: / v_vlines_string.
v_offset = v_level * 3.
IF v_level NE 0.
IF v_prev_level < v_level.
WRITE: AT v_offset '|', / ''.
WRITE: / v_vlines_string.
ENDIF.
v_offset = v_level * 3.
WRITE AT v_offset '|--'.
ENDIF.
v_offset = v_offset + 3.
CASE items-symbol.
WHEN '+'.
WRITE AT v_offset sym_plus_folder AS SYMBOL
COLOR 4 INTENSIFIED HOTSPOT.
WHEN '-'.
WRITE AT v_offset sym_minus_folder AS SYMBOL
COLOR 4 INTENSIFIED HOTSPOT.
WHEN OTHERS. FORMAT COLOR 5.
ENDCASE.
WRITE: items-text.
v_prev_level = v_level.
HIDE: items-id.
ADD 1 TO v_items_count.
READ TABLE items WITH KEY parent_id = items-id.
*** Increase The Level & Exit The Loop If The Item Has Children ***
IF sy-subrc = 0.
start_tabix = sy-tabix.
APPEND v_tabix TO tabix_stack. "level is no of records in stack
v_parent_id = items-parent_id.
v_tabix = v_tabix + 1.
READ TABLE items INDEX v_tabix.
v_offset = 2 + ( v_level - 1 ) * 3.
IF v_level > 0.
IF items-parent_id = v_parent_id_for_vline AND sy-subrc = 0.
v_vlines_string+v_offset = '|'.
ELSE.
v_vlines_string+v_offset = ' '.
ENDIF.
ENDIF.
EXIT.
ENDIF.
*** Decrease Level ***
AT LAST.
IF v_level > 1.
v_offset = 2 + ( v_level - 2 ) * 3.
IF v_level = 1. v_offset = 1. ENDIF.
v_vlines_string+v_offset = ' '.
ENDIF.
" next loop starts from parent index, not parent index + 1
" because of different parents level will decrease anyway
PERFORM read_from_stack CHANGING start_tabix.
APPEND start_tabix TO tabix_stack. "must return index to stack
ENDAT.
ENDLOOP.
DESCRIBE TABLE items.
IF start_tabix > sy-tfill OR v_items_count >= sy-tfill.
EXIT.
ENDIF.
ENDDO.
ENDFORM. "PRINT_TREE
*** Form For Expanding & Collapsing The Tree Nodes ***
FORM expand_collapse USING value(v_id).
DATA: v_no_more_orphans,
items_temp LIKE items OCCURS 100 WITH HEADER LINE.
DELETE items_show WHERE parent_id = v_id. "try to collapse
IF sy-subrc = 0. "first collapse sussessful
DO. "cascade collapse - delete 'orphans' that are left
REFRESH items_temp.
MOVE items_show[] TO items_temp[].
SORT items_temp BY id.
v_no_more_orphans = 'X'.
LOOP AT items_show WHERE parent_id NE ''.
READ TABLE items_temp WITH KEY id = items_show-parent_id
BINARY SEARCH TRANSPORTING NO FIELDS.
IF sy-subrc NE 0. "no parent - it's an orphan
CLEAR v_no_more_orphans.
DELETE items_show.
ENDIF.
ENDLOOP.
IF v_no_more_orphans = 'X'. EXIT. ENDIF.
ENDDO.
items_show-symbol = '+'.
MODIFY items_show TRANSPORTING symbol WHERE id = v_id.
ELSE. "unsuccessful collapse - expand
items_show-symbol = '-'.
MODIFY items_show TRANSPORTING symbol WHERE id = v_id.
LOOP AT items WHERE parent_id = v_id. "show children
APPEND items TO items_show.
ENDLOOP.
LOOP AT items_show WHERE parent_id = v_id. "check grandchildren
READ TABLE items WITH KEY parent_id = items_show-id.
IF sy-subrc = 0.
items_show-symbol = '+'.
ELSE.
items_show-symbol = ''.
ENDIF.
MODIFY items_show.
ENDLOOP.
ENDIF.
ENDFORM. "EXPAND_COLLAPSE
*&--------------------------------------------------------------------*
*& Form APPEND_ITEM
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
* -->VALUE(ID) text
* -->VALUE(PARENtext)
* -->VALUE(TEXT)text
*---------------------------------------------------------------------*
FORM append_item USING value(id) value(parent_id) value(text).
items-id = id.
items-parent_id = parent_id.
items-text = text.
APPEND items.
ENDFORM. "APPEND_ITEM
*&--------------------------------------------------------------------*
*& Form READ_FROM_STACK
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
* -->TABIX text
*---------------------------------------------------------------------*
FORM read_from_stack CHANGING tabix LIKE sy-tabix.
DESCRIBE TABLE tabix_stack.
CHECK sy-tfill NE 0.
READ TABLE tabix_stack INDEX sy-tfill.
tabix = tabix_stack.
DELETE tabix_stack INDEX sy-tfill.
ENDFORM. "READ_FROM_STACK
Thanks & Regards,
A.Uttam