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

To generate the alv tree in report beside use OO class, we can also use FM to implement it. After displaying, we can also deal with the user action on the alv tree.

To achieve this, several step needs to do:

1. Get necessary data that you want to display

2. Generate the nodes of alv tree

3. Call FM 'RS_TREE_CONSTRUCT' to construct the tree

4. Define our own GUI status. In the following example, I assign a function code 'DOUB' to F2 function key

5. Call FM 'RS_TREE_LIST_DISPLAY'. Here we need 'CALLBACK_USER_COMMAND' to process user action in the subroutine; 'CALLBACK_GUI_STATUS' to display self-defined gui status

6. In the subroutine 'USER_COMMAND', process user action. In the example I only give 3 functions

    <1> Use FM 'RS_TREE_EXPAND' to expand the node list where user click

    <2> Use FM 'RS_TREE_COMPRESS' to compress the node

    <3> When user click the second level node, give a alv list about the detail info.

 The following is the code sample:

Error rendering macro 'code': Invalid value specified for parameter 'lang'
REPORT  zyh_tree_call_list_alv.
TYPE-POOLS: slis, fibs, stree.
*-----------------------------------------------------------------------
* Explanation of these four tables. The relationship
*    scarr      Airline
*    spfli      Airplane
*    sflight    One Flight Schedule
*    sbook      Booking Flight
*-----------------------------------------------------------------------
* Type declare
TYPES:
  BEGIN OF ty_scarr,
    carrid TYPE s_carr_id,
  END OF ty_scarr,
  BEGIN OF ty_spfli,
    carrid TYPE s_carr_id,
    connid TYPE s_conn_id,
  END OF ty_spfli,
  BEGIN OF ty_sflight,
    carrid TYPE s_carr_id,
    connid TYPE s_conn_id,
    fldate TYPE s_date,
  END OF ty_sflight.
* Table type declare
TYPES:
  ty_scarr_tab TYPE STANDARD TABLE OF ty_scarr,
  ty_spfli_tab TYPE STANDARD TABLE OF ty_spfli,
  ty_sflight_tab TYPE STANDARD TABLE OF ty_sflight.
* Define internal table
DATA:
  i_scarr TYPE ty_scarr_tab,
  i_spfli TYPE ty_spfli_tab,
  i_sflight TYPE ty_sflight_tab,
  w_node TYPE snodetext,
  i_node TYPE STANDARD TABLE OF snodetext,
  i_fieldcat TYPE slis_t_fieldcat_alv.
START-OF-SELECTION.
  PERFORM get_data.
  PERFORM build_tree.
  PERFORM display_tree.
*&---------------------------------------------------------------------*
*&      Form  get_data
*&---------------------------------------------------------------------*
*       Select data
*----------------------------------------------------------------------*
FORM get_data.
  SELECT carrid
    FROM scarr
    INTO TABLE i_scarr.
  SELECT carrid
         connid
    FROM spfli
    INTO TABLE i_spfli
    FOR ALL ENTRIES IN i_scarr
    WHERE carrid = i_scarr-carrid.
ENDFORM.                    "get_data
*&---------------------------------------------------------------------*
*&      Form  build_tree
*&---------------------------------------------------------------------*
*       Build Node of Tree
*----------------------------------------------------------------------*
FORM build_tree.
  DATA:
    w_scarr TYPE ty_scarr,
    w_spfli TYPE ty_spfli.
  CLEAR: i_node, w_node.
  SORT:  i_scarr BY carrid,
         i_spfli BY carrid connid.
***************************************************************************
* Title node display in header
* node name style
  w_node-type = 'T'. "T type
  w_node-name = 'Flight Details'.
  w_node-tlevel = '01'. "This level is hiberarchy level
  w_node-nlength = '15'. "The space that the name of node occupy
  w_node-color = '4'. "color of name of node
* node content style
  w_node-text = 'Information About Flight(Node Text)'.
  w_node-tlength = '50'.
  w_node-tcolor = 3.
  APPEND w_node TO i_node.
  CLEAR w_node.
***************************************************************************
* First level node
  LOOP AT i_scarr INTO w_scarr.
    w_node-type = 'P'.  "P type, real node
    w_node-name = 'CARRID'. "Node text that will display
    w_node-tlevel = '02'. "Hiberarchy level
    w_node-nlength = '8'. "The space that name text will ocupy
    w_node-color = '1'. "Node name text color
    w_node-text = w_scarr-carrid. "Node content text
    w_node-tlength = '20'. "Node content text space.
    w_node-tcolor = 4. "Node content color
    APPEND w_node TO i_node.
    CLEAR w_node.
