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:
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.
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.
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 ).
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.