Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

This is a example about ALV Tree using  OO ABAP. i used the class cl_gui_alv_tree. Look all code:

Error rendering macro 'code': Invalid value specified for parameter 'lang'
 *& REPORT  Z_ALV_TREE                                                  *
****This program is a example about ALV Tree using OO ABAP
REPORT  Z_ALV_TREE                    .
****TYPES
TYPE-POOLS: SLIS.
****INCLUDE
INCLUDE <ICON>.
INCLUDE BCALV_TREE_EVENT_RECEIVER.
****CLASS
CLASS CL_GUI_COLUMN_TREE DEFINITION LOAD.
CLASS CL_GUI_CFW DEFINITION LOAD.
****TABLES
TYPES:  BEGIN OF IT ,
      NODE_MAIN(15) TYPE C,
      NODE_N01(15) TYPE C,
      NODE_N02(15) TYPE C,
      NODE_N03(15) TYPE C,
      END OF IT.
DATA: IT_EXIT TYPE IT OCCURS 0,
      IT_EXIT1 TYPE IT OCCURS 0,
      WA_EXIT1 LIKE LINE OF IT_EXIT,
      WA_EXIT LIKE LINE OF IT_EXIT.
****STRUCTURE FOR ALV TREE
DATA:
LT_LIST_COMMENTARY TYPE SLIS_T_LISTHEADER, """COMENTARY
L_LOGO TYPE SDYDO_VALUE,                   """LOGO
LS_VARIANT TYPE DISVARIANT,                """VARIANT
L_HIERARCHY_HEADER TYPE TREEV_HHDR,        """HEADER
TREE1  TYPE REF TO CL_GUI_ALV_TREE,        """TREE
GT_FIELDCATALOG TYPE LVC_T_FCAT,           """CATALOG
OK_CODE TYPE SY-UCOMM.                     """OK-CODE
****INITIALIZATION
INITIALIZATION.
*  CLEAR: it_exit, wa_exit.
*****START-OF-SELECTION
START-OF-SELECTION.
**** MAIN NODE
  CLEAR:IT_EXIT, WA_EXIT.
  BREAK P33GGOTERA.
  MOVE  'Main Node'  TO WA_EXIT-NODE_MAIN.
  APPEND WA_EXIT TO IT_EXIT.
  CLEAR WA_EXIT.
**** FIRST NODE
  MOVE : 'First Level'  TO WA_EXIT-NODE_N01,
         'Main Node'  TO WA_EXIT-NODE_MAIN.
  APPEND WA_EXIT TO IT_EXIT.
  CLEAR WA_EXIT.
  MOVE : 'First Level A'  TO WA_EXIT-NODE_N02,
         'Main Node'  TO WA_EXIT-NODE_MAIN.
  APPEND WA_EXIT TO IT_EXIT.
  CLEAR WA_EXIT.
  MOVE : 'First Level B'  TO WA_EXIT-NODE_N02,
         'Main Node'  TO WA_EXIT-NODE_MAIN.
  APPEND WA_EXIT TO IT_EXIT.
  CLEAR WA_EXIT.
**** SECOND NODE
  MOVE:  'Second Level'  TO WA_EXIT-NODE_N01,
         'Main Node'  TO WA_EXIT-NODE_MAIN.
  APPEND WA_EXIT TO IT_EXIT.
  CLEAR WA_EXIT.
  MOVE:  'Second Level A'  TO WA_EXIT-NODE_N02,
         'Main Node'  TO WA_EXIT-NODE_MAIN.
  APPEND WA_EXIT TO IT_EXIT.
  CLEAR WA_EXIT.
  MOVE:  'Second Level B'  TO WA_EXIT-NODE_N03,
         'Main Node'  TO WA_EXIT-NODE_MAIN.
  APPEND WA_EXIT TO IT_EXIT.
  CLEAR WA_EXIT.
**** THIRD NODE
  MOVE:  'Third Level'  TO WA_EXIT-NODE_N01,
         'Main Node'  TO WA_EXIT-NODE_MAIN.
  APPEND WA_EXIT TO IT_EXIT.
  CLEAR WA_EXIT.
****SCREEN
  CALL SCREEN 100.
****
*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.
  SET PF-STATUS 'ALVTREE'.
  IF TREE1 IS INITIAL.
* CATALOG
    PERFORM FO_FIELDCAT.
* CREATE TREE
    PERFORM CREATE_TREE.
* create Hierarchy-header
    PERFORM BUILD_HIERARCHY_HEADER CHANGING L_HIERARCHY_HEADER.
* create info-table for html-header
    PERFORM BUILD_COMMENT USING
                   LT_LIST_COMMENTARY.
* repid for saving variants
    LS_VARIANT-REPORT = SY-REPID.
* Creation of ALV
    CALL METHOD TREE1->SET_TABLE_FOR_FIRST_DISPLAY
      EXPORTING
        IS_HIERARCHY_HEADER = L_HIERARCHY_HEADER
        IT_LIST_COMMENTARY  = LT_LIST_COMMENTARY
        I_SAVE              = 'A'
        IS_VARIANT          = LS_VARIANT
      CHANGING
        IT_OUTTAB           = IT_EXIT1
        IT_FIELDCATALOG     = GT_FIELDCATALOG.
* create hierarchy
    PERFORM CREATE_HIERARCHY.
* register events
    PERFORM REGISTER_EVENTS.
  ENDIF.
  CALL METHOD CL_GUI_CFW=>FLUSH.
ENDMODULE.                 " STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
  CLEAR OK_CODE.
  OK_CODE = SY-UCOMM.
  CASE OK_CODE.
    WHEN 'EXIT' OR 'BACK' .
      PERFORM EXIT_PROGRAM.
    WHEN 'BACK1'.
      CALL METHOD TREE1->FREE.
      LEAVE TO SCREEN 0.
    WHEN OTHERS.
      CALL METHOD CL_GUI_CFW=>DISPATCH.
  ENDCASE.
  CLEAR OK_CODE.
  CALL METHOD CL_GUI_CFW=>FLUSH.
ENDMODULE.                 " USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*&      Form  exit_program
*&---------------------------------------------------------------------*
FORM EXIT_PROGRAM.
  CALL METHOD TREE1->FREE.
  LEAVE PROGRAM.
ENDFORM.                    " exit_program
*&---------------------------------------------------------------------*
*&      Form  fo_fieldcat
*&---------------------------------------------------------------------*
FORM FO_FIELDCAT.
  DATA: LS_FIELDCATALOG TYPE LVC_S_FCAT.
****MAIN NODE
  LS_FIELDCATALOG-COL_POS = '1'.
  LS_FIELDCATALOG-FIELDNAME = 'NODE_MAIN'.
  LS_FIELDCATALOG-TABNAME = 'IT_EXIT'.
  LS_FIELDCATALOG-COLTEXT  = 'Main Node'.
  LS_FIELDCATALOG-KEY = 'X'.
  LS_FIELDCATALOG-JUST = 'L'.
  LS_FIELDCATALOG-NO_OUT = 'X'.
  LS_FIELDCATALOG-OUTPUTLEN = '15'.
  APPEND  LS_FIELDCATALOG TO GT_FIELDCATALOG.
  CLEAR LS_FIELDCATALOG.
****FIRST LEVEL
  LS_FIELDCATALOG-COL_POS = '2'.
  LS_FIELDCATALOG-FIELDNAME = 'NODE_N01'.
  LS_FIELDCATALOG-TABNAME = 'IT_EXIT'.
  LS_FIELDCATALOG-COLTEXT  = 'First Level'.
*  ls_fieldcatalog-key = 'X'.
  LS_FIELDCATALOG-JUST = 'L'.
*  ls_fieldcatalog-no_out = 'X'.
  LS_FIELDCATALOG-OUTPUTLEN = '15'.
  APPEND  LS_FIELDCATALOG TO GT_FIELDCATALOG.
  CLEAR LS_FIELDCATALOG.
****SECOND LEVEL
  LS_FIELDCATALOG-COL_POS = '3'.
  LS_FIELDCATALOG-FIELDNAME = 'NODE_N02'.
  LS_FIELDCATALOG-TABNAME = 'IT_EXIT'.
  LS_FIELDCATALOG-COLTEXT  = 'Second Level'.
*  ls_fieldcatalog-key = 'X'.
  LS_FIELDCATALOG-JUST = 'L'.
*  ls_fieldcatalog-no_out = 'X'.
  LS_FIELDCATALOG-OUTPUTLEN = '15'.
  APPEND  LS_FIELDCATALOG TO GT_FIELDCATALOG.
  CLEAR LS_FIELDCATALOG.
****THIRD LEVEL
  LS_FIELDCATALOG-COL_POS = '4'.
  LS_FIELDCATALOG-FIELDNAME = 'NODE_N03'.
  LS_FIELDCATALOG-TABNAME = 'IT_EXIT'.
  LS_FIELDCATALOG-COLTEXT  = 'Third Level'.
*  ls_fieldcatalog-key = 'X'.
  LS_FIELDCATALOG-JUST = 'L'.
*  ls_fieldcatalog-no_out = 'X'.
  LS_FIELDCATALOG-OUTPUTLEN = '15'.
  APPEND  LS_FIELDCATALOG TO GT_FIELDCATALOG.
  CLEAR LS_FIELDCATALOG.
ENDFORM.                    " fo_fieldcat
*&---------------------------------------------------------------------*
*&      Form  create_tree
*&---------------------------------------------------------------------*
FORM CREATE_TREE.
* create container for alv-tree
  DATA: L_TREE_CONTAINER_NAME(30) TYPE C,
        L_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER.
  L_TREE_CONTAINER_NAME = 'TREE1'.
  IF SY-BATCH IS INITIAL.
    CREATE OBJECT L_CUSTOM_CONTAINER
      EXPORTING
            CONTAINER_NAME = L_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.
  ENDIF.
* create tree control
  CREATE OBJECT TREE1
    EXPORTING
        PARENT              = L_CUSTOM_CONTAINER
        NODE_SELECTION_MODE = CL_GUI_COLUMN_TREE=>NODE_SEL_MODE_SINGLE
        ITEM_SELECTION      = 'X'
        NO_HTML_HEADER      = ''
        NO_TOOLBAR          = ''
    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.
  IF SY-SUBRC <> 0.
    MESSAGE X208(00) WITH 'ERROR'.                          "#EC NOTEXT
  ENDIF.
****
ENDFORM.                    " create_tree
*&---------------------------------------------------------------------*
*&      Form  build_hierarchy_header
*&---------------------------------------------------------------------*
FORM BUILD_HIERARCHY_HEADER CHANGING
                               P_HIERARCHY_HEADER TYPE TREEV_HHDR.
  P_HIERARCHY_HEADER-HEADING = 'Node'.
  P_HIERARCHY_HEADER-WIDTH = 15.
  P_HIERARCHY_HEADER-WIDTH_PIX = ''.
ENDFORM.                    " build_hierarchy_header
*&---------------------------------------------------------------------*
*&      Form  build_comment
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_LT_LIST_COMMENTARY  text
*----------------------------------------------------------------------*
FORM BUILD_COMMENT USING
      PT_LIST_COMMENTARY TYPE SLIS_T_LISTHEADER.
  DATA: LS_LINE TYPE SLIS_LISTHEADER, TEXT(60) TYPE C.
*
* LIST HEADING LINE: TYPE H
  CLEAR LS_LINE.
  LS_LINE-TYP  = 'H'.
* LS_LINE-KEY:  NOT USED FOR THIS TYPE
  LS_LINE-INFO = SY-TITLE.
  APPEND LS_LINE TO PT_LIST_COMMENTARY.
* STATUS LINE: TYPE S
  CLEAR LS_LINE.
  LS_LINE-KEY = 'Date:'.
  WRITE SY-DATUM TO LS_LINE-INFO USING EDIT MASK '__/__/____'.
  LS_LINE-TYP  = 'S'.
  APPEND LS_LINE TO PT_LIST_COMMENTARY.
*
  CLEAR LS_LINE.
  LS_LINE-KEY = 'User:'.
  LS_LINE-INFO = SY-UNAME.
  LS_LINE-TYP  = 'S'.
  APPEND LS_LINE TO PT_LIST_COMMENTARY.
ENDFORM.                    " build_comment
*&---------------------------------------------------------------------*
*&      Form  create_hierarchy
*&---------------------------------------------------------------------*
FORM CREATE_HIERARCHY.
* add data to tree
  DATA: L_KEY_MAIN TYPE LVC_NKEY,
        L_KEY_NODE1 TYPE LVC_NKEY,
        L_KEY_NODE2 TYPE LVC_NKEY,
        L_KEY_NODE3 TYPE LVC_NKEY,
        L_NODE TYPE LVC_VALUE.
*****
  LOOP AT IT_EXIT INTO WA_EXIT.
*****main node
    MOVE-CORRESPONDING WA_EXIT TO WA_EXIT1.
    AT NEW NODE_MAIN.
      CLEAR L_NODE.
      L_NODE = WA_EXIT1-NODE_MAIN.
      PERFORM ADD_NODE_MAIN USING WA_EXIT1
                              ''
                            L_NODE
                        CHANGING L_KEY_MAIN.
    ENDAT.
****node 1
    MOVE-CORRESPONDING WA_EXIT1 TO WA_EXIT.
    IF NOT WA_EXIT-NODE_N01 IS INITIAL.
      CLEAR L_NODE.
      L_NODE = WA_EXIT-NODE_N01.
      PERFORM ADD_NODE USING  WA_EXIT
                              L_KEY_MAIN
                              L_NODE
                        CHANGING L_KEY_NODE1.
    ENDIF.
****node 2
    IF NOT WA_EXIT-NODE_N02 IS INITIAL.
      CLEAR L_NODE.
      L_NODE = WA_EXIT-NODE_N02.
      PERFORM ADD_NODE USING WA_EXIT
                              L_KEY_NODE1
                              L_NODE
                        CHANGING L_KEY_NODE2.
    ENDIF.
****node 3
    IF NOT WA_EXIT-NODE_N03 IS INITIAL.
      CLEAR L_NODE.
      L_NODE = WA_EXIT-NODE_N03.
      PERFORM ADD_NODE USING WA_EXIT
                              L_KEY_NODE2
                              L_NODE
                        CHANGING L_KEY_NODE3.
    ENDIF.
  ENDLOOP.
* this method must be called to send the data to the frontend
  CALL METHOD TREE1->FRONTEND_UPDATE.
*****
ENDFORM.                    " create_hierarchy
*&---------------------------------------------------------------------*
*&      Form  register_events
*&---------------------------------------------------------------------*
FORM REGISTER_EVENTS.
* define the events which will be passed to the backend
  DATA: LT_EVENTS TYPE CNTL_SIMPLE_EVENTS,
        L_EVENT TYPE CNTL_SIMPLE_EVENT.
* define the events which will be passed to the backend
  L_EVENT-EVENTID = CL_GUI_COLUMN_TREE=>EVENTID_EXPAND_NO_CHILDREN.
  APPEND L_EVENT TO LT_EVENTS.
  L_EVENT-EVENTID = CL_GUI_COLUMN_TREE=>EVENTID_CHECKBOX_CHANGE.
  APPEND L_EVENT TO LT_EVENTS.
  L_EVENT-EVENTID = CL_GUI_COLUMN_TREE=>EVENTID_HEADER_CONTEXT_MEN_REQ.
  APPEND L_EVENT TO LT_EVENTS.
  L_EVENT-EVENTID = CL_GUI_COLUMN_TREE=>EVENTID_NODE_CONTEXT_MENU_REQ.
  APPEND L_EVENT TO LT_EVENTS.
  L_EVENT-EVENTID = CL_GUI_COLUMN_TREE=>EVENTID_ITEM_CONTEXT_MENU_REQ.
  APPEND L_EVENT TO LT_EVENTS.
  L_EVENT-EVENTID = CL_GUI_COLUMN_TREE=>EVENTID_HEADER_CLICK.
  APPEND L_EVENT TO LT_EVENTS.
  L_EVENT-EVENTID = CL_GUI_COLUMN_TREE=>EVENTID_ITEM_KEYPRESS.
  APPEND L_EVENT TO LT_EVENTS.
  CALL METHOD TREE1->SET_REGISTERED_EVENTS
    EXPORTING
      EVENTS                    = LT_EVENTS
    EXCEPTIONS
      CNTL_ERROR                = 1
      CNTL_SYSTEM_ERROR         = 2
      ILLEGAL_EVENT_COMBINATION = 3.
  IF SY-SUBRC <> 0.
    MESSAGE X208(00) WITH 'ERROR'.                          "#EC NOTEXT
  ENDIF.
* set Handler
  DATA: L_EVENT_RECEIVER TYPE REF TO LCL_TREE_EVENT_RECEIVER.
  CREATE OBJECT L_EVENT_RECEIVER.
  SET HANDLER L_EVENT_RECEIVER->HANDLE_NODE_CTMENU_REQUEST
                                                        FOR TREE1.
  SET HANDLER L_EVENT_RECEIVER->HANDLE_NODE_CTMENU_SELECTED
                                                        FOR TREE1.
  SET HANDLER L_EVENT_RECEIVER->HANDLE_ITEM_CTMENU_REQUEST
                                                        FOR TREE1.
  SET HANDLER L_EVENT_RECEIVER->HANDLE_ITEM_CTMENU_SELECTED
                                                        FOR TREE1.
ENDFORM.                    " register_events
*&---------------------------------------------------------------------*
*&      Form  add_node_main
*&---------------------------------------------------------------------*
FORM ADD_NODE      USING       P_AUX TYPE IT
                               P_RELAT_KEY TYPE LVC_NKEY
                               P_NODE TYPE LVC_VALUE
                     CHANGING  P_NODE_KEY TYPE LVC_NKEY.
  DATA: L_NODE_TEXT TYPE LVC_VALUE,
        LS_SFLIGHT TYPE SFLIGHT.
* set item-layout
  DATA: LT_ITEM_LAYOUT TYPE LVC_T_LAYI,
        LS_ITEM_LAYOUT TYPE LVC_S_LAYI.
*  ls_item_layout-t_image = '<at:var at:name="3P" />'.
  LS_ITEM_LAYOUT-FIELDNAME = TREE1->C_HIERARCHY_COLUMN_NAME.
  LS_ITEM_LAYOUT-STYLE   =
                        CL_GUI_COLUMN_TREE=>STYLE_INTENSIFD_CRITICAL.
  APPEND LS_ITEM_LAYOUT TO LT_ITEM_LAYOUT.
* add node
  L_NODE_TEXT =  P_NODE.
  CALL METHOD TREE1->ADD_NODE
    EXPORTING
      I_RELAT_NODE_KEY = P_RELAT_KEY
      I_RELATIONSHIP   = CL_GUI_COLUMN_TREE=>RELAT_LAST_CHILD
      I_NODE_TEXT      = L_NODE_TEXT
      IS_OUTTAB_LINE   = P_AUX
      IT_ITEM_LAYOUT   = LT_ITEM_LAYOUT
    IMPORTING
      E_NEW_NODE_KEY   = P_NODE_KEY.
*****
ENDFORM.                    " add_node
*&---------------------------------------------------------------------*
*&      Form  add_complete_line
*&---------------------------------------------------------------------*
FORM ADD_COMPLETE_LINE USING   P_AUX TYPE IT
                               P_RELAT_KEY TYPE LVC_NKEY
                               P_NODE TYPE LVC_VALUE
                     CHANGING  P_NODE_KEY TYPE LVC_NKEY.
  DATA: L_NODE_TEXT TYPE LVC_VALUE.
* set item-layout
  DATA: LT_ITEM_LAYOUT TYPE LVC_T_LAYI,
        LS_ITEM_LAYOUT TYPE LVC_S_LAYI.
  LS_ITEM_LAYOUT-FIELDNAME = TREE1->C_HIERARCHY_COLUMN_NAME.
  LS_ITEM_LAYOUT-CLASS   = CL_GUI_COLUMN_TREE=>ITEM_CLASS_TEXT.
*  ls_item_layout-editable = 'X'.
  APPEND LS_ITEM_LAYOUT TO LT_ITEM_LAYOUT.
  L_NODE_TEXT =  P_NODE.
  CALL METHOD TREE1->ADD_NODE
    EXPORTING
      I_RELAT_NODE_KEY = P_RELAT_KEY
      I_RELATIONSHIP   = CL_GUI_COLUMN_TREE=>RELAT_LAST_CHILD
      IS_OUTTAB_LINE   = P_AUX
      I_NODE_TEXT      = L_NODE_TEXT
      IT_ITEM_LAYOUT   = LT_ITEM_LAYOUT
    IMPORTING
      E_NEW_NODE_KEY   = P_NODE_KEY.
ENDFORM.                    " add_complete_line
*&---------------------------------------------------------------------*
*&      Form  add_node_main
*&---------------------------------------------------------------------*
FORM ADD_NODE_MAIN USING       P_AUX TYPE IT
                               P_RELAT_KEY TYPE LVC_NKEY
                               P_NODE TYPE LVC_VALUE
                     CHANGING  P_NODE_KEY TYPE LVC_NKEY.
  DATA: L_NODE_TEXT TYPE LVC_VALUE,
        LS_SFLIGHT TYPE SFLIGHT.
* set item-layout
  DATA: LT_ITEM_LAYOUT TYPE LVC_T_LAYI,
        LS_ITEM_LAYOUT TYPE LVC_S_LAYI.
*  ls_item_layout-t_image = '<at:var at:name="3P" />'.
  LS_ITEM_LAYOUT-FIELDNAME = TREE1->C_HIERARCHY_COLUMN_NAME.
  LS_ITEM_LAYOUT-STYLE   =
                        CL_GUI_COLUMN_TREE=>STYLE_INTENSIFD_CRITICAL.
  APPEND LS_ITEM_LAYOUT TO LT_ITEM_LAYOUT.
* add node
  L_NODE_TEXT =  P_NODE.
  CALL METHOD TREE1->ADD_NODE
    EXPORTING
      I_RELAT_NODE_KEY = P_RELAT_KEY
      I_RELATIONSHIP   = CL_GUI_COLUMN_TREE=>RELAT_LAST_CHILD
      I_NODE_TEXT      = L_NODE_TEXT
      IS_OUTTAB_LINE   = P_AUX
      IT_ITEM_LAYOUT   = LT_ITEM_LAYOUT
    IMPORTING
      E_NEW_NODE_KEY   = P_NODE_KEY.
*****
ENDFORM.                    " add_node_main

4 Comments

  1. Guest

    Thank you for the example.

  2. perfect (smile) , it is a good example, I can remember what I have learned about Data Structure. Thank you .