Final code for the PBO(..O01) and FORM(..F01) includes

The following code allows the easy creation of the example ALVtree report. Simply copy and past it into the appropriate includes(PBO and FORM).


*--------------------------------------------------------------*
***INCLUDE ZDEMO_ALVTREEO01 .
*--------------------------------------------------------------*
*&-------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&-------------------------------------------------------------*
*       text
*--------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  SET PF-STATUS 'STATUS1'.
*  Set titlebar 'xxx'.
* If ALVtree already exists then it  mush not be re-created as this
* will cause a runtime error.
  IF gd_tree IS INITIAL.
    PERFORM create_alvtree_container.
    PERFORM create_object_in_container.
    PERFORM define_events.
    PERFORM create_alvtree_hierarchy.
  ENDIF.
  CALL METHOD cl_gui_cfw=>flush.
ENDMODULE.                 " STATUS_0100  OUTPUT


*--------------------------------------------------------------*
*   INCLUDE ZDEMO_ALVTREEF01                                   *
*--------------------------------------------------------------*
*&-------------------------------------------------------------*
*&      Form  CREATE_ALVTREE_CONTAINER
*&-------------------------------------------------------------*
*       Create container for ALVtree report
*--------------------------------------------------------------*
form create_alvtree_container.
* Create container for alv-tree
  gd_tree_container_name = 'SCREEN_CONTAINER'.
  create object gd_custom_container
      exporting
            container_name = gd_tree_container_name
      exceptions
            cntl_error                  = 1
            cntl_system_error           = 2
            create_error                = 3
            lifetime_error              = 4
            lifetime_dynpro_dynpro_link = 5.
  if sy-subrc <> 0.
    message x208(00) with 'ERROR'.
  endif.
endform.                    " CREATE_ALVTREE_CONTAINER
*&---------------------------------------------------------------------*
*&      Form  create_object_in_container
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM create_object_in_container .
  CREATE OBJECT gd_tree
    EXPORTING
        parent              = gd_custom_container
        node_selection_mode = cl_gui_simple_tree=>node_sel_mode_single
    EXCEPTIONS
        cntl_system_error            = 2
        create_error                 = 3
        lifetime_error               = 4
        illegal_node_selection_mode  = 5
        failed                       = 6.
  IF sy-subrc <> 0.
    MESSAGE x208(00) WITH 'ERROR'.
  ENDIF.
ENDFORM.                    " create_object_in_container
*&---------------------------------------------------------------------*
*&      Form  create_alvtree_hierarchy
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM create_alvtree_hierarchy .
  DATA: NODE LIKE MTREESNODE,
        ld_node type string.
  LOOP AT it_ekko INTO wa_ekko.
* Build the node table.
* Caution: The nodes are inserted into the tree according to the order
* in which they occur in the table. In consequence, a node must not
* occur in the node table before its parent node.
*   Node with key 'Root'
    node-node_key = wa_ekko-ebeln. " 'Root'.   "#EC NOTEXT
    " Key of the node
    CLEAR node-relatkey.      " Special case: A root node has no parent
    CLEAR node-relatship.     " node.
    node-hidden = ' '.        " The node is visible,
    node-disabled = ' '.      " selectable,
    node-isfolder = 'X'.      " a folder.
    CLEAR node-n_image.       " Folder-/ Leaf-Symbol in state "closed":
    " use default.
    CLEAR node-exp_image.     " Folder-/ Leaf-Symbol in state "open":
    " use default
    CLEAR node-expander.      " see below.
    node-text = wa_ekko-ebeln.      "'Root'.
    APPEND node TO node_table.
    LOOP AT it_ekpo INTO wa_ekpo WHERE ebeln EQ wa_ekko-ebeln.
*     Node with key wa_ekpo-ebelp    "'Child1'
      ld_node = sy-tabix.
      concatenate wa_ekpo-ebelp ld_node into ld_node.
      node-node_key = ld_node.  "'Child1'. #EC NOTEXT
*     Node is inserted as child of the node with key wa_ekpo-ebeln  "'Root'.
      node-relatkey = wa_ekpo-ebeln.  " 'Root'.
      node-relatship = cl_gui_simple_tree=>relat_last_child.
      node-hidden = ' '.
      node-disabled = ' '.
      node-isfolder = ' '.
      CLEAR node-n_image.
      CLEAR node-exp_image.
      node-expander = ' '.  " The node is marked with a '+', although
