How to get the updated data from a editable ALV (CL_GUI_ALV_GRID)

This program demonstrates how to get the updated data when a button is clicked in the GUI toolbar or ALV toolbar. The ALV is created using CL_GUI_ALV_GRID.

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
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
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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
*&---------------------------------------------------------------------*
*& Report ZZSOURAV_REFRESH_ALV
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT zzsourav_refresh_alv.
DATA: ok_code TYPE syucomm.
*----------------------------------------------------------------------*
* CLASS main DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS main DEFINITION.
PUBLIC SECTION.
DATA: i_sbook TYPE STANDARD TABLE OF sbook INITIAL SIZE 0,
i_sbook_old TYPE STANDARD TABLE OF sbook INITIAL SIZE 0.

METHODS:constructor,
handle_toolbar FOR EVENT toolbar OF cl_gui_alv_grid
IMPORTING e_object e_interactive,
handle_user_command FOR EVENT
user_command OF cl_gui_alv_grid
IMPORTING e_ucomm,
check_changed_data.
PROTECTED SECTION.


PRIVATE SECTION.
TYPE-POOLS: icon.
DATA:container TYPE REF TO cl_gui_custom_container,
oref_alv TYPE REF TO cl_gui_alv_grid,
wa_layout TYPE lvc_s_layo,
i_fieldcatalog TYPE lvc_t_fcat.
METHODS:get_field_catalog,get_layout.
ENDCLASS. "main DEFINITION
*----------------------------------------------------------------------*
* CLASS main IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS main IMPLEMENTATION.
METHOD constructor.

SELECT * FROM sbook INTO TABLE i_sbook
UP TO 50 ROWS.
i_sbook_old[] = i_sbook[].

CREATE OBJECT container
EXPORTING
* parent =
container_name = 'CONT1'
* style =
* lifetime = lifetime_default
repid = sy-repid
dynnr = '0100'
* no_autodef_progid_dynnr =
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5
OTHERS = 6
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.


CREATE OBJECT oref_alv
EXPORTING
* i_shellstyle = 0
* i_lifetime =
i_parent = container
* i_appl_events = space
* i_parentdbg =
* i_applogparent =
* i_graphicsparent =
* i_name =
* i_fcat_complete = space
EXCEPTIONS
error_cntl_create = 1
error_cntl_init = 2
error_cntl_link = 3
error_dp_create = 4
OTHERS = 5
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

CALL METHOD me->get_field_catalog.
CALL METHOD me->get_layout.

CALL METHOD oref_alv->set_table_for_first_display
EXPORTING
* i_buffer_active =
* i_bypassing_buffer =
* i_consistency_check =
* i_structure_name =
* is_variant =
* i_save =
* i_default = 'X'
is_layout = wa_layout
* is_print =
* it_special_groups =
* it_toolbar_excluding =
* it_hyperlink =
* it_alv_graphics =
* it_except_qinfo =
* ir_salv_adapter =
CHANGING
it_outtab = i_sbook
it_fieldcatalog = i_fieldcatalog
* it_sort =
* it_filter =
EXCEPTIONS
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3
OTHERS = 4
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
SET HANDLER me->handle_toolbar FOR oref_alv.
SET HANDLER me->handle_user_command FOR oref_alv.

CALL METHOD oref_alv->set_toolbar_interactive.

ENDMETHOD. "constructor
METHOD get_layout.
wa_layout-cwidth_opt = 'X'.
ENDMETHOD. "get_layout
METHOD get_field_catalog.
FIELD-SYMBOLS: <lf1> TYPE lvc_s_fcat.

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
* I_BUFFER_ACTIVE =
i_structure_name = 'SBOOK'
i_client_never_display = 'X'
* I_BYPASSING_BUFFER =
i_internal_tabname = 'I_SBOOK'
CHANGING
ct_fieldcat = i_fieldcatalog
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

LOOP AT i_fieldcatalog ASSIGNING <lf1>.
IF <lf1>-fieldname = 'SMOKER'.
<
lf1>-edit = 'X'. " REUSE_ALV_GRID_DISPLAY
ENDIF.
ENDLOOP.
ENDMETHOD. "get_field_catalog
METHOD handle_toolbar.
DATA: l_toolbar TYPE stb_button.
CLEAR l_toolbar.
* append an icon to show booking table
CLEAR l_toolbar.
MOVE 'SAVE' TO l_toolbar-function.
MOVE icon_system_save TO l_toolbar-icon.
MOVE 'Save' TO l_toolbar-quickinfo.
MOVE 'Save' TO l_toolbar-text.
MOVE ' ' TO l_toolbar-disabled.
APPEND l_toolbar TO e_object->mt_toolbar.

ENDMETHOD. "handle_toolbar
METHOD handle_user_command.
CASE e_ucomm.
WHEN 'SAVE'.
IF i_sbook[] <> i_sbook_old[].
MESSAGE i001(00) WITH 'You have changed some data'.
ELSE.
MESSAGE i001(00) WITH 'No data changed'.
ENDIF.
ENDCASE.
ENDMETHOD. "user_command
METHOD check_changed_data.

CALL METHOD oref_alv->check_changed_data
* IMPORTING
* e_valid =
* CHANGING
* c_refresh = 'X'
.


ENDMETHOD. "CHECK_CHANGED_DATA
ENDCLASS. "main IMPLEMENTATION

START-OF-SELECTION.

DATA: oref_main TYPE REF TO main.
CREATE OBJECT oref_main.

CALL SCREEN 0100.
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
SET PF-STATUS 'S0100'.
* SET TITLEBAR 'xxx'.

ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.

CASE ok_code.
WHEN 'BACK'.

SET SCREEN 00.
LEAVE SCREEN.
WHEN 'SAVE'.
" Check this part if the button is SAP standard toolbar
CALL METHOD oref_main->check_changed_data.
IF oref_main->i_sbook[] NE oref_main->i_sbook_old[].
MESSAGE i001(00) WITH 'You have changed some data'.
ELSE.
MESSAGE i001(00) WITH 'No data changed'.
ENDIF.

ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT


Initial ALV display with editable column:



When any of the "SAVE" button is clicked but no changed data:



When something is changed and any of the "SAVE" button is clicked:

1 comment:

Engin Akdeniz said...

Nice Post. Thans you help me a lot.

Post a Comment