You are on page 1of 12

SALV Tree

SALV
- cl_salv_tree.

.
, , ,
.
, SALV ,
-
factory().

, .
.

,
r_container.
hide_header
abap_true (X).
, :

1 REPORT

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

FIELD-SYMBOLS:
CLASS
PUBLIC SECTION.
CLASS-DATA:
go_alv_tree TYPE REF TO cl_salv_tree,
gt_empty_tab TYPE STANDARD TABLE OF spfli,
gt_full_tab TYPE STANDARD TABLE OF spfli.
CLASS-METHODS:
create_tree, " ALV
get_data,
"
create_nodes, " ( )
show_tree.
" () ALV
ENDCLASS.
START-OF-SELECTION.
lcl_report=>get_data( ).
lcl_report=>create_tree( ).
lcl_report=>create_nodes( ).
lcl_report=>show_tree( ).
CLASS lcl_report IMPLEMENTATION.
METHOD show_tree.
go_alv_tree->display( ).
ENDMETHOD.
METHOD create_nodes.
DATA:
lo_nodes TYPE REF TO cl_salv_nodes,
lo_node TYPE REF TO cl_salv_node.
"
lo_nodes = go_alv_tree->get_nodes( ).
SORT gt_full_tab BY carrid.
"
LOOP AT gt_full_tab ASSIGNING <fs_line>.
TRY.
lo_node = lo_nodes->add_node( related_node = ''
relationship = cl_gui_column_tree=>relat_first_child
).
"
lo_node->set_data_row( <fs_line> ).
CATCH cx_salv_msg.
ENDTRY.
ENDLOOP.
ENDMETHOD.
METHOD get_data.
SELECT * FROM spfli INTO CORRESPONDING FIELDS OF TABLE gt_full_tab.
ENDMETHOD.
METHOD create_tree.
TRY.
cl_salv_tree=>factory(
IMPORTING
r_salv_tree = go_alv_tree
CHANGING
t_table = gt_empty_tab ).
CATCH cx_salv_error.
MESSAGE ' ALV' TYPE 'E'.
ENDTRY.
ENDMETHOD.
ENDCLASS.


,
.
- add_node. :

related_node

,
( ).
cl_salv_node key.
.

relationship

, ,
cl_gui_column_tree:
relat_first_child
relat_last_child
relat_first_sibling
relat_last_sibling
relat_next_sibling
relat_prev_sibling

data_row

,
.

collapsen_icon

expanded_icon

text

visible

folder

abap_true,
,

expander

()

row_style

() .
IF_SALV_C_TREE_STYLE
:
DEFAULT
INHERITED
INTENSIFIED
INACTIVE
INTENSIFIED_CRITICAL
EMPHASIZED_NEGATIVE
EMPHASIZED_POSITIVE
EMPHASIZED
EMPHASIZED_A
EMPHASIZED_B
EMPHASIZED_C


. cl_salv_tree_settings.
, :

1 METHOD create_nodes.
2
DATA:
3
lo_nodes TYPE REF TO cl_salv_nodes,
4
lo_node TYPE REF TO cl_salv_node,
5
lv_carrid_key TYPE lvc_nkey,
6
lv_expand_icon TYPE SALV_DE_TREE_IMAGE,
7
lv_collapse_icon TYPE SALV_DE_TREE_IMAGE,
8
lv_hier_icon TYPE SALV_DE_TREE_IMAGE,
9
lo_settings TYPE REF TO cl_salv_tree_settings,
10
lv_carrid TYPE spfli-carrid.
11
12 lv_expand_icon = icon_expand.
13 lv_collapse_icon = icon_collapse.
14 lv_hier_icon = icon_tree.
15
16 lo_settings = go_alv_tree->get_tree_settings( ).
17 lo_settings->set_hierarchy_header( '' ).
18 lo_settings->set_hierarchy_size( 30 ).
19 lo_settings->set_hierarchy_icon( lv_hier_icon ).
20
21 "
22 lo_nodes = go_alv_tree->get_nodes( ).
23 SORT gt_full_tab BY carrid.
24 "
25 LOOP AT gt_full_tab ASSIGNING <fs_line>.
26
IF lv_carrid <> <fs_line>-carrid.
27
TRY.
28
lo_node = lo_nodes->add_node(
29
related_node = ''
30
relationship = cl_gui_column_tree=>relat_last_child
31
data_row
= <fs_line>

32
collapsed_icon = lv_expand_icon
33
expanded_icon = lv_collapse_icon
34
row_style
= IF_SALV_C_TREE_STYLE=>emphasized_a
35
text
= ' ' ).
36
lv_carrid_key = lo_node->get_key( ).
37
CATCH cx_salv_msg.
38
ENDTRY.
39
ELSE.
40
TRY.
41
lo_node = lo_nodes->add_node(
42
related_node = lv_carrid_key
43
relationship = cl_gui_column_tree=>relat_last_child
44
data_row
= <fs_line>
45
collapsed_icon = lv_expand_icon
46
row_style
= IF_SALV_C_TREE_STYLE=>emphasized_b
47
expanded_icon = lv_collapse_icon
48
text
= ' ' ).
49
CATCH cx_salv_msg.
50
ENDTRY.
51
ENDIF.
52
lv_carrid = <fs_line>-carrid.
53 ENDLOOP.
54 ENDMETHOD.
:


