ABAP Code: Append/ Delete Row in ALV

This sample program illustrates how to add and remove lines to a table using the ALV Grid Control and how to implement the saving of the new data.

In order to implement such requirement, here are the steps to undertake:

1.Lock your database table accordingly;
2.Set status of columns to editable;
3.Set all cells of the table non-editable by using the style table;
4.Define internal tables to remember inserted and deleted lines,
thus the delta between input made after the last saving;
5.Check if there exist already other records with equal key fields;
6.Use protocol attributes MT_DELETED_ROWS and MT_INSERTED_ROWS
to remember which lines where deleted or inserted. Save this
information in your internal tables;
7.Check if any errors exist in protocol by using method
CHECK_CHANGED_DATA of your ALV Grid instance;
8.When all values are valid, use your internal tables to update your table on the database;
9.Refresh your internal tables;
10.Unlock your database table;


Sample Code:


  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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
* Classes ****************************************

class screen_init definition create private.
  public section.
    class-methods init_screen.
    methods constructor.

  private section.
    data: splitter_h type ref to cl_gui_splitter_container,
          splitter_v type ref to cl_gui_splitter_container,
          picture type ref to cl_gui_picture,
          tree type ref to cl_gui_simple_tree.
    methods: fill_tree,
             fill_picture.
endclass.


class screen_handler definition.
  public section.
    methods: constructor importing container
               type ref to cl_gui_container,
             handle_node_double_click
               for event node_double_click
               of cl_gui_simple_tree
               importing node_key.
  private section.

    data: html_viewer type ref to cl_gui_html_viewer,
          list_viewer type ref to cl_gui_alv_grid.

    methods: fill_html importing carrid type spfli-carrid,
             fill_list importing carrid type spfli-carrid
                                 connid type spfli-connid.
endclass.


class screen_init implementation.
  method init_screen.
    data screen type ref to screen_init.
    create object screen.
  endmethod.

  method constructor.
    data: events type cntl_simple_events,
          event like line of events,
          event_handler type ref to screen_handler,
          container_left type ref to cl_gui_container,
          container_right type ref to cl_gui_container,
          container_top type ref to cl_gui_container,
          container_bottom type ref to cl_gui_container.

    create object splitter_h
           exporting
           parent = cl_gui_container=>screen0
           rows = 1
           columns = 2.

    call method splitter_h->set_border
         exporting border = cl_gui_cfw=>false.

    call method splitter_h->set_column_mode
         exporting mode = splitter_h->mode_absolute.

    call method splitter_h->set_column_width
         exporting id = 1
         width = 107.

    container_left  = splitter_h->get_container( row = 1 column = 1 ).

    container_right = splitter_h->get_container( row = 1 column = 2 ).

    create object splitter_v
           exporting
           parent = container_left
           rows = 2
           columns = 1.

    call method splitter_v->set_border
         exporting border = cl_gui_cfw=>false.

    call method splitter_v->set_row_mode
         exporting mode = splitter_v->mode_absolute.

    call method splitter_v->set_row_height
         exporting id = 1
         height = 160.

    container_top    = splitter_v->get_container( row = 1 column = 1 ).

    container_bottom = splitter_v->get_container( row = 2 column = 1 ).

    create object picture
           exporting parent = container_top.

    create object tree
           exporting parent = container_bottom                     node_selection_mode =
                            cl_gui_simple_tree=>node_sel_mode_single.
    create object  event_handler
           exporting container = container_right.

    event-eventid = cl_gui_simple_tree=>eventid_node_double_click.

    event-appl_event = ' '.   "system event, does not trigger PAI
    append event to events.
    call method tree->set_registered_events
         exporting events = events.
    set handler event_handler->handle_node_double_click for tree.

    call method: me->fill_picture,
                 me->fill_tree.
  endmethod.

  method fill_picture.
    types pict_line(256) type x.
    data  pict_tab type table of pict_line.
    data  url(255) type c.

    import pict_tab = pict_tab from database abtree(pi) id 'FLIGHTS'.

    call function 'DP_CREATE_URL'

         exporting

              type    = 'IMAGE'

              subtype = 'GIF'

         tables

              data    = pict_tab

         changing

              url     = url.


    call method picture->load_picture_from_url exporting url = url.

    call method picture->set_display_mode

         exporting display_mode = picture->display_mode_fit_center.

  endmethod.


  method fill_tree.

    data: node_table type table of abdemonode,

          node type abdemonode,

          spfli_wa type spfli,

          spfli_tab type sorted table of spfli

                    with unique key carrid connid.


    select carrid connid

      from spfli

      into corresponding fields of table spfli_tab.


    node-hidden = ' '.                 " All nodes are visible,

    node-disabled = ' '.               " selectable,

    node-isfolder = 'X'.                                    " a folder,

    node-expander = ' '.               " have no '+' sign for expansion.

    loop at spfli_tab into spfli_wa.

      at new carrid.

        node-node_key = spfli_wa-carrid.

        clear node-relatkey.

        clear node-relatship.

        node-text = spfli_wa-carrid.

        node-n_image =   ' '.

        node-exp_image = ' '.

        append node to node_table.

      endat.

      at new connid.

        concatenate spfli_wa-carrid spfli_wa-connid into node-node_key.

        node-relatkey = spfli_wa-carrid.

        node-relatship = cl_gui_simple_tree=>relat_last_child.

        node-text = spfli_wa-connid.

        node-n_image =   '@AV@'.       "AV is the internal code

        node-exp_image = '@AV@'.       "for an airplane icon

      endat.

      append node to node_table.

    endloop.


    call method tree->add_nodes

         exporting table_structure_name = 'ABDEMONODE'

                   node_table = node_table.

  endmethod.


