General - Reporting Tree in ABAP

*
* An ABAP reporting tree program that allows the user to press enter or double click on 
* any line. The report is updated with new internal table data and re-written. The cursor 
* and paging returns to the exact same line the user was on, even though the report was 
* re-written from the internal table. Input fields are filled with data based on ENTER of 
* DOUBLE CLICK. [+] and [-] is displayed on each line and changes if user selects to 
* expand/collapse.
* 
* Create a gui status (8000) with Functions Keys
*                                 ENTER        = ENTE
*                                 BACK         = BACK
*                                 EXIT         = %EX
*                                 CANCEL       = RW
*                                 PICK         = F2
*                                 EXPAND ALL   = EXPA
*                                 COLLAPSE ALL = COLA
*
* Written by : SAP Basis, ABAP Programming and Other IMG Stuff
*              http://www.erpgreat.com
*
REPORT ZTREE NO STANDARD PAGE HEADING
             LINE-COUNT 65
             LINE-SIZE 80.

DATA: BEGIN OF IT_DATA OCCURS 0,
        EXP(1) TYPE C,
        WERKS LIKE T001W-WERKS,
        MATNR LIKE MARA-MATNR,
        VALUE1(13) TYPE C,
        VALUE2(13) TYPE C,
      END OF IT_DATA.

DATA: W_MATNR LIKE MARA-MATNR,
      W_COUNT TYPE I,
      G_FIELD(40) TYPE C.

DATA: BEGIN OF PAGE,
        SCRLLINE LIKE SYST-LILLI,      "scroll line to top of screen
        CURSLINE LIKE SYST-LILLI,      "line to put cursor on
        HDRLINES TYPE I,               "lines in header + 1
        TWOHDRS TYPE I,                "two headers on screen
        CPOSTOP TYPE I,                "position of cursor from top
        CPOSBOT TYPE I,                "position of cursor from bottom
        CPOSIT TYPE I,                 "position on the screen
        PAGNO TYPE I,                  "pageno of line
        PGELINES TYPE I,               "lines used so far
      END OF PAGE.

START-OF-SELECTION.
  PERFORM BUILD_IT_DATA.

END-OF-SELECTION.
  PERFORM WRITE_REPORT.

TOP-OF-PAGE.
  PERFORM TOP_OF_PAGE.

TOP-OF-PAGE DURING LINE-SELECTION.
  PERFORM TOP_OF_PAGE.

AT LINE-SELECTION.
  G_FIELD = SPACE.
  GET CURSOR FIELD G_FIELD.
  PERFORM LINE_SELECTION.

AT USER-COMMAND.
  G_FIELD = SPACE.
  GET CURSOR FIELD G_FIELD.
  PERFORM USER_COMMAND.

*---------------------------------------------------------------------*
*       FORM BUILD_IT_DATA                                            *
*---------------------------------------------------------------------*
FORM BUILD_IT_DATA.
  IT_DATA-WERKS = '0000'.
  WHILE IT_DATA-WERKS < '0100'.
    ADD '10' TO IT_DATA-WERKS.
    W_MATNR = 0.
    WHILE W_MATNR < 10.
      ADD 1 TO W_MATNR.
      IT_DATA-EXP = '+'.
      WRITE W_MATNR TO IT_DATA-MATNR.
      IF IT_DATA-WERKS = 10 AND W_MATNR = 1.
            WRITE 'ZTEST2' TO IT_DATA-VALUE1.
            WRITE 'Testing program 2' TO IT_DATA-VALUE2.
      ELSE.
            CLEAR: IT_DATA-VALUE1, IT_DATA-VALUE2.
      ENDIF.
      APPEND IT_DATA.
    ENDWHILE.
  ENDWHILE.
