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.

ABAP Code: Editable ALV

This report illustrates how to set chosen cells of an ALV Grid Control editable. Below are the essential steps undertaken:

1.Extend your output table for a field, e.g., CELLTAB, that holds information about the edit status of each cell for the corresponding row (the table type is SORTED!).
2. After selecting data, set edit status for each row in a loop according to field SEATSMAX.
   a.Use attribute CL_GUI_ALV_GRID=>MC_STYLE_ENABLED to set a cell to status    "editable".
   b.Use attribute CL_GUI_ALV_GRID=>MC_STYLE_DISABLED to set a cell to status "non-editable".
  c.Copy your celltab to the celltab of the current row of gt_outtab.

3.Provide the fieldname of the celltab field by using field STYLEFNAME of the layout structure.


Sample Code:

data: ok_code like sy-ucomm,
      save_ok like sy-ucomm,
      g_container type scrfname value 'BCALV_GRID_DEMO_0100_CONT1',
      grid1  type ref to cl_gui_alv_grid,
      g_custom_container type ref to cl_gui_custom_container,
      gs_layout type lvc_s_layo.
*      g_max type i value 500.

*§1.Extend your output table for a field, e.g., CELLTAB, that holds
*   information about the edit status of each cell for the
*   corresponding row (the table type is SORTED!).
databegin of gt_outtab occurs 0"with header line
        count type int2.
        include structure sflight.
data: celltab type lvc_t_styl.
dataend of gt_outtab.

data: gs_fieldcat type lvc_s_fcat,
      gt_fieldcat type lvc_t_fcat.

parameters: g_max type i default '600',
            g_rstyle type char1 as checkbox,
            g_rowins type char1 as checkbox"default 'X'.  "key active

*---------------------------------------------------------------------*
*       MAIN                                                          *
*---------------------------------------------------------------------*
call screen 100.

*---------------------------------------------------------------------*
*       MODULE PBO OUTPUT                                             *
*---------------------------------------------------------------------*
module pbo output.
  set pf-status 'MAIN100'.
  set titlebar 'MAIN100'.
  if g_custom_container is initial.
    create object g_custom_container
      exporting
        container_name = g_container.
    create object grid1
      exporting
        i_parent = g_custom_container.

* select data, set styles
    perform select_data_and_init_style.

*§3.Provide the fieldname of the celltab field by using field
*   STYLEFNAME of the layout structure.
    gs_layout-stylefname = 'CELLTAB'.
    gs_layout-sel_mode   = 'D'.
* short key 'Delete row' active?
    if g_rowins is not initial.
      gs_layout-no_rowins = 'X'"not allowed
    endif.

* fieldcat
    perform set_fieldcat using gt_fieldcat.

    call method grid1->set_table_for_first_display
      exporting "i_structure_name = 'SFLIGHT'
        is_layout       = gs_layout
      changing
        it_outtab       = gt_outtab[]
        it_fieldcatalog = gt_fieldcat.

  endif.
endmodule.
*---------------------------------------------------------------------*
*       MODULE PAI INPUT                                              *
*---------------------------------------------------------------------*
module pai input.
  save_ok = ok_code.
  clear ok_code.
  case save_ok.
    when 'EXIT'.
      perform exit_program.
    when 'SWITCH'.
      perform switch_edit_mode.
    when others.
*     do nothing
  endcase.
endmodule.
*---------------------------------------------------------------------*
*       FORM EXIT_PROGRAM                                             *
*---------------------------------------------------------------------*
form exit_program.
  set screen 0.
  leave screen.
endform.
*&---------------------------------------------------------------------*
*&      Form  SELECT_DATA_AND_INIT_STYLE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form select_data_and_init_style.
  data: lt_sflight type table of sflight with header line,
        lt_celltab type lvc_t_styl,
        l_index type i.

  select * from sflight into table lt_sflight up to g_max rows.
