Application Development and Automation Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Bikash_Mishra
Newcomer
6,821

Introduction: In this blog post, we will explore how to create a tree structure in SAP ABAP using the CL_SALV_TREE class, which is part of the SAP ALV (ABAP List Viewer) framework. We will go through a simple example that demonstrates how to display hierarchical data using a tree control and how to handle data dynamically. 

Problem Overview: 

In this scenario we aim to achieve the following goals with this code: 

  1. Retrieve material master data from the MARA table based on user input (material number).
  2. Group the data in a hierarchical structure by material type (MTART). 
  3. Display the resulting data in a tree-like structure, making it easier for users to navigate through material data. 

     Steps to Create a Tree Structure: 

    We will break down the code step by step to understand how to create the tree structure and display the material master data. 

    Define a Custom Structure for Material Master Data 

     

    The first part of the code defines a custom structure ST_MARA that contains selected fields from the MARA table. This custom structure helps to organize and store the data we retrieve from the table.

 

TYPES: BEGIN OF st_mara, 
         matnr TYPE mara-matnr, 
         ernam TYPE mara-ernam, 
         laeda TYPE mara-laeda, 
         aenam TYPE mara-aenam, 
         vpsta TYPE mara-vpsta, 
         pstat TYPE mara-pstat, 
         mtart TYPE mara-mtart, 
         meins TYPE mara-meins, 
     END OF st_mara. 

 

 

 

Declare Data Variables 

 

In this section, we declare the necessary data variables, such as the work area (WA_MARA) and internal table (IT_MARA) to store the material data. We also declare variables related to the tree structure that will be generated by the CL_SALV_TREE class. 

 

 

DATA: wa_mara   TYPE st_mara, 
     it_mara   TYPE STANDARD TABLE OF st_mara, 
     it_outtab LIKE it_mara. 
 
DATA: lo_tree TYPE REF TO cl_salv_tree. 
DATA: nodes         TYPE REF TO cl_salv_nodes, 
     node          TYPE REF TO cl_salv_node, 
     columns       TYPE REF TO cl_salv_columns, 
     lo_functions  TYPE REF TO cl_salv_functions_tree, 
     key           TYPE salv_de_node_key. 

 


 

  • LO_TREE: Reference to the instance of the CL_SALV_TREE class. 
  • nodes: A reference to the nodes in the tree. 
  • node: A reference to a specific node within the tree. 
  • columns: A reference to the tree columns for optimization. 
  • LO_FUNCTIONS: A reference to the tree functions (sorting, filtering, etc.). 
  • key: The unique key for each node in the tree. 

     

    Define the Selection Screen for Material Number 

     

    The SELECT-OPTIONS statement allows users to input a range of material numbers (MATNR) to filter the data they want to display. 

     

 

SELECT-OPTIONS s_matnr FOR wa_mara-matnr. 

 

 

Select Data from the MARA Table 

 

Here, we select data from the MARA table based on the user's input for the material numbers (MATNR). The data is stored in the internal table IT_MARA.  

 

SELECT matnr 
      ernam 
      laeda 
      aenam 
      vpsta 
      pstat 
      mtart 
      meins 
 FROM mara 
 INTO TABLE it_mara 
 WHERE matnr IN s_matnr. 
  

 


Create the Tree Instance 

We create an instance of the CL_SALV_TREE class using the factory method. The internal table IT_OUTTAB will store the data to be passed to the tree. 

 

 

CALL METHOD cl_salv_tree=>factory 
 IMPORTING 
   R_SALV_TREE = lo_tree 
 CHANGING 
   t_table     = it_outtab. 

 

 

The LO_TREE object is an instance of the CL_SALV_TREE class, and IT_OUTTAB is the table that will hold the data displayed in the tree. 

 

 
Add Nodes to the Tree 

 

This section is the heart of the logic that adds nodes to the tree structure. We retrieve the nodes using the GET_NODES () method. Then, we loop over each material in IT_MARA and add them to the tree grouped by their material type (MTART). 

 

NODES = lo_tree->get_nodes( ). 
LOOP AT it_mara INTO wa_mara. 
 ON CHANGE OF wa_mara-mtart. 
   CLEAR key. 
   TRY. 
       node = nodes->add_node( related_node = KEY 
       relationship = cl_gui_column_tree=>relat_first_child 
       data_row = wa_mara ). 
       KEY = node->get_key( ). 
     CATCH cx_salv_msg. 
   ENDTRY. 
 ENDON. 
  
 TRY. 
     node = nodes->add_node( related_node = KEY 
     relationship = cl_gui_column_tree=>relat_last_child 
     data_row = wa_mara ). 
   CATCH cx_salv_msg. 
 ENDTRY. 