ENDFORM.
*---------------------------------------------------------------------*
*       FORM WRITE_REPORT                                             *
*---------------------------------------------------------------------*
FORM WRITE_REPORT.
  SET PF-STATUS '8000'.
  SYST-LSIND = '0'.
  LOOP AT IT_DATA.
    AT NEW WERKS.
      FORMAT COLOR COL_HEADING INTENSIFIED OFF.
      WRITE: /1 SYST-VLINE NO-GAP,
                '[' NO-GAP,
                IT_DATA-EXP NO-GAP HOTSPOT ON,
                ']'.
      FORMAT COLOR COL_NORMAL INTENSIFIED ON.
      WRITE:     IT_DATA-WERKS,
              AT SYST-LINSZ SYST-VLINE.
      PAGE-PAGNO = SYST-PAGNO.
      HIDE: IT_DATA-WERKS, PAGE-PAGNO.
    ENDAT.
    IF IT_DATA-EXP = '-'.
      FORMAT COLOR COL_NORMAL INTENSIFIED OFF.
      WRITE: /1 SYST-VLINE,
              8 IT_DATA-MATNR,
                IT_DATA-VALUE1 INPUT ON,
                IT_DATA-VALUE2 INPUT ON,
             AT SYST-LINSZ SYST-VLINE.
      HIDE: IT_DATA, PAGE-PAGNO.
    ENDIF.
  ENDLOOP.
  ULINE.
ENDFORM.
*---------------------------------------------------------------------*
*       FORM USER_COMMAND                                             *
*---------------------------------------------------------------------*
FORM USER_COMMAND.
  PERFORM SET_SCROLL_LINE.
  CASE SY-UCOMM.
    WHEN 'ENTE'.
      READ TABLE IT_DATA WITH KEY WERKS = IT_DATA-WERKS
                                  MATNR = IT_DATA-MATNR.
      IF SYST-SUBRC = 0.
        CASE G_FIELD.
          WHEN 'IT_DATA-VALUE1'.
            IF IT_DATA-VALUE1 <> SPACE.
               SUBMIT (IT_DATA-VALUE1) VIA SELECTION-SCREEN AND RETURN.
            ELSE.
               IT_DATA-VALUE1 = 'Enter 1 Press'(001).
            ENDIF.
          WHEN 'IT_DATA-VALUE2'.
            IT_DATA-VALUE2 = 'Enter 2 Press'(001).
        ENDCASE.
        MODIFY IT_DATA INDEX SYST-TABIX.
      ENDIF.
      PERFORM WRITE_REPORT.
      PERFORM POSITION_LIST.
      CASE G_FIELD.
        WHEN 'IT_DATA-EXP'.
          ADD 1 TO PAGE-CURSLINE.
          SET CURSOR FIELD 'IT_DATA-VALUE1' LINE PAGE-CURSLINE.
        WHEN 'IT_DATA-VALUE1'.
          SET CURSOR FIELD 'IT_DATA-VALUE1' LINE PAGE-CURSLINE.
        WHEN 'IT_DATA-VALUE2'.
          SET CURSOR FIELD 'IT_DATA-VALUE2' LINE PAGE-CURSLINE.
      ENDCASE.
    WHEN 'EXPA'.
      PERFORM SET_EXPAND USING '-'.
      PERFORM WRITE_REPORT.
    WHEN 'COLA'.
      PERFORM SET_EXPAND USING '+'.
      PERFORM WRITE_REPORT.
  ENDCASE.
ENDFORM.
*---------------------------------------------------------------------*
*       FORM SET_EXPAND                                               *
*---------------------------------------------------------------------*
FORM SET_EXPAND USING W_EXP.
  LOOP AT IT_DATA.
    IT_DATA-EXP = W_EXP.
    MODIFY IT_DATA INDEX SYST-TABIX.
  ENDLOOP.