*                        it has no children. When the user clicks on the
*                        + to open the node, the event
*                        expand_no_children is fired. The programmer can
*                        add the children of the
*                        node within the event handler of the
*                        expand_no_children event
*                        (see method handle_expand_no_children
*                        of class lcl_application)
      node-text = wa_ekpo-ebelp.
      APPEND node TO node_table.
    ENDLOOP.
  ENDLOOP.
* Add nodes to alv tree
  CALL METHOD GD_TREE->ADD_NODES
    EXPORTING
      TABLE_STRUCTURE_NAME = 'MTREESNODE'
      NODE_TABLE           = NODE_TABLE
    EXCEPTIONS
      FAILED                         = 1
      ERROR_IN_NODE_TABLE            = 2
      DP_ERROR                       = 3
      TABLE_STRUCTURE_NAME_NOT_FOUND = 4
      OTHERS                         = 5.
ENDFORM.                    " create_alvtree_hierarchy
*&---------------------------------------------------------------------*
*&      Form  define_events
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form define_events .
  data: EVENTS TYPE CNTL_SIMPLE_EVENTS,
        event type cntl_simple_event.
* define the events which will be passed to the backend
  " node double click
  event-eventid = CL_GUI_SIMPLE_TREE=>EVENTID_NODE_DOUBLE_CLICK.
  event-appl_event = 'X'. " process PAI if event occurs
  APPEND event to events.
  " expand no children
  event-eventid = CL_GUI_SIMPLE_TREE=>EVENTID_EXPAND_NO_CHILDREN.
  event-appl_event = 'X'.
  APPEND event to events.
  CALL METHOD GD_TREE->SET_REGISTERED_EVENTS
    EXPORTING
      EVENTS = EVENTS
    EXCEPTIONS
      CNTL_ERROR                = 1
      CNTL_SYSTEM_ERROR         = 2
      ILLEGAL_EVENT_COMBINATION = 3.
* assign event handlers in the application class to each desired event
  SET HANDLER GD_APPLICATION->HANDLE_NODE_DOUBLE_CLICK FOR GD_TREE.
  SET HANDLER GD_APPLICATION->HANDLE_EXPAND_NO_CHILDREN FOR GD_TREE.
endform.                    " define_events

Related Articles

Activate user interaction within SAP ALV tree
Add button to SAP ALV tree toolbar using ABAP code
ABAP ALV Tree tutorial to create a fully working ALVTree Control SAP dynpro program
Create ALV Tree Custom control and add to sap dynpro screen
Final ABAP code for the ALV Tree PBO(..O01) and FORM(..F01) includes
Create SAP ALV Tree Container in ABAP dynpro screen container
Create Container and Object
Create ALV tree main program including Data & Event declaration
SAP ALV Tree fieldcatalog creation
Set SAP ALV tree table for first display and user interaction
Build SAP ALV tree Hierarchy
Build SAP ALV tree Hierarchy Header
Add alv tree already exists check to pbo module
Create all includes required for the ALV tree report
Full ABAP code listing of main ALV tree program
Module creation screen
Create SAP ALV Tree object within ABAP dynpro screen container/H1>
Create ok code to store ALV tree user interaction
Example Output from ALV tree display
Create screen control
Create Screen along with PBO and PAI modules
Create Pf-status(screen menu functionality)
Refresh ALVtree
Build Report Header(Title)
ALVtree toolbar processing
Implement user defined buttons to ALVtree toolbar
ALVtree user interaction processing
Implement ALVtree user interaction processing
Setup ALVtree variant
Main Steps to create ALV simple Tree(Using Objects)
Create Add Custom control to screen
Create Container and Object
Create and register events for user interaction
Create ALVtree Hierarchy
Add already exists check!
Create Includes for ALVtree report
Full ABAP code listing of main program
Create Container and Object
Create screen control
Example Output from ALV simple tree display
Create screen control
Create Screen along with PBO and PAI modules
Create Pf-status(screen menu functionality)