ENDLOOP. 

 


 

  • The ON CHANGE OF WA_MARA-MTART ensures that we are grouping material data by material type (MTART). 
  • The add node method is called twice: 
  • First for the parent node (i.e., the material type). 
  • Then for each material as a child node of the corresponding material type. 

     

    Optimize Columns and Enable Tree Functions 

     

    After adding the data to the tree, we can optimize the columns for better readability and enable interactive tree functions such as sorting, filtering, and expanding nodes. 

 

columns = lo_tree->get_columns( ). 
columns->set_optimize( abap_true ). 
 
lo_functions = lo_tree->get_functions( ). 
lo_functions->set_all( abap_true ). 

 


 

  • The SET_OPTIMIZE method ensures that the tree is displayed efficiently with appropriate column widths. 
  • The SET_ALL method enables all the tree functions, making the tree interactive for users. 

     

    Display the Tree 

    Finally, the tree is displayed using the display method. 

 

lo_tree->display( ). 

 

 

 

This method renders the tree on the screen for the user to interact with. 

 

Code Summary: 

Here is the complete ABAP code for creating a tree structure based on the Material data: 

 

REPORT ZTREE_EXAMPLE1. 

 
TYPES: BEGIN OF st_mara, 
          matnr TYPE mara-matnr, 
          ernam TYPE Mara-ernam, 
          laeda TYPE Mara-laeda, 
          aenam TYPE Mara-aenam, 
          vpsta TYPE Mara-vpsta, 
          pstat TYPE Mara-pstat, 
          mtart TYPE mara-mtart, 
          meins TYPE Mara-meins, 
      END OF st_mara. 

DATA: wa_mara   TYPE st_mara, 
      it_mara   TYPE STANDARD TABLE OF st_mara, 
      it_outtab LIKE it_mara. 
DATA: lo_tree TYPE REF TO cl_salv_tree. 

DATA: nodes         TYPE REF TO cl_salv_nodes, 
      node          TYPE REF TO cl_salv_node, 
      columns       type ref to cl_salv_columns, 
      lo_functions  TYPE REF TO cl_salv_functions_tree, 
      key           TYPE salv_de_node_key. 

 

SELECT-OPTIONS s_matnr FOR wa_mara-matnr. 
" Select data 
 
SELECT matnr 
       ernam 
       laeda 
       aenam 
       vpsta 
       pstat 
       mtart 
       meins   FROM mara 
    INTO TABLE it_mara WHERE matnr IN s_matnr. 

"Create instance with an empty table 
CALL METHOD cl_salv_tree=>factory 
  IMPORTING 
    R_SALV_TREE = lo_tree 
  CHANGING 
    t_table     = it_outtab. 
" Add the nodes to the tree 
NODES = lo_tree->get_nodes( ). 
LOOP AT it_mara INTO wa_mara. 
  ON CHANGE OF wa_mara-mtart. 
    CLEAR key. 
    TRY. 
        node = nodes->add_node( related_node = KEY 
        relationship = cl_gui_column_tree=>relat_first_child 
        data_row = wa_mara ). 
        KEY = node->get_key( ). 
      CATCH cx_salv_msg. 
    ENDTRY. 
  ENDON. 
  TRY. 
      node = nodes->add_node( related_node = KEY 
      relationship = cl_gui_column_tree=>relat_last_child 
      data_row = wa_mara ). 
    CATCH cx_salv_msg. 
  ENDTRY. 
ENDLOOP. 
columns = lo_tree->get_columns( ). 
columns->set_optimize( abap_true ). 
"Set default status 
lo_functions = lo_tree->get_functions( ). 
lo_functions->set_all( abap_true ). 
"Display table 
lo_tree->display( ). 

 

Output: 

Conclusion: 

In this blog post, we demonstrated how to create a tree structure in SAP ABAP using the CL_SALV_TREE class. By following these steps, you can display data in an ALV tree format, which makes it easier to represent data and improve the user experience when dealing with large datasets. The tree structure allows users to view data in a structured way, expanding and collapsing nodes to navigate through complex information. 

  

 

1 Comment