|
STEP ONE - ADD THESE LINES OF CODE IN THE DECLARATION AREA **** DECLARATION DATA: BEGIN OF items OCCURS 100, id type sy-tabix, parent_id type sy-tabix, text(1000), symbol, END OF items, tabix_stack LIKE sy-tabix OCCURS 10 WITH HEADER LINE, items_show LIKE items OCCURS 100 WITH HEADER LINE. DATA: parent_stack LIKE sy-tabix OCCURS 10 WITH HEADER LINE, t_parent LIKE sy-tabix, current LIKE sy-tabix. INCLUDE <symbol>. INCLUDE ZTREE_REPORT_INCLUDES.
STEP TWO - ADD THESE LINES OF CODE TO POPULATE THE INTERNAL TABLE **** POPULATE DATA REFRESH parent_stack. current = 0. parent_stack = 0. APPEND parent_stack. * itab_data is an internal table with one field of length 1000. * itab_data contains the output in this one field, as it must appear on the screen LOOP AT itab_data. CASE itab_data-qualf. WHEN 1. "New parent current = current + 1. PERFORM read_from_parent_stack CHANGING t_parent. parent_stack = current. APPEND parent_stack. PERFORM append_item USING current t_parent itab_xml_data. WHEN 2. "End of current leg current = current + 1. PERFORM append_item USING current t_parent itab_xml_data. perform delete_parent_stack. WHEN 3. "Same level current = current + 1. PERFORM read_from_parent_stack CHANGING t_parent. PERFORM append_item USING current t_parent itab_xml_data. ENDCASE. ENDLOOP. STEP THREE - ADD THESE LINES OF CODE TO PRINT THE REPORT LOOP AT items WHERE parent_id = 0. MOVE-CORRESPONDING items TO items_show. items_show-symbol = '+'. APPEND items_show. ENDLOOP. PERFORM print_tree TABLES items_show. STEP FOUR - ADD THESE LINES OF CODE TO EXPAND \ COLLAPSE THE TREE * at line-selection - when the node is opened/closed or item double-clk 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. STEP FIVE - ADD THIS INCLUDE CODE *----------------------------------------------------------------------* * INCLUDE ZTREE_REPORT_INCLUDES * *----------------------------------------------------------------------* * form append_item 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 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. * form 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. "remember current index v_id = items-id. v_parent_id_for_vline = items-parent_id. * decrease level and exit loop if parent not the same as previous IF items-parent_id NE v_parent_id. PERFORM read_from_stack CHANGING start_tabix. "level = NoOfRecs READ TABLE items INDEX start_tabix. v_parent_id = items-parent_id. ADD 1 TO start_tabix. "next loop starts from parent index + 1 * clear vline 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. * write item FORMAT COLOR OFF. DESCRIBE TABLE tabix_stack LINES v_level."level is no of StackRecs 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 level and exit loop if item has children IF sy-subrc = 0. start_tabix = sy-tabix. APPEND v_tabix TO tabix_stack. "level is no of recs in stack v_parent_id = items-parent_id. * set vline 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. * at last - decrease level AT LAST. * clear vline 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. * form expand_collapse 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. "succesfull first collapse 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. "unsuccessfull 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. * form read_from_parent_stack FORM read_from_parent_stack CHANGING tabix LIKE sy-tabix. DESCRIBE TABLE parent_stack. CHECK sy-tfill NE 0. READ TABLE parent_stack INDEX sy-tfill. tabix = parent_stack. ENDFORM. *& Form delete_parent_stack *&---------------------------------------------------------------------* form delete_parent_stack. DESCRIBE TABLE parent_stack. CHECK sy-tfill NE 0. DELETE parent_stack INDEX sy-tfill. endform. " delete_parent_stack |