How to develop A TREE LIST interactive report Using SAP ABAP with Function modules RS_TREE_CONSTRUCT and RS_TREE_LIST_DISPLAY
REPORT ZTREE_MATERIAL.
***Material Basic Data Declarations
TYPES: BEGIN OF TY_MARA,
MATNR TYPE MARA-MATNR,
MTART TYPE MARA-MTART,
MBRSH TYPE MARA-MBRSH,
MATKL TYPE MARA-MATKL,
MEINS TYPE MARA-MEINS,
END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA.
DATA : WA_MARA TYPE TY_MARA.
***Material Descriptions Declarations
TYPES: BEGIN OF TY_MAKT,
MATNR TYPE MAKT-MATNR,
SPRAS TYPE MAKT-SPRAS,
MAKTX TYPE MAKT-MAKTX,
END OF TY_MAKT.
DATA : IT_MAKT TYPE TABLE OF TY_MAKT.
DATA : WA_MAKT TYPE TY_MAKT.
***Data Table decleration for FM RS_TREE_CONSTRUCT
DATA: IT_NODE TYPE STANDARD TABLE OF SNODETEXT,
WA_NODE TYPE SNODETEXT.
**Declare Constants for TREE
CONSTANTS:
C_COL_KEY TYPE C LENGTH 1 VALUE 1,"COL_KEY,
C_COL_FIELD TYPE C LENGTH 1 VALUE 2,"COL_NORMAL,
C_COL_MATNR TYPE C LENGTH 1 VALUE 1,"COL_KEY,
C_COL_MAKTX TYPE C LENGTH 1 VALUE 5."COL_POSITIVE.
**Selection Screen
SELECT-OPTIONS : S_MATNR FOR WA_MARA-MATNR.
START-OF-SELECTION.
**Get Data From Tables
SELECT MATNR MTART MBRSH MATKL MEINS FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS WHERE MATNR IN S_MATNR.
SELECT MATNR SPRAS MAKTX FROM MAKT INTO TABLE IT_MAKT FOR ALL ENTRIES IN IT_MARA WHERE MATNR = IT_MARA-MATNR.
"Create root node at level 1
WA_NODE-TLEVEL = 1.
WA_NODE-NAME = 'Materials'.
WA_NODE-NLENGTH = 20.
WA_NODE-COLOR = C_COL_KEY.
WA_NODE-TEXT = 'Material Master Report'.
WA_NODE-TLENGTH = 50.
APPEND WA_NODE TO IT_NODE.
LOOP AT IT_MARA INTO WA_MARA.
CLEAR WA_NODE.
WA_NODE-TLEVEL = 2. "Node Level 2
"Material Number
WA_NODE-NAME = WA_MARA-MATNR.
WA_NODE-NLENGTH = 18.
WA_NODE-COLOR = C_COL_MATNR.
"Material Type
WA_NODE-TEXT1 = WA_MARA-MTART.
WA_NODE-TLENGTH1 = 4.
WA_NODE-TCOLOR1 = C_COL_FIELD.
"Industry Sector
WA_NODE-TEXT2 = WA_MARA-MBRSH.
WA_NODE-TLENGTH2 = 1.
WA_NODE-TCOLOR2 = C_COL_FIELD.
"Material Group
WA_NODE-TEXT3 = WA_MARA-MATKL.
WA_NODE-TLENGTH3 = 4.
WA_NODE-TCOLOR3 = C_COL_FIELD.
"Unit of Measure
WA_NODE-TEXT4 = WA_MARA-MEINS.
WA_NODE-TLENGTH4 = 3.
WA_NODE-TCOLOR4 = C_COL_FIELD.
APPEND WA_NODE TO IT_NODE.
LOOP AT IT_MAKT INTO WA_MAKT WHERE MATNR = WA_MARA-MATNR.
CLEAR WA_NODE.
WA_NODE-TLEVEL = 3. "Node level 3
"Material Number
WA_NODE-NAME = WA_MAKT-MATNR.
WA_NODE-NLENGTH = 18.
WA_NODE-COLOR = C_COL_MAKTX.
"Language ISO Code
WA_NODE-TEXT = WA_MAKT-SPRAS.
WA_NODE-TLENGTH = 2.
WA_NODE-TCOLOR = C_COL_FIELD.
"Material Description
WA_NODE-TEXT1 = WA_MAKT-MAKTX.
WA_NODE-TLENGTH1 = 40.
WA_NODE-TCOLOR1 = C_COL_FIELD.
APPEND WA_NODE TO IT_NODE.
ENDLOOP.
ENDLOOP.
**Construct Tree
CALL FUNCTION 'RS_TREE_CONSTRUCT'
* EXPORTING
* INSERT_ID = '000000'
* RELATIONSHIP = ' '
* LOG =
TABLES
NODETAB = IT_NODE
EXCEPTIONS
TREE_FAILURE = 1
ID_NOT_FOUND = 2
WRONG_RELATIONSHIP = 3
OTHERS = 4.
IF SY-SUBRC <> 0.
WRITE 'Error in Tree Construction'.
ENDIF.
***Display TREE
CALL FUNCTION 'RS_TREE_LIST_DISPLAY'
EXPORTING
CALLBACK_PROGRAM = SY-REPID.
Input
Final output of the TREE like looks like below.
To create a TREE LIST, we use
RS_TREE_CONSTRUCT
and RS_TREE_LIST_DISPLAY
Function Modules
RS_TREE_CONSTRUCT is used to construct a TREE node.
RS_TREE_LIST_DISPLAY is used to display/print constructed TREE.
REPORT ZTREE_MATERIAL.
***Material Basic Data Declarations
TYPES: BEGIN OF TY_MARA,
MATNR TYPE MARA-MATNR,
MTART TYPE MARA-MTART,
MBRSH TYPE MARA-MBRSH,
MATKL TYPE MARA-MATKL,
MEINS TYPE MARA-MEINS,
END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA.
DATA : WA_MARA TYPE TY_MARA.
***Material Descriptions Declarations
TYPES: BEGIN OF TY_MAKT,
MATNR TYPE MAKT-MATNR,
SPRAS TYPE MAKT-SPRAS,
MAKTX TYPE MAKT-MAKTX,
END OF TY_MAKT.
DATA : IT_MAKT TYPE TABLE OF TY_MAKT.
DATA : WA_MAKT TYPE TY_MAKT.
***Data Table decleration for FM RS_TREE_CONSTRUCT
DATA: IT_NODE TYPE STANDARD TABLE OF SNODETEXT,
WA_NODE TYPE SNODETEXT.
**Declare Constants for TREE
CONSTANTS:
C_COL_KEY TYPE C LENGTH 1 VALUE 1,"COL_KEY,
C_COL_FIELD TYPE C LENGTH 1 VALUE 2,"COL_NORMAL,
C_COL_MATNR TYPE C LENGTH 1 VALUE 1,"COL_KEY,
C_COL_MAKTX TYPE C LENGTH 1 VALUE 5."COL_POSITIVE.
**Selection Screen
SELECT-OPTIONS : S_MATNR FOR WA_MARA-MATNR.
START-OF-SELECTION.
**Get Data From Tables
SELECT MATNR MTART MBRSH MATKL MEINS FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS WHERE MATNR IN S_MATNR.
SELECT MATNR SPRAS MAKTX FROM MAKT INTO TABLE IT_MAKT FOR ALL ENTRIES IN IT_MARA WHERE MATNR = IT_MARA-MATNR.
"Create root node at level 1
WA_NODE-TLEVEL = 1.
WA_NODE-NAME = 'Materials'.
WA_NODE-NLENGTH = 20.
WA_NODE-COLOR = C_COL_KEY.
WA_NODE-TEXT = 'Material Master Report'.
WA_NODE-TLENGTH = 50.
APPEND WA_NODE TO IT_NODE.
LOOP AT IT_MARA INTO WA_MARA.
CLEAR WA_NODE.
WA_NODE-TLEVEL = 2. "Node Level 2
"Material Number
WA_NODE-NAME = WA_MARA-MATNR.
WA_NODE-NLENGTH = 18.
WA_NODE-COLOR = C_COL_MATNR.
"Material Type
WA_NODE-TEXT1 = WA_MARA-MTART.
WA_NODE-TLENGTH1 = 4.
WA_NODE-TCOLOR1 = C_COL_FIELD.
"Industry Sector
WA_NODE-TEXT2 = WA_MARA-MBRSH.
WA_NODE-TLENGTH2 = 1.
WA_NODE-TCOLOR2 = C_COL_FIELD.
"Material Group
WA_NODE-TEXT3 = WA_MARA-MATKL.
WA_NODE-TLENGTH3 = 4.
WA_NODE-TCOLOR3 = C_COL_FIELD.
"Unit of Measure
WA_NODE-TEXT4 = WA_MARA-MEINS.
WA_NODE-TLENGTH4 = 3.
WA_NODE-TCOLOR4 = C_COL_FIELD.
APPEND WA_NODE TO IT_NODE.
LOOP AT IT_MAKT INTO WA_MAKT WHERE MATNR = WA_MARA-MATNR.
CLEAR WA_NODE.
WA_NODE-TLEVEL = 3. "Node level 3
"Material Number
WA_NODE-NAME = WA_MAKT-MATNR.
WA_NODE-NLENGTH = 18.
WA_NODE-COLOR = C_COL_MAKTX.
"Language ISO Code
WA_NODE-TEXT = WA_MAKT-SPRAS.
WA_NODE-TLENGTH = 2.
WA_NODE-TCOLOR = C_COL_FIELD.
"Material Description
WA_NODE-TEXT1 = WA_MAKT-MAKTX.
WA_NODE-TLENGTH1 = 40.
WA_NODE-TCOLOR1 = C_COL_FIELD.
APPEND WA_NODE TO IT_NODE.
ENDLOOP.
ENDLOOP.
**Construct Tree
CALL FUNCTION 'RS_TREE_CONSTRUCT'
* EXPORTING
* INSERT_ID = '000000'
* RELATIONSHIP = ' '
* LOG =
TABLES
NODETAB = IT_NODE
EXCEPTIONS
TREE_FAILURE = 1
ID_NOT_FOUND = 2
WRONG_RELATIONSHIP = 3
OTHERS = 4.
IF SY-SUBRC <> 0.
WRITE 'Error in Tree Construction'.
ENDIF.
***Display TREE
CALL FUNCTION 'RS_TREE_LIST_DISPLAY'
EXPORTING
CALLBACK_PROGRAM = SY-REPID.
Input
Final output of the TREE like looks like below.