cl_salv_table,
GUI ,
SALV_DEMO_TREE_SIMPLE.
,
cl_salv_functions_tree .
GUI
,
,
:

1 METHOD setup_functions.
2 go_alv_tree->set_screen_status(
3
pfstatus
= 'SALV_STANDARD'
4
REPORT
= sy-repid "
5
set_functions = go_alv_tree->c_functions_all ).
6 ENDMETHOD.

1 METHOD setup_functions.
2 DATA: lo_functions TYPE REF TO cl_salv_functions_tree.
3
4 lo_functions = go_alv_tree->get_functions( ).
5 lo_functions->set_all( ).
6 ENDMETHOD.


- &CALC_SUM.


cl_salv_table,

cl_salv_columns_tree cl_salv_column_tree.

:

1 METHOD setup_columns.

2 DATA:
3
lo_columns TYPE REF TO cl_salv_columns_tree,
4
lo_column TYPE REF TO cl_salv_column_tree.
5
6 lo_columns = go_alv_tree->get_columns( ).
7 lo_columns->set_optimize( abap_true ).
8 ENDMETHOD.



cl_salv_events_tree.
:

TOP_OF_PAGE
END_OF_PAGE
BEFORE_SALV_FUNCTION
AFTER_SALV_FUNCTION
ADDED_FUNCTION
LINK_CLICK (, ..)
DOUBLE_CLICK
KEYPRESS
CHECKBOX_CHANGE
EXPAND_EMPTY_FOLDER


SALV_DEMO_TREE_EVENTS.


cl_salv_item.
, (,
..), ,
(checkbox).
,
get_item( ).
:

1 METHOD create_nodes.
2
DATA:
3
lo_nodes TYPE REF TO cl_salv_nodes,
4
lo_node TYPE REF TO cl_salv_node,
5
lv_carrid_key TYPE lvc_nkey,
6
lv_expand_icon TYPE SALV_DE_TREE_IMAGE,
7
lv_collapse_icon TYPE SALV_DE_TREE_IMAGE,
8
lv_hier_icon TYPE SALV_DE_TREE_IMAGE,
9
lo_item TYPE REF TO cl_salv_item,