ENDFORM.
*---------------------------------------------------------------------*
*       FORM LINE_SELECTION                                           *
*---------------------------------------------------------------------*
FORM LINE_SELECTION.
  PERFORM SET_SCROLL_LINE.
  READ TABLE IT_DATA WITH KEY WERKS = IT_DATA-WERKS
                              MATNR = IT_DATA-MATNR.
  CASE G_FIELD.
    WHEN 'IT_DATA-EXP'.
      LOOP AT IT_DATA WHERE WERKS = IT_DATA-WERKS.
        IF IT_DATA-EXP = '-'.
          IT_DATA-EXP = '+'.
        ELSE.
          IT_DATA-EXP = '-'.
        ENDIF.
        MODIFY IT_DATA INDEX SYST-TABIX.
      ENDLOOP.
    WHEN 'IT_DATA-VALUE1'.
      IT_DATA-VALUE1 = 'Double Click'(004).
      MODIFY IT_DATA INDEX SYST-TABIX.
    WHEN 'IT_DATA-VALUE2'.
      IT_DATA-VALUE2 = 'Double Click'(004).
      MODIFY IT_DATA INDEX SYST-TABIX.
  ENDCASE.
  PERFORM WRITE_REPORT.
  PERFORM POSITION_LIST.
  CASE G_FIELD.
    WHEN 'IT_DATA-EXP'.
      ADD 1 TO PAGE-CURSLINE.
      SET CURSOR FIELD 'IT_DATA-VALUE1' LINE PAGE-CURSLINE.
    WHEN 'IT_DATA-VALUE1'.
      SET CURSOR FIELD 'IT_DATA-VALUE1' LINE PAGE-CURSLINE.
    WHEN 'IT_DATA-VALUE2'.
      SET CURSOR FIELD 'IT_DATA-VALUE2' LINE PAGE-CURSLINE.
  ENDCASE.
ENDFORM.
*---------------------------------------------------------------------*
*       FORM SET_SCROLL_LINE                                          *
* determine the correct line to scroll the re-written report to, so   *
* that the user is returned to the same spot on the screen            *
* if user is mid way between pages, ie two headers are visable on the *
* screen, check if they are on the secondary page and adjust the      *
* scroll variable accordingly so as not to jump up lines because the  *
* scroll variable will be out by the number of lines in the extra hdr *
*---------------------------------------------------------------------*
FORM SET_SCROLL_LINE.
  PAGE-CURSLINE = SYST-LILLI.
  PAGE-SCRLLINE = SYST-LILLI - ( SYST-CUROW - PAGE-HDRLINES ).
  PAGE-PGELINES = ( PAGE-PAGNO - 1 ) * SYST-LINCT.
  PAGE-CPOSTOP = ( PAGE-SCRLLINE + SYST-CUROW ) - PAGE-PGELINES.
  PAGE-CPOSBOT = ( PAGE-SCRLLINE - SYST-CUROW ) - PAGE-PGELINES.
  PAGE-CPOSIT = PAGE-CPOSTOP + PAGE-CPOSBOT.
  PAGE-TWOHDRS = PAGE-HDRLINES * 2.
  IF PAGE-CPOSIT LT PAGE-TWOHDRS.
    PAGE-SCRLLINE = PAGE-SCRLLINE + 1.
  ENDIF.
ENDFORM.
*---------------------------------------------------------------------*
*       FORM POSITION_LIST                                            *
*---------------------------------------------------------------------*
FORM POSITION_LIST.
  CALL FUNCTION 'LIST_SCROLL_LINE_TOPMOST'
       EXPORTING
            LIST_INDEX = 1
            LIST_LINE  = PAGE-SCRLLINE
       EXCEPTIONS
            OTHERS     = 1.
  CHECK SYST-SUBRC = 0.
ENDFORM.
*---------------------------------------------------------------------*
*       FORM TOP_OF_PAGE                                              *
*---------------------------------------------------------------------*
FORM TOP_OF_PAGE.
* set page-hdrlines = lines in header + 1 system generated blank line
  PAGE-HDRLINES = 5.
  ULINE.
  WRITE : /1 SYST-VLINE, 'Header'(002), AT SYST-LINSZ SYST-VLINE.
  WRITE : /1 SYST-VLINE, 'Lines'(003), AT SYST-LINSZ SYST-VLINE.
  ULINE.
ENDFORM.

Fast Links:
Example on List Box
List Box in ABAP Report

SAP Books
SAP Certification, Functional, Basis Administration and ABAP Programming Reference Books

ABAP Tips
ABAP and Samples Program Codes for Abapers

Best regards,
SAP Basis, ABAP Programming and Other IMG Stuff
http://www.erpgreat.com

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.  The site www.erpgreat.com is in no way affiliated with SAP AG. 
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk. 
 The content on this site may not be reproduced or redistributed without the express written permission of 
www.erpgreat.com or the content authors.