Sunday, 2 August 2015

Creating Tree ALV with Class CL_GUI_ALV_TREE

---------------------------------------------------------------------------------------------------------

DATA :   lr_cust TYPE REF TO cl_gui_custom_container,
              lr_tree TYPE REF TO cl_gui_alv_tree,
              lt_scarr TYPE TABLE OF scarr,
              gt_scarr TYPE TABLE OF scarr,
              ls_scarr TYPE scarr,
              ls_header TYPE treev_hhdr,
              lv_node_key1 TYPE lvc_nkey,
              lv_node_key2 TYPE lvc_nkey,
              lv_node_text TYPE lvc_value.

START-OF-SELECTION.
  CALL SCREEN 0001.
*&---------------------------------------------------------------------*
*&      Module  STATUS_0001  OUTPUT
*&---------------------------------------------------------------------*

MODULE status_0001 OUTPUT.
  SET PF-STATUS 'STATUS'.
*  SET TITLEBAR 'xxx'.
ENDMODULE.                 " STATUS_0001  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  GET_DATA  OUTPUT
*&---------------------------------------------------------------------*
MODULE get_data OUTPUT.
  IF lt_scarr IS INITIAL.
    SELECT FROM scarr INTO TABLE lt_scarr.
  ENDIF.
ENDMODULE.                 " GET_DATA  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  BUILD_TREE  OUTPUT
*&---------------------------------------------------------------------*
MODULE build_tree OUTPUT.


  IF lr_cust IS NOT BOUND.


    CREATE OBJECT lr_cust
      EXPORTING
        container_name              'ALV_TREE'
        repid                       sy-repid
        dynnr                       sy-dynnr
      EXCEPTIONS
        cntl_error                  1
        cntl_system_error           2
        create_error                3
        lifetime_error              4
        lifetime_dynpro_dynpro_link 5
        OTHERS                      6.

  ENDIF.


  IF lr_tree IS NOT BOUND.


    CREATE OBJECT lr_tree
      EXPORTING
        parent                      lr_cust
        node_selection_mode         cl_gui_column_tree=>node_sel_mode_single
        item_selection              'X'    " Can Individual Items be Selected?
        no_toolbar                  ' '    " NO_TOOLBAR
        no_html_header              'X'    " NO_HTML_HEADER
      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.

  ENDIF.


* Prepairing Header
  ls_header-heading 'FLight Information'.
  ls_header-tooltip 'Flight Data'.
  ls_header-width 40.
  ls_header-width_pix ' '.


  CALL METHOD lr_tree->set_table_for_first_display
    EXPORTING
      i_structure_name    'SCARR'
      is_hierarchy_header ls_header
    CHANGING
      it_outtab           gt_scarr" table must be initial

* Prepairing Root node


  lv_node_text 'Flight Code'.
  CALL METHOD lr_tree->add_node
    EXPORTING
      i_relat_node_key     ' '
      i_relationship       cl_gui_column_tree=>relat_last_child
      i_node_text          lv_node_text
    IMPORTING
      e_new_node_key       lv_node_key1  " get the root node key
    EXCEPTIONS
      relat_node_not_found 1
      node_not_found       2
      OTHERS               3.

* Create Hierarchy ( nodes )
  LOOP AT lt_scarr INTO ls_scarr.

    lv_node_text ls_scarr-carrname.


    CALL METHOD lr_tree->add_node
      EXPORTING
        i_relat_node_key     lv_node_key1  " pass the root node key
        i_relationship       cl_gui_column_tree=>relat_last_child    " How to Insert Node
*        is_outtab_line       = ls_scarr    " Attributes of Inserted Node
        i_node_text          lv_node_text   " Hierarchy Node Text
      IMPORTING
        e_new_node_key       lv_node_key2   " Key of New Node Key
      EXCEPTIONS
        relat_node_not_found 1
        node_not_found       2
        OTHERS               3.

  ENDLOOP.

* Must Call this method to display nodes on the container
  CALL METHOD lr_tree->frontend_update.


ENDMODULE.                 " BUILD_TREE  OUTPUT


*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0001  INPUT
*&---------------------------------------------------------------------*
MODULE user_command_0001 INPUT.
  CASE sy-ucomm.
    WHEN 'BACK' OR 'EXIT' OR 'CANCEL'.
      LEAVE TO SCREEN 0.
  ENDCASE.
ENDMODULE.                 " USER_COMMAND_0001  INPUT


---------------------------------------------------------------------------------------------------------

Screen flow Logic.
Screen  Layout.



Execute the program.

Here we have the tree.

Now remove the comment line so that we can display all the columns. Execute the program.


 Here is how the Tree ALV is displayed.




---------------------------------------------------------------------------------------------------------

No comments:

Comments system

Disqus Shortname