Copyright 2024 - BV TallVision IT

ALV grid as editor - changing fields (Abap Objects). This will allow you to turn your report into an editor with the same look and feel as SM31

When an ALV grid is used, it's also possible to switch to "edit mode" which will perform some drastic measures to the ALV: If a single field (e.g.) is made available for editing, the following changes are done to the ALV:

 

  • All non-editable fields become grey (no pretty blue/green/white colors any more)
  • A whole suite of functions are added to the menu, including "Append row", "Insert row", "Delete row", "Duplicate row". If you decide you don't need this functionality, you'll need to change the menu (for which an AbapcadabrA article is available).
  • The fields that can be changed are editable

Changes will be accepted into the internal table unless you stop them. The idea is that the end user can do whatever changes until the enter key is pressed, at which point the accumulated changes can be checked.

Class CL_ALV_CHANGED_DATA_PROTOCOL will hold all changes.

REPORT Z_DEMO_ALV_STARTINGPOINT.

tables: mara.
data: i_mara type standard table of mara.

data: g_okcode like sy-ucomm,
      my_dock type ref to cl_gui_docking_container,
      my_alv type ref to cl_gui_alv_grid.

class: g_cl_user_interaction definition deferred.

data: g_user_interaction type ref to g_cl_user_interaction.

select-options: so_matnr for mara-matnr.

start-of-selection.

  select * from mara into table i_mara
    up to 50 rows
    where matnr in so_matnr.

  perform initialize_dock_alv.

  call screen 0900.

*-----------------------------------------------------------------------
*      C L A S S   D E F I N I T I O N   &   I M P L E M E N T A T I O N
*-----------------------------------------------------------------------
class g_cl_user_interaction definition.

  public section.

* Respond to change a field contents
    methods change_field for event DATA_CHANGED
      of cl_gui_alv_grid importing ER_DATA_CHANGED.
endclass.

class g_cl_user_interaction implementation.

*---------------------------------------------------------------------
  method change_field.
*---------------------------------------------------------------------
* er_data_changed REF TO CL_ALV_CHANGED_DATA_PROTOCOL
    data l_modification type LVC_S_MODI.
    loop at er_data_changed->MT_MOD_CELLS into l_modification.
* Test the change..
    endloop.

  endmethod.

endclass.

*---------------------------------------------------------------------*
*       MODULE pbo OUTPUT                                             *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
module pbo output.
  set pf-status 'MAIN'.
endmodule.
*---------------------------------------------------------------------*
*       MODULE pai                                                    *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
module pai.
  case g_okcode.
    when 'BACK'.
      leave to screen 0.
  endcase.
endmodule.

*---------------------------------------------------------------------*
*       FORM initialize_dock_alv                                      *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
form initialize_dock_alv.

  data: i_fieldcatalog_local type lvc_t_fcat,
        lo_fieldcatalog type lvc_s_fcat.

  CREATE OBJECT my_dock
    EXPORTING
      REPID = 'Z_DEMO_ALV_STARTINGPOINT'
      DYNNR = '0900'
      RATIO = 95.

  CREATE OBJECT MY_ALV
    EXPORTING
      I_PARENT          = my_dock.

* Build up field catalog from DDIC settings:
  call function 'LVC_FIELDCATALOG_MERGE'
       EXPORTING
            i_structure_name = 'MARA'
       CHANGING
            ct_fieldcat      = i_fieldcatalog_local.

* Determine which fields you want to allow editing on
  loop at i_fieldcatalog_local from 3 to 8
    into lo_fieldcatalog.
    lo_fieldcatalog-edit = 'X'.
    modify i_fieldcatalog_local from lo_fieldcatalog.
  endloop.

* Set a field to "changeable":
  CALL METHOD MY_ALV->SET_TABLE_FOR_FIRST_DISPLAY
    CHANGING
      IT_OUTTAB                     = i_mara
      IT_FIELDCATALOG               = i_fieldcatalog_local.

* Ensure the ENTER key triggers the change data event:
 call method MY_ALV->register_edit_event
               exporting
                  i_event_id = cl_gui_alv_grid=>mc_evt_enter.

* Register change data event:
  create object g_user_interaction.
  set handler g_user_interaction->change_field for MY_ALV.

endform.