* move corresponding fields from lt_sflight to gt_outtab
  loop at lt_sflight.
    move-corresponding lt_sflight to gt_outtab.
    append gt_outtab.
  endloop.

*§2.After selecting data, set edit status for each row in a loop
*   according to field SEATSMAX.
  loop at gt_outtab.
    l_index = sy-tabix.
    gt_outtab-count = l_index.
    refresh lt_celltab.
    if gt_outtab-seatsmax ge 300.
      perform fill_celltab using 'RW'
                                 l_index
                           changing lt_celltab.
    else.
      perform fill_celltab using 'RO'
                                 l_index
                           changing lt_celltab.
    endif.
*§2c.Copy your celltab to the celltab of the current row of gt_outtab.
    insert lines of lt_celltab into table gt_outtab-celltab.
    modify gt_outtab index l_index.
  endloop.
endform.                               " SELECT_DATA_AND_INIT_STYLE
*&---------------------------------------------------------------------*
*&      Form  FILL_CELLTAB
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_PT_CELLTAB  text
*----------------------------------------------------------------------*
form fill_celltab using value(p_mode) l_index
                  changing pt_celltab type lvc_t_styl.

  data: ls_celltab type lvc_s_styl,
        l_index1 type i,
        l_mode type raw4,
        l_rowstyle(1type c.
* This forms sets the style of column 'PRICE' editable
* according to 'p_mode' and the rest to read only either way.

  if p_mode eq 'RW'.   ">300
*§2a.Use attribute CL_GUI_ALV_GRID=>MC_STYLE_ENABLED to set a cell
*    to status "editable".
    l_mode = cl_gui_alv_grid=>mc_style_enabled.
  else"p_mode eq 'RO'   "<300
*§2b.Use attribute CL_GUI_ALV_GRID=>MC_STYLE_DISABLED to set a cell
*    to status "non-editable".
    l_mode = cl_gui_alv_grid=>mc_style_disabled.
  endif.


* is row style selected?
  l_rowstyle = g_rstyle.
  l_index1   = l_index.

* define rowstyle for single rows:
  if l_rowstyle is not initial.
    clear ls_celltab.

    if l_index1 <= '100'.
      ls_celltab-fieldname = ' '.
      ls_celltab-style = cl_gui_alv_grid=>mc_style_no_delete_row. " row not deleted
      insert ls_celltab into table pt_celltab.

* define editablility of single cells:
      ls_celltab-fieldname = 'CARRID'.
      ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.
      insert ls_celltab into table pt_celltab.
      ls_celltab-fieldname = 'CONNID'.
      ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.
      insert ls_celltab into table pt_celltab.
      ls_celltab-fieldname = 'FLDATE'.
      ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.
      insert ls_celltab into table pt_celltab.
      ls_celltab-fieldname = 'PRICE'.
      ls_celltab-style = l_mode.    "depending on value seatmax
      insert ls_celltab into table pt_celltab.
      ls_celltab-fieldname = 'CURRENCY'.
      ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.
      insert ls_celltab into table pt_celltab.
      ls_celltab-fieldname = 'PLANETYPE'.
      ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.
      insert ls_celltab into table pt_celltab.
      ls_celltab-fieldname = 'SEATSMAX'.
      ls_celltab-style = l_mode.
      insert ls_celltab into table pt_celltab.
      ls_celltab-fieldname = 'SEATSOCC'.
      ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.
      insert ls_celltab into table pt_celltab.
      ls_celltab-fieldname = 'PAYMENTSUM'.
      ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.
      insert ls_celltab into table pt_celltab.
    else.
*  rows can be deleted
      if l_index1 > 100 and l_index1 <= 200.
* do nothing for row style  and cell style
* set cell style:
        ls_celltab-fieldname = 'CARRID'.
        ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.
        insert ls_celltab into table pt_celltab.
        ls_celltab-fieldname = 'CONNID'.
        ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.
        insert ls_celltab into table pt_celltab.
        ls_celltab-fieldname = 'FLDATE'.
        ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.
        insert ls_celltab into table pt_celltab.
        ls_celltab-fieldname = 'PRICE'.
        ls_celltab-style = l_mode.    "depending on value seatmax
        insert ls_celltab into table pt_celltab.
        ls_celltab-fieldname = 'CURRENCY'.
        ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.
        insert ls_celltab into table pt_celltab.
        ls_celltab-fieldname = 'PLANETYPE'.
        ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.
        insert ls_celltab into table pt_celltab.
        ls_celltab-fieldname = 'SEATSMAX'.
        ls_celltab-style = l_mode.
        insert ls_celltab into table pt_celltab.
        ls_celltab-fieldname = 'SEATSOCC'.
        ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.
        insert ls_celltab into table pt_celltab.
        ls_celltab-fieldname = 'PAYMENTSUM'.
        ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.
        insert ls_celltab into table pt_celltab.
      else.
        if l_index1 > 200 and l_index1 <= 250.
* everything is editable: > 300, rows can not be deleted
           ls_celltab-fieldname = ' '.
           ls_celltab-style = cl_gui_alv_grid=>mc_style_no_delete_row. " row not deleted
           insert ls_celltab into table pt_celltab.

           ls_celltab-fieldname = 'CARRID'.
           ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
           insert ls_celltab into table pt_celltab.
           ls_celltab-fieldname = 'CONNID'.
           ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
           insert ls_celltab into table pt_celltab.
           ls_celltab-fieldname = 'FLDATE'.
           ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
           insert ls_celltab into table pt_celltab.
           ls_celltab-fieldname = 'PRICE'.
           ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
           insert ls_celltab into table pt_celltab.
           ls_celltab-fieldname = 'CURRENCY'.
           ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
           insert ls_celltab into table pt_celltab.
           ls_celltab-fieldname = 'PLANETYPE'.
           ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
           insert ls_celltab into table pt_celltab.
           ls_celltab-fieldname = 'SEATSMAX'.
           ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
           insert ls_celltab into table pt_celltab.
           ls_celltab-fieldname = 'SEATSOCC'.
           ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
           insert ls_celltab into table pt_celltab.
           ls_celltab-fieldname = 'PAYMENTSUM'.
           ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
           insert ls_celltab into table pt_celltab.
         else.  "BT 250 and 600  rows can be deleted.
           ls_celltab-fieldname = 'CARRID'.
           ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
           insert ls_celltab into table pt_celltab.
           ls_celltab-fieldname = 'CONNID'.
           ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
           insert ls_celltab into table pt_celltab.
           ls_celltab-fieldname = 'FLDATE'.
           ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
           insert ls_celltab into table pt_celltab.
           ls_celltab-fieldname = 'PRICE'.
           ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
           insert ls_celltab into table pt_celltab.
           ls_celltab-fieldname = 'CURRENCY'.
           ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
           insert ls_celltab into table pt_celltab.
           ls_celltab-fieldname = 'PLANETYPE'.
           ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
           insert ls_celltab into table pt_celltab.
           ls_celltab-fieldname = 'SEATSMAX'.
           ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
           insert ls_celltab into table pt_celltab.
           ls_celltab-fieldname = 'SEATSOCC'.
           ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
           insert ls_celltab into table pt_celltab.
           ls_celltab-fieldname = 'PAYMENTSUM'.
           ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
           insert ls_celltab into table pt_celltab.
          endif.

      endif.
    endif.
  else.
*without any row style: rows can be deleted
    ls_celltab-fieldname = 'CARRID'.
    ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
    insert ls_celltab into table pt_celltab.
    ls_celltab-fieldname = 'CONNID'.
    ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
    insert ls_celltab into table pt_celltab.
    ls_celltab-fieldname = 'FLDATE'.
    ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
    insert ls_celltab into table pt_celltab.
    ls_celltab-fieldname = 'PRICE'.
    ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.   "depending on value seatmax
    insert ls_celltab into table pt_celltab.
    ls_celltab-fieldname = 'CURRENCY'.
    ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
    insert ls_celltab into table pt_celltab.
    ls_celltab-fieldname = 'PLANETYPE'.
    ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
    insert ls_celltab into table pt_celltab.
    ls_celltab-fieldname = 'SEATSMAX'.
    ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
    insert ls_celltab into table pt_celltab.
    ls_celltab-fieldname = 'SEATSOCC'.
    ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
    insert ls_celltab into table pt_celltab.
    ls_celltab-fieldname = 'PAYMENTSUM'.
    ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
    insert ls_celltab into table pt_celltab.
  endif.

endform.                               " FILL_CELLTAB
*&---------------------------------------------------------------------*
*&      Form  SWITCH_EDIT_MODE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form switch_edit_mode.

  if grid1->is_ready_for_input( ) eq 0.

* set edit enabled cells ready for input
    call method grid1->set_ready_for_input
      exporting
        i_ready_for_input = 1.

  else.
* lock edit enabled cells against input
    call method grid1->set_ready_for_input
      exporting
        i_ready_for_input = 0.

  endif.
endform.                               " SWITCH_EDIT_MODE
*&---------------------------------------------------------------------*
*&      Form  SET_FIELDCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GT_FIELDCAT  text
*----------------------------------------------------------------------*
form set_fieldcat  using    rt_fieldcat type lvc_t_fcat.

  data: ls_fieldcat type lvc_s_fcat,
        lt_fieldcat type lvc_t_fcat,
        n type i.                                           "#EC NEEDED

  call function 'LVC_FIELDCATALOG_MERGE'
   exporting
     i_structure_name             = 'SFLIGHT'
    changing
      ct_fieldcat                  = gt_fieldcat
*    EXCEPTIONS
*      INCONSISTENT_INTERFACE       = 1
*      PROGRAM_ERROR                = 2
*      OTHERS                       = 3
            .
  if sy-subrc <> 0.    "#EC *
* Implement suitable error handling here
  endif.

  lt_fieldcat = rt_fieldcat.

* add count field
  ls_fieldcat-fieldname = 'COUNT'.
  ls_fieldcat-inttype   = 'I'.
  ls_fieldcat-datatype  = 'INT2'.
  ls_fieldcat-scrtext_s = 'Count'.                          "#EC NOTEXT
  ls_fieldcat-scrtext_m = 'Count'.                          "#EC NOTEXT
  ls_fieldcat-scrtext_l = 'Counter'.                        "#EC NOTEXT
  ls_fieldcat-col_pos = '1'.
  ls_fieldcat-col_opt = 'X'.
  ls_fieldcat-intlen    = 2.
  ls_fieldcat-outputlen = 2.

  append ls_fieldcat to lt_fieldcat.

  rt_fieldcat = lt_fieldcat.

endform.                    " SET_FIELDCAT




Output:

ABAP Code: How to Implement ALV Tree?

This report program is based on the standard SAP program BCALV_TREE_01. It shows the essential steps to build up a hierarchy using an ALV Tree Control (class CL_GUI_ALV_TREE). 

In this program, the hierarchy tree consists of nodes for each month on top level (this level can not be build by a simple ALV Tree because there is no field for months in our output table SFLIGHT. Thus, you can not define this hierarchy by sorting nor initial calculations neither a special layout has been applied. Note also that this example does not build up and change the fieldcatalog of the output table. For this reason, all fields of the output table are shown in the columns although the fields CARRID and FLDATE are already placed in the tree on the left.

Steps:
1.Usual steps when using control technology.

  • Define reference variables.
  • Create ALV Tree Control and corresponding container.
2.Create Hierarchy-header
3.Create empty Tree Control
4.Create hierarchy (nodes and leaves)

  • Select data
  • Sort output table according to your conceived hierarchy
  • Add data to tree
5.Send data to frontend.
6.Call dispatch to process toolbar functions 

Complete Source 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
* ァ1a. Define reference variables
data: g_alv_tree         type ref to cl_gui_alv_tree,
      g_custom_container type ref to cl_gui_custom_container.

data: gt_sflight      type sflight occurs 0,      "Output-Table
      ok_code like sy-ucomm,
      save_ok like sy-ucomm,           "OK-Code
      g_max type i value 255.

end-of-selection.

  call screen 100.

*&---------------------------------------------------------------------*
*&      Module  PBO  OUTPUT
*&---------------------------------------------------------------------*
*       process before output
*----------------------------------------------------------------------*
module pbo output.

  set pf-status 'MAIN100'.
  set titlebar 'MAINTITLE'.

  if g_alv_tree is initial.
    perform init_tree.

    call method cl_gui_cfw=>flush
      exceptions
        cntl_system_error = 1
        cntl_error        = 2.
    if sy-subrc ne 0.
      call function 'POPUP_TO_INFORM'
        exporting
          titel = 'Automation Queue failure'(801)
          txt1  = 'Internal error:'(802)
          txt2  = 'A method in the automation queue'(803)
          txt3  = 'caused a failure.'(804).
    endif.
  endif.

endmodule.                             " PBO  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  PAI  INPUT
*&---------------------------------------------------------------------*
*       process after input
*----------------------------------------------------------------------*
module pai input.
  save_ok = ok_code.
  clear ok_code.

  case save_ok.
    when 'EXIT' or 'BACK' or 'CANC'.
      perform exit_program.

    when others.
* ァ6. Call dispatch to process toolbar functions
      call method cl_gui_cfw=>dispatch.

  endcase.

  call method cl_gui_cfw=>flush.
endmodule.                             " PAI  INPUT

*&---------------------------------------------------------------------*
*&      Form  init_tree
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form init_tree.
* ァ1b. Create ALV Tree Control and corresponding Container.

* create container for alv-tree
  data: l_tree_container_name(30) type c.

  l_tree_container_name = 'CCONTAINER1'.

     create object g_custom_container
        exporting
              container_name = l_tree_container_name
        exceptions
              cntl_error                  = 1
              cntl_system_error           = 2
              create_error                = 3
              lifetime_error              = 4
              lifetime_dynpro_dynpro_link = 5.
    if sy-subrc <> 0.
      message x208(00) with 'ERROR'(100).
    endif.

* create tree control
  create object g_alv_tree
    exporting
        parent              = g_custom_container
        node_selection_mode = cl_gui_column_tree=>node_sel_mode_single
        item_selection      = 'X'
        no_html_header      = 'X'
        no_toolbar          = ''
    exceptions
        cntl_error                   = 1
        cntl_system_error            = 2
        create_error                 = 3
        lifetime_error               = 4
        illegal_node_selection_mode  = 5
        failed                       = 6
        illegal_column_name          = 7.
  if sy-subrc <> 0.
    message x208(00) with 'ERROR'.                          "#EC NOTEXT
  endif.

* ァ2. Create Hierarchy-header
* The simple ALV Tree uses the text of the fields which were used
* for sorting to define this header. When you use
* the 'normal' ALV Tree the hierarchy is build up freely
* by the programmer this is not possible, so he has to define it
* himself.
  data l_hierarchy_header type treev_hhdr.
  perform build_hierarchy_header changing l_hierarchy_header.

* ァ3. Create empty Tree Control
* IMPORTANT: Table 'gt_sflight' must be empty. Do not change this table
* (even after this method call). You can change data of your table
* by calling methods of CL_GUI_ALV_TREE.
* Furthermore, the output table 'gt_outtab' must be global and can
* only be used for one ALV Tree Control.
  call method g_alv_tree->set_table_for_first_display
    exporting
      i_structure_name    = 'SFLIGHT'
      is_hierarchy_header = l_hierarchy_header
    changing
      it_outtab           = gt_sflight. "table must be empty !

* ァ4. Create hierarchy (nodes and leaves)
  perform create_hierarchy.

* ァ5. Send data to frontend.
  call method g_alv_tree->frontend_update.

* wait for automatic flush at end of pbo
endform.                               " init_tree
*&---------------------------------------------------------------------*
*&      Form  build_hierarchy_header
*&---------------------------------------------------------------------*
*       build hierarchy-header-information
*----------------------------------------------------------------------*
*      -->P_L_HIERARCHY_HEADER  strucxture for hierarchy-header
*----------------------------------------------------------------------*
form build_hierarchy_header changing
                               p_hierarchy_header type treev_hhdr.

  p_hierarchy_header-heading = 'Month/Carrier/Date'(300).
  p_hierarchy_header-tooltip = 'Flights in a month'(400).
  p_hierarchy_header-width = 30.
  p_hierarchy_header-width_pix = ' '.

endform.                               " build_hierarchy_header
*&---------------------------------------------------------------------*
*&      Form  exit_program
*&---------------------------------------------------------------------*
*       free object and leave program
*----------------------------------------------------------------------*
form exit_program.

  call method g_custom_container->free.
  leave program.

endform.                               " exit_program
*&---------------------------------------------------------------------*
*&      Form  create_hierarchy
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form create_hierarchy.

  data: ls_sflight type sflight,
        lt_sflight type sflight occurs 0,
        l_yyyymm(6) type c,            "year and month of sflight-fldate
        l_yyyymm_last(6) type c,
        l_carrid like sflight-carrid,
        l_carrid_last like sflight-carrid.

  data: l_month_key type lvc_nkey,
        l_carrid_key type lvc_nkey,
        l_last_key type lvc_nkey.

* ァ4a. Select data
  select * from sflight into table lt_sflight up to g_max rows.

* ァ4b. Sort output table according to your conceived hierarchy
* We sort in this order:
*    year and month (top level nodes, yyyymm of DATS)
*      carrier id (next level)
*         day of month (leaves, dd of DATS)
  sort lt_sflight by fldate+0(6) carrid fldate+6(2).
* Note: The top level nodes do not correspond to a field of the
* output table. Instead we use data of the table to invent another
* hierarchy level above the levels that can be build by sorting.

* ァ4c. Add data to tree

  loop at lt_sflight into ls_sflight.
* Prerequesite: The table is sorted.
* You add a node everytime the values of a sorted field changes.
* Finally, the complete line is added as a leaf below the last
* node.
    l_yyyymm = ls_sflight-fldate+0(6).
    l_carrid = ls_sflight-carrid.

* Top level nodes:
    if l_yyyymm <> l_yyyymm_last.      "on change of l_yyyymm
      l_yyyymm_last = l_yyyymm.

*Providing no key means that the node is added on top level:
      perform add_month using    l_yyyymm
                                      ''
                             changing l_month_key.
* The month changed, thus, there is no predecessor carrier
      clear l_carrid_last.
    endif.

* Carrier nodes:
* (always inserted as child of the last month
*  which is identified by 'l_month_key')
    if l_carrid <> l_carrid_last.      "on change of l_carrid
      l_carrid_last = l_carrid.
      perform add_carrid_line using    ls_sflight
                                       l_month_key
                              changing l_carrid_key.
    endif.

* Leaf:
* (always inserted as child of the last carrier
*  which is identified by 'l_carrid_key')
    perform add_complete_line using  ls_sflight
                                     l_carrid_key
                            changing l_last_key.
  endloop.

endform.                               " create_hierarchy

*&---------------------------------------------------------------------*
*&      Form  add_month
*&---------------------------------------------------------------------*
form add_month  using     p_yyyymm type c
                          p_relat_key type lvc_nkey
                changing  p_node_key type lvc_nkey.

  data: l_node_text type lvc_value,
        ls_sflight type sflight,
        l_month(15) type c.            "output string for month

* get month name for node text
  perform get_month using p_yyyymm
                    changing l_month.
  l_node_text = l_month.

* add node:
* ALV Tree firstly inserts this node as a leaf if you do not provide
* IS_NODE_LAYOUT with field ISFOLDER set. In form 'add_carrid_line'
* the leaf gets a child and thus ALV converts it to a folder
* automatically.
*
  call method g_alv_tree->add_node
    exporting
      i_relat_node_key = p_relat_key
      i_relationship   = cl_gui_column_tree=>relat_last_child
      i_node_text      = l_node_text
      is_outtab_line   = ls_sflight
    importing
      e_new_node_key   = p_node_key.

endform.                               " add_month
*--------------------------------------------------------------------
form add_carrid_line using     ps_sflight type sflight
                               p_relat_key type lvc_nkey
                     changing  p_node_key type lvc_nkey.

  data: l_node_text type lvc_value,
        ls_sflight type sflight.

* add node
* ALV Tree firstly inserts this node as a leaf if you do not provide
* IS_NODE_LAYOUT with field ISFOLDER set. In form 'add_carrid_line'
* the leaf gets a child and thus ALV converts it to a folder
* automatically.
*
  l_node_text =  ps_sflight-carrid.
  call method g_alv_tree->add_node
    exporting
      i_relat_node_key = p_relat_key
      i_relationship   = cl_gui_column_tree=>relat_last_child
      i_node_text      = l_node_text
      is_outtab_line   = ls_sflight
    importing
      e_new_node_key   = p_node_key.

endform.                               " add_carrid_line
*&---------------------------------------------------------------------*
*&      Form  add_complete_line
*&---------------------------------------------------------------------*
form add_complete_line using   ps_sflight type sflight
                               p_relat_key type lvc_nkey
                     changing  p_node_key type lvc_nkey.

  data: l_node_text type lvc_value.

  write ps_sflight-fldate to l_node_text mm/dd/yyyy.

* add leaf:
* ALV Tree firstly inserts this node as a leaf if you do not provide
* IS_NODE_LAYOUT with field ISFOLDER set.
* Since these nodes will never get children they stay leaves
* (as intended).
*
  call method g_alv_tree->add_node
    exporting
      i_relat_node_key = p_relat_key
      i_relationship   = cl_gui_column_tree=>relat_last_child
      is_outtab_line   = ps_sflight
      i_node_text      = l_node_text
    importing
      e_new_node_key   = p_node_key.

endform.                               " add_complete_line
*&---------------------------------------------------------------------*
*&      Form  GET_MONTH
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_YYYYMM  text
*      <--P_L_MONTH  text
*----------------------------------------------------------------------*
form get_month using    p_yyyymm
               changing p_month.
* Returns the name of month according to the digits in p_yyyymm

  data: l_monthdigits(2) type c.

  l_monthdigits = p_yyyymm+4(2).
  case l_monthdigits.
    when '01'.
      p_month = 'January'(701).
    when '02'.
      p_month = 'February'(702).
    when '03'.
      p_month = 'March'(703).
    when '04'.
      p_month = 'April'(704).
    when '05'.
      p_month = 'May'(705).
    when '06'.
      p_month = 'June'(706).
    when '07'.
      p_month = 'July'(707).
    when '08'.
      p_month = 'August'(708).
    when '09'.
      p_month = 'September'(709).
    when '10'.
      p_month = 'October'(710).
    when '11'.
      p_month = 'November'(711).
    when '12'.
      p_month = 'December'(712).
  endcase.
  concatenate p_yyyymm+0(4) '->' p_month into p_month.

endform.                               " GET_MONTH
*-----------------------------------------------------------------------


Sample Output:

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...