OOPS ALV WITH TREE REPORT

*&---------------------------------------------------------------------*
*& Report  ZOOPS_ALV_TREE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT ZOOPS_ALV_TREE.

INCLUDE ZOOPS_ALV_TREE_TOP.

*&---------------------------------------------------------------------*
*&  Include           ZOOPS_ALV_TREE_TOP
*&---------------------------------------------------------------------*

DATACONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
      DISPLAY TYPE REF TO CL_GUI_ALV_TREE_SIMPLE.     " FOR TREE DISPLAY IN TREE FORMAT

DATAI_BSEG TYPE TABLE OF BSEG,
      W_BSEG TYPE BSEG.

DATAI_FCAT TYPE LVC_T_FCAT,
      W_FCAT LIKE LINE OF I_FCAT.         " FOR FIELD CATALOG.

DATAI_SORT TYPE LVC_T_SORT,
      W_SORT LIKE LINE OF I_SORT.         
" FOR INTERNAL TABLE,WORK AREA,OBJECTS DECLARATION




START-OF-SELECTION.
CALL SCREEN 500.        " CALL THE SCREEN AND CREATE A CONTAINER
*&---------------------------------------------------------------------*
*&      Module  STATUS_0500  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE status_0500 OUTPUT.
*  SET PF-STATUS 'xxxxxxxx'.
*  SET TITLEBAR 'xxx'.
PERFORM CREATE_OBJECTS.   " FOR OBJECTS CREATION
PERFORM GET_DATA.         " FETCHING THE DATA
PERFORM FCAT.             " FIELD CATALOG
PERFORM SORT.             " SORTING
PERFORM DISPLAY.          " DISPLAYING THE OUTPUT

ENDMODULE.                " STATUS_0500  OUTPUT
*&---------------------------------------------------------------------*
*&      Form  CREATE_OBJECTS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM create_objects .
CREATE OBJECT container
  EXPORTING
*    parent                      =
    container_name              'CONTAINER'
*    style                       =
*    lifetime                    = lifetime_default
*    repid                       =
*    dynnr                       =
*    no_autodef_progid_dynnr     =
*  EXCEPTIONS
*    cntl_error                  = 1
*    cntl_system_error           = 2
*    create_error                = 3
*    lifetime_error              = 4
*    lifetime_dynpro_dynpro_link = 5
*    others                      = 6
    .
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

CREATE OBJECT display
  EXPORTING
*    i_lifetime                  =
    i_parent                    CONTAINER
*    i_shellstyle                =
*    i_node_selection_mode       = cl_gui_column_Tree=>NODE_SEL_MODE_SIN
*    i_hide_selection            =
*    i_item_selection            = 'X'
*    i_no_toolbar                =
*    i_no_html_header            =
*    i_print                     =
*  EXCEPTIONS
*    cntl_error                  = 1
*    cntl_system_error           = 2
*    create_error                = 3
*    lifetime_error              = 4
*    illegal_node_selection_mode = 5
*    failed                      = 6
*    illegal_column_name         = 7
*    others                      = 8
    .
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.



ENDFORM.                    " CREATE_OBJECTS
*&---------------------------------------------------------------------*
*&      Form  GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_data .

  SELECT FROM BSEG
    INTO TABLE I_BSEG
    UP TO 300 ROWS.

ENDFORM.                    " GET_DATA
*&---------------------------------------------------------------------*
*&      Form  FCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM fcat .
  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
   EXPORTING
*     I_BUFFER_ACTIVE              =
     I_STRUCTURE_NAME             'BSEG'
*     I_CLIENT_NEVER_DISPLAY       = 'X'
*     I_BYPASSING_BUFFER           =
*     I_INTERNAL_TABNAME           =
    CHANGING
      ct_fieldcat                  I_FCAT