10
lo_settings TYPE REF TO cl_salv_tree_settings,
11
lv_carrid TYPE spfli-carrid.
12
13 lv_expand_icon = icon_expand.
14 lv_collapse_icon = icon_collapse.
15 lv_hier_icon = icon_tree.
16
17 lo_settings = go_alv_tree->get_tree_settings( ).
18 lo_settings->set_hierarchy_header( '' ).
19 lo_settings->set_hierarchy_size( 30 ).
20 lo_settings->set_hierarchy_icon( lv_hier_icon ).
21
22 "
23 lo_nodes = go_alv_tree->get_nodes( ).
24 SORT gt_full_tab BY carrid.
25 "
26 LOOP AT gt_full_tab ASSIGNING <fs_line>.
27
IF lv_carrid <> <fs_line>-carrid.
28
TRY.
29
lo_node = lo_nodes->add_node(
30
related_node = ''
31
relationship = cl_gui_column_tree=>relat_last_child
32
data_row
= <fs_line>
33
collapsed_icon = lv_expand_icon
34
expanded_icon = lv_collapse_icon
35
row_style
= IF_SALV_C_TREE_STYLE=>emphasized_a
36
text
= ' ' ).
37
lv_carrid_key = lo_node->get_key( ).
38
lo_item = lo_node->get_item( 'CARRID' ).
39
lo_item->set_type( if_salv_c_item_type=>button ).
40
lo_item = lo_node->get_item( 'CITYFROM' ).
41
lo_item->set_font( if_salv_c_item_font=>fixed_size ).
42
lo_item->set_enabled( abap_false ).
43
CATCH cx_salv_msg.
44
ENDTRY.
45
ELSE.
46
TRY.
47
lo_node = lo_nodes->add_node(
48
related_node = lv_carrid_key
49
relationship = cl_gui_column_tree=>relat_last_child
50
data_row
= <fs_line>
51
collapsed_icon = lv_expand_icon
52
row_style
= IF_SALV_C_TREE_STYLE=>emphasized_negative
53
expanded_icon = lv_collapse_icon
54
text
= ' - ' ).
55
lo_item = lo_node->get_item( 'CARRID' ).
56
lo_item->set_type( if_salv_c_item_type=>checkbox ).
57
lo_item->set_editable( abap_true ).
58
CATCH cx_salv_msg.
59
ENDTRY.
60
ENDIF.
61
lv_carrid = <fs_line>-carrid.
62 ENDLOOP.
63 ENDMETHOD.
"create_nodes

, c

:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

REPORT zsalv_tree_sample.
FIELD-SYMBOLS: <fs_line> TYPE spfli.
INCLUDE <icon>.
CLASS lcl_report DEFINITION.
PUBLIC SECTION.
CLASS-DATA:
go_alv_tree TYPE REF TO cl_salv_tree,
gt_empty_tab TYPE STANDARD TABLE OF spfli,
gt_full_tab TYPE STANDARD TABLE OF spfli.
CLASS-METHODS:
create_tree, " ALV
get_data,
"
setup_functions,"
create_nodes, " ( )
setup_columns, "
on_link_click FOR EVENT link_click OF cl_salv_events_tree IMPORTING NODE_KEY,
setup_events,
show_tree.
" () ALV
ENDCLASS.
"lcl_report DEFINITION
START-OF-SELECTION.
lcl_report=>get_data( ).
lcl_report=>create_tree( ).
lcl_report=>create_nodes( ).
lcl_report=>setup_functions( ).
lcl_report=>setup_columns( ).
lcl_report=>setup_events( ).
lcl_report=>show_tree( ).
CLASS lcl_report IMPLEMENTATION.

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

METHOD show_tree.
go_alv_tree->display( ).
ENDMETHOD.

"show_tree

METHOD create_nodes.
DATA:
lo_nodes TYPE REF TO cl_salv_nodes,
lo_node TYPE REF TO cl_salv_node,
lv_carrid_key TYPE lvc_nkey,
lv_expand_icon TYPE SALV_DE_TREE_IMAGE,
lv_collapse_icon TYPE SALV_DE_TREE_IMAGE,
lv_hier_icon TYPE SALV_DE_TREE_IMAGE,
lo_item TYPE REF TO cl_salv_item,
lv_count TYPE I,
lo_settings TYPE REF TO cl_salv_tree_settings,
lv_carrid TYPE spfli-carrid.
lv_expand_icon = ICON_CLOSED_FOLDER.
lv_collapse_icon = ICON_OPEN_FOLDER.
lv_hier_icon = icon_tree.
lo_settings = go_alv_tree->get_tree_settings( ).
lo_settings->set_hierarchy_header( '' ).
lo_settings->set_hierarchy_size( 30 ).
lo_settings->set_hierarchy_icon( lv_hier_icon ).
"
lo_nodes = go_alv_tree->get_nodes( ).
SORT gt_full_tab BY carrid.
"
LOOP AT gt_full_tab ASSIGNING <fs_line>.
IF lv_carrid <> <fs_line>-carrid.
TRY.
lv_count = 0.
LOOP AT gt_full_tab TRANSPORTING NO FIELDS WHERE carrid = <fs_line>-carrid.
lv_count = lv_count + 1.
ENDLOOP.
IF lv_count > 1.
lo_node = lo_nodes->add_node(
related_node = ''
relationship = cl_gui_column_tree=>relat_last_child
data_row
= <fs_line>
collapsed_icon = lv_expand_icon
expanded_icon = lv_collapse_icon
row_style
= IF_SALV_C_TREE_STYLE=>emphasized_a
text
= ' ' ).
ELSE.
lo_node = lo_nodes->add_node(
related_node = ''
relationship = cl_gui_column_tree=>relat_last_child
data_row
= <fs_line>
collapsed_icon = lv_collapse_icon
row_style
= IF_SALV_C_TREE_STYLE=>emphasized_a
text
= ' ' ).
ENDIF.
lv_carrid_key = lo_node->get_key( ).
IF lv_count > 1.
lo_item = lo_node->get_item( 'CARRID' ).
lo_item->set_type( if_salv_c_item_type=>button ).
lo_item->set_value( '' ).
ENDIF.
lo_item = lo_node->get_item( 'CITYFROM' ).
lo_item->set_font( if_salv_c_item_font=>fixed_size ).
lo_item->set_enabled( abap_false ).
CATCH cx_salv_msg.