endclass.


*


class screen_handler implementation.


  method constructor.

    create object: html_viewer exporting parent = container,

                   list_viewer exporting i_parent = container.

  endmethod.


  method handle_node_double_click.

    data: carrid type spfli-carrid,

          connid type spfli-connid.


    carrid = node_key(2).

    connid = node_key+2(4).

    if connid is initial.

      call method: fill_html exporting carrid = carrid,

                   html_viewer->set_visible exporting visible = 'X',

                   list_viewer->set_visible exporting visible = ' '.

    else.

      call method: fill_list exporting carrid = carrid

                                       connid = connid,

                   list_viewer->set_visible exporting visible = 'X',

                   html_viewer->set_visible exporting visible = ' '.

    endif.


    call method cl_gui_cfw=>flush.

  endmethod.


  method fill_html.

    data url type scarr-url.


    select single url

    from   scarr

    into   url

    where  carrid = carrid.


    call method html_viewer->show_url exporting url = url.

  endmethod.


  method fill_list.

    data: flight_tab type table of demofli,

          begin of flight_title,

            carrname type scarr-carrname,

            cityfrom type spfli-cityfrom,

            cityto   type spfli-cityto,

          end of flight_title,

          list_layout type lvc_s_layo.


    select   single c~carrname p~cityfrom p~cityto

    into     corresponding fields of flight_title

    from     ( scarr as c

               inner join spfli   as p on c~carrid = p~carrid )

    where    p~carrid = carrid and

             p~connid = connid.


    select   fldate seatsmax seatsocc

    into     corresponding fields of table flight_tab

    from     sflight

    where    carrid = carrid and connid = connid

    order by fldate.


    concatenate flight_title-carrname

                connid

                flight_title-cityfrom

                flight_title-cityto

                into list_layout-grid_title separated by space.


    list_layout-smalltitle = 'X'.      "The list title has small fonts,

    list_layout-cwidth_opt = 'X'.      "the column width is adjusted,

    list_layout-no_toolbar = 'X'.      "the toolbar is suppressed.


    call method list_viewer->set_table_for_first_display

         exporting i_structure_name = 'DEMOFLI'

                   is_layout = list_layout

         changing  it_outtab         = flight_tab.

  endmethod.


endclass.



* Program execution ************************************************


load-of-program.

  call screen 100.


* Dialog Modules PBO


module status_0100 output.

  set pf-status 'SCREEN_100'.

  set titlebar 'TIT_100'.

  call method screen_init=>init_screen.

endmodule.


* Dialog Modules PAI


module cancel input.

  leave program.

endmodule.

No comments:

Post a Comment

How to download the data structure of tables in SE11?

Good morning people around the world. Its me again doing some blogs. Sharing to you some tips and tricks in the SAP world. Well, before I s...