*   EXCEPTIONS
*     INCONSISTENT_INTERFACE       = 1
*     PROGRAM_ERROR                = 2
*     OTHERS                       = 3
            .
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.

  LOOP AT I_FCAT INTO W_FCAT.
 IF W_FCAT-FIELDNAME 'DMBTR' .      " DMBTR FIELD IS AMOUNT IN LOCAL CURRENCY
   W_FCAT-DO_SUM 'X'.               " CALUCATING GRAND TOTAL
   ELSEIF W_FCAT-FIELDNAME 'BUKRS'" COMPANY CODE IS THE FIELD
   ELSEIF W_FCAT-FIELDNAME 'BELNR'" ACCOUNTING DOCUMENT NUMBER IS THE FIELD
   ELSEIF W_FCAT-FIELDNAME 'GJAHR'" FISCAL YEAR
     W_FCAT-NO_OUT         'X'.     " DO NOT DISPLAY FIELD IN OUTPUT
     ENDIF.
MODIFY I_FCAT FROM W_FCAT INDEX SY-TABIX.
ENDLOOP.
ENDFORM.                    " FCAT
*&---------------------------------------------------------------------*
*&      Form  SORT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM sort .
CLEAR W_SORT.
W_SORT-SPOS '1'.          " POSITION
W_SORT-FIELDNAME 'BUKRS'" NAME OF THE FIELD
W_SORT-UP 'X'.            " SORTING IN ASCENDING
W_SORT-SUBTOT 'X'.        " CALCULATING SUB TOTAL
APPEND W_SORT TO I_SORT.

CLEAR W_SORT.
W_SORT-SPOS '2'.          " POSITION
W_SORT-FIELDNAME 'BELNR'" NAME OF THE FIELD
W_SORT-UP 'X'.            " SORTING IN ASCENDING
W_SORT-SUBTOT 'X'.        " CALCULATING SUB TOTAL
APPEND W_SORT TO I_SORT.

CLEAR W_SORT.
W_SORT-SPOS '3'.          " POSITION
W_SORT-FIELDNAME 'GJAHR'" NAME OF THE FIELD
W_SORT-UP 'X'.            " SORTING IN ASCENDING
W_SORT-SUBTOT 'X'.        " CALCULATING SUB TOTAL
APPEND W_SORT TO I_SORT.

ENDFORM.                    " SORT
*&---------------------------------------------------------------------*
*&      Form  DISPLAY
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM display .
  CALL METHOD display->set_table_for_first_display
*    EXPORTING
*      i_structure_name     =
*      is_variant           =
*      i_save               =
*      i_default            = 'X'
*      is_layout            =
*      it_special_groups    =
*      it_list_commentary   =
*      i_logo               =
*      i_background_id      =
*      it_toolbar_excluding =
*      it_grouplevel_layout =
*      it_except_qinfo      =
    CHANGING
      it_outtab            I_BSEG
      it_fieldcatalog      I_FCAT
*      it_filter            =
      it_sort              I_SORT
      .

  CALL METHOD display->expand_tree
    EXPORTING
      i_level                 '2'
*    EXCEPTIONS
*      failed                  = 1    "EXPANDING NODES
*      cntl_system_error       = 2
*      error_in_node_key_table = 3
*      dp_error                = 4
*      node_not_found          = 5
*      others                  = 6
          .
  IF sy-subrc <> 0.
*   Implement suitable error handling here
  ENDIF.



ENDFORM.                    " DISPLAY

***********************************************************************
HERE WE NEED TO USE CL_GUI_ALV_TREE_SIMPLE  CLASS FOR TREE .

FOR DISPLAYING WE CAN USE EXPAND TREE OR SET_TABLE_FOR_FIRST_DISPLAY METHOD

IF WE USE EXPAND_TREE THE NODES WILL OPEN AUTOMATICALLY ,
WITH SET_TABLE_FOR_FIRST_DISPLAY WONT OPEN NODE AUTOMATICALLY ,WE NEED TO DO MANUALLY.
***********************************************************************

OUTPUT: WITH EXPAND_TREE


WITH SET_TABLE_FOR_FIRST_DISPLAY