*   Second level node
    LOOP AT i_spfli INTO w_spfli WHERE carrid = w_scarr-carrid.
      w_node-type = 'P'.
      w_node-name = 'CONNID'.
      w_node-tlevel = '03'.
      w_node-nlength = '8'.
      w_node-color = 1.
      w_node-text = w_spfli-connid.
      w_node-tlength = '20'.
      w_node-color = 4.
      APPEND w_node TO i_node.
      CLEAR w_node.
    ENDLOOP.
  ENDLOOP.
ENDFORM.                    "build_tree
*&---------------------------------------------------------------------*
*&      Form  display_tree
*&---------------------------------------------------------------------*
*       Display alv tree
*----------------------------------------------------------------------*
FORM display_tree.
* Construtor
  CALL FUNCTION 'RS_TREE_CONSTRUCT'
    TABLES
      nodetab            = i_node
    EXCEPTIONS
      tree_failure       = 1
      id_not_found       = 2
      wrong_relationship = 3
      OTHERS             = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
* Display
  CALL FUNCTION 'RS_TREE_LIST_DISPLAY'
    EXPORTING
      callback_program                = sy-repid
      callback_user_command           = 'USER_COMMAND'
      callback_gui_status             = 'MY_STATUS'
            .
ENDFORM.                    "display_tree
*&---------------------------------------------------------------------*
*&      Form  user_command
*&---------------------------------------------------------------------*
*       Aware the paramter of this form must be defined like this
*----------------------------------------------------------------------*
*      -->PT_NODE          Node that be selected
*      -->PV_COMMAND       Fcode of user operation
*      -->PV_EXIT          text
*      -->PV_LIST_REFRESH  text
*----------------------------------------------------------------------*
FORM user_command TABLES    pt_node
                  STRUCTURE seucomm
                  USING     pv_command TYPE c
                  CHANGING  pv_exit TYPE c
                            pv_list_refresh TYPE c.
  CASE pv_command.
    WHEN 'EXIT'.
      LEAVE PROGRAM.
    WHEN 'DOUB'.
      IF pt_node-tlevel = '03'.
        PERFORM get_detail_data USING pt_node-text.
        PERFORM build_fieldcat.
        PERFORM alv_display.
      ENDIF.
*     expand list
      IF pt_node-selfield = 'PLUS'.
        CALL FUNCTION 'RS_TREE_EXPAND'
          EXPORTING
            node_id = pt_node-id.
*     compress list
      ELSEIF pt_node-selfield = 'MINUS'.
        CALL FUNCTION 'RS_TREE_COMPRESS'
          EXPORTING
            node_id = pt_node-id.
      ENDIF.
  ENDCASE.
ENDFORM.                    "user_command
*&---------------------------------------------------------------------*
*&      Form  my_status
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM my_status.
  SET PF-STATUS 'STATUS'.
ENDFORM.                    "my_status
*&---------------------------------------------------------------------*
*&      Form  get_detail_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->ID         text
*----------------------------------------------------------------------*
FORM get_detail_data USING id TYPE seu_text.
  CLEAR i_sflight.
  SELECT carrid
         connid
         fldate
    FROM sflight
    INTO TABLE i_sflight
    WHERE connid = id.
ENDFORM.                    "get_detail_data
*&---------------------------------------------------------------------*
*&      Form  build_fieldcat
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM build_fieldcat.
  DATA:
    w_fieldcat TYPE slis_fieldcat_alv.
  CLEAR i_fieldcat.
  w_fieldcat-fieldname = 'CARRID'.
  w_fieldcat-seltext_m = 'Airline carrier'.
  APPEND w_fieldcat TO i_fieldcat.
  CLEAR w_fieldcat.
  w_fieldcat-fieldname = 'CONNID'.
  w_fieldcat-seltext_m = 'Connection No'.
  APPEND w_fieldcat TO i_fieldcat.
  CLEAR w_fieldcat.
  w_fieldcat-fieldname = 'FLDATE'.
  w_fieldcat-seltext_m = 'Flight Date'.
  APPEND w_fieldcat TO i_fieldcat.
  CLEAR w_fieldcat.
ENDFORM.                    "build_fieldcat
*&---------------------------------------------------------------------*
*&      Form  alv_display
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM alv_display.
  CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
    EXPORTING
      i_callback_program = sy-repid
      it_fieldcat        = i_fieldcat
    TABLES
      t_outtab           = i_sflight.
ENDFORM.                    "alv_display