100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166

ENDTRY.
ELSE.
TRY.
lo_node = lo_nodes->add_node(
related_node = lv_carrid_key
relationship = cl_gui_column_tree=>relat_last_child
data_row
= <fs_line>
row_style
= IF_SALV_C_TREE_STYLE=>emphasized_negative
text
= ' - ' ).
lo_item = lo_node->get_item( 'CARRID' ).
lo_item->set_type( if_salv_c_item_type=>checkbox ).
lo_item->set_editable( abap_true ).
CATCH cx_salv_msg.
ENDTRY.
ENDIF.
lv_carrid = <fs_line>-carrid.
ENDLOOP.
ENDMETHOD.
"create_nodes
METHOD get_data.
SELECT * FROM spfli INTO CORRESPONDING FIELDS OF TABLE gt_full_tab.
ENDMETHOD.
"get_data
METHOD create_tree.
TRY.
cl_salv_tree=>factory(
IMPORTING
r_salv_tree = go_alv_tree
CHANGING
t_table = gt_empty_tab ).
CATCH cx_salv_error.
MESSAGE ' ALV' TYPE 'E'.
ENDTRY.
ENDMETHOD.
"create_tree
METHOD setup_functions.
go_alv_tree->set_screen_status(
pfstatus
= 'SALV_STANDARD'
REPORT
= sy-repid "
set_functions = go_alv_tree->c_functions_all ).
ENDMETHOD.
METHOD setup_columns.
DATA:
lo_columns TYPE REF TO cl_salv_columns_tree,
lo_column TYPE REF TO cl_salv_column_tree.
TRY.
lo_columns = go_alv_tree->get_columns( ).
lo_columns->set_optimize( abap_true ).
lo_column ?= lo_columns->get_column( 'MANDT' ).
lo_column->set_technical( abap_true ).
lo_column ?= lo_columns->get_column( 'FLTYPE' ).
lo_column->set_technical( abap_true ).
lo_column ?= lo_columns->get_column( 'PERIOD' ).
lo_column->set_technical( abap_true ).
CATCH cx_salv_msg cx_salv_not_found.
ENDTRY.
ENDMETHOD.
METHOD on_link_click.
DATA: lo_nodes
TYPE REF TO cl_Salv_nodes,
lo_main_node TYPE REF TO cl_salv_node,
lo_item
TYPE REF TO cl_salv_item,
lo_child_node TYPE REF TO cl_salv_node.
lo_nodes = go_alv_tree->get_nodes( ).

167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195

TRY.
lo_main_node = lo_nodes->get_node( node_key ).
lo_child_node = lo_main_node->get_first_child( ).
WHILE lo_child_node IS BOUND.
lo_item = lo_child_node->get_item( 'CARRID' ).
IF lo_item->is_checked( ) = abap_true.
lo_item->set_checked( abap_false ).
ELSE.
lo_item->set_checked( abap_true ).
ENDIF.
lo_child_node = lo_child_node->get_next_sibling( ).
ENDWHILE.
CATCH cx_salv_msg.
EXIT.
ENDTRY.
ENDMETHOD.
METHOD setup_events.
DATA: lo_events TYPE REF TO cl_salv_events_tree.
lo_events = go_alv_tree->get_event( ).
SET HANDLER on_link_click FOR lo_events.
ENDMETHOD.
ENDCLASS.

"lcl_report IMPLEMENTATION

- See more at: http://abap-blog.ru/abap/abap-objects/rabota-s-salv-tree/#sthash.wFolN3zW.dpuf