Skip to Content
0
Dec 21, 2020 at 02:31 AM

Build ALV Tree

82 Views Last edit Dec 21, 2020 at 02:37 AM 5 rev

FORM BUILD_ALV_TREE .


DATA: LT_ITAB LIKE GT_IMPORT.

*-- Create an ALV Tree with an Empty table LT_ITAB
CLEAR LT_ITAB[].
TRY.
CL_SALV_TREE=>FACTORY(
IMPORTING
R_SALV_TREE = GR_TREE
CHANGING
T_TABLE = LT_ITAB[] ).
CATCH CX_SALV_NO_NEW_DATA_ALLOWED CX_SALV_ERROR.
EXIT.
ENDTRY.

*-- Create Tree
PERFORM CREATE_TREE.

*-- Columns
LR_COLUMNS = GR_TREE->GET_COLUMNS( ).
LR_COLUMNS->SET_OPTIMIZE( 'X' ).

LR_COLUMN = LR_COLUMNS->GET_COLUMN( 'LEVEL').
LR_COLUMN->SET_VISIBLE( ABAP_FALSE ).

*-- header
SETTINGS = GR_TREE->GET_TREE_SETTINGS( ).
SETTINGS->SET_HIERARCHY_HEADER( 'LEVEL ').
SETTINGS->SET_HIERARCHY_SIZE( 30 ).
SETTINGS->SET_HIERARCHY_ICON( '@3M@' ).

*-- functions
GR_FUNCTIONS = GR_TREE->GET_FUNCTIONS( ).
GR_FUNCTIONS->SET_ALL( ABAP_TRUE ).

*-- Display the table
GR_TREE->DISPLAY( ).

ENDFORM. " DISPLAY_ALV_TREE
*&---------------------------------------------------------------------*
*& Form CREATE_TREE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM CREATE_TREE .

DATA: L_LEVEL_KEY TYPE LVC_NKEY,
L_VNDMN_KEY TYPE LVC_NKEY,
L_LAST_KEY TYPE LVC_NKEY.

*-- Loop at the Internal table GT_IMPORT,
* --> For every new LEVEL we gone create a node
* --> For every new VNDMN line we will add a line
* The tree logic is as follow
* - LEVEL 1
* - VNDMN 1
* - VNDMN 2
* - VNDMN 3

* - LEVEL 2
* - VNDMN 1
* - VNDMN 2
* - VNDMN 3

LOOP AT GT_IMPORT INTO GS_IMPORT.
* LOOP AT <GT_IMPORT> ASSIGNING <GS_IMPORT>.

*-- on change of LEVEL
ON CHANGE OF GS_IMPORT-LEVEL.
PERFORM ADD_LEVEL_LINE USING GS_IMPORT
' '
CHANGING L_LEVEL_KEY.
ENDON.

*-- Add Line
PERFORM ADD_COMPLETE_LINE USING GS_IMPORT
L_VNDMN_KEY
CHANGING L_LAST_KEY.

ENDLOOP. "gt_itab

ENDFORM. " CREATE_TREE
*&---------------------------------------------------------------------*
*& Form ADD_LEVEL_LINE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_GS_IMPORT text
* -->P_0474 text
* <--P_L_LEVEL_KEY text
*----------------------------------------------------------------------*
FORM ADD_LEVEL_LINE USING LS_ITAB LIKE GS_IMPORT
P_KEY
CHANGING P_LEVEL_KEY.

DATA: NODES TYPE REF TO CL_SALV_NODES,
NODE TYPE REF TO CL_SALV_NODE,
ITEM TYPE REF TO CL_SALV_ITEM.
DATA: LV_TEXT TYPE LVC_VALUE.
DATA: LS_DATA LIKE GS_IMPORT.
* FIELD-SYMBOLS: <LS_DATA> LIKE <GS_IMPORT>.

LS_DATA = LS_ITAB.

LV_TEXT = LS_ITAB-LEVEL.
CLEAR LS_DATA.

*... §0 working with nodes
NODES = GR_TREE->GET_NODES( ).

TRY.
* ... §0.1 add a new node
* ... §0.3 set the data for the nes node
NODE = NODES->ADD_NODE( RELATED_NODE = P_KEY
DATA_ROW = LS_DATA
RELATIONSHIP = CL_GUI_COLUMN_TREE=>RELAT_LAST_CHILD ).
NODE->SET_TEXT( LV_TEXT ).
P_LEVEL_KEY = NODE->GET_KEY( ).

CATCH CX_SALV_MSG.
ENDTRY.

ENDFORM. " ADD_LEVEL_LINE

*&---------------------------------------------------------------------*
*& Form ADD_COMPLETE_LINE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_GS_IMPORT text
* -->P_L_VNDMN_KEY text
* <--P_L_LAST_KEY text
*----------------------------------------------------------------------*
FORM ADD_COMPLETE_LINE USING LS_ITAB LIKE GS_IMPORT
P_VNDMN_KEY
CHANGING P_LAST_KEY.

DATA: NODES TYPE REF TO CL_SALV_NODES,
NODE TYPE REF TO CL_SALV_NODE.
DATA: LV_TEXT TYPE LVC_VALUE.

NODES = GR_TREE->GET_NODES( ).

TRY.
NODE = NODES->ADD_NODE( RELATED_NODE = P_VNDMN_KEY
DATA_ROW = LS_ITAB
RELATIONSHIP = CL_GUI_COLUMN_TREE=>RELAT_LAST_CHILD ).

P_LAST_KEY = NODE->GET_KEY( ).

LV_TEXT = LS_ITAB-LEVEL.
NODE->SET_TEXT( LV_TEXT ).


CATCH CX_SALV_MSG.
ENDTRY.

ENDFORM. " ADD_COMPLETE_LINE

I want to build ALV Tree to group line of infomation by level columm. But below this code the ALV Tree display(see picture attach) can not group line by level.

Attachments

level.png (57.4 kB)