5/8/2019 - 10:18 AM

ALV: Semplice ALV EDITABILE con tabella output e fieldcat dinamici (RTTI/rtts)

1) copy code below

2) Create a blank screen 100 with a custom control on it called CCONTAINER1

3) Create a standard status (SE41) with just the standard BACK, CANCEL and EXIT tool bars on it and give it any title

You will see a standard editable ALV GRID -- the grid toolbars will give you limied functionality.

You can add events such as double click, get cells etc but I would understand how the basic code works.

For a look at standard methods available in cl_gui_alv_grid just use SE24.

For any OTHER structure just put your structure between begin of s_elements and end of S_elements .

Fill also your table in the form populate dynamic itab.

Easy isn't it. This program IS COMPLETELY GENERAL.

If you create a function module to call the grid you could create the screen and status in the FMOD so each program will no longer need it's own screen.

program zzz_simple_editable_grid.

* Define any structure
types:  begin of s_elements,
  vbeln   type vapma-vbeln,
  posnr   type vapma-posnr,
  matnr   type vapma-matnr,
  kunnr   type vapma-kunnr,
  werks   type vapma-werks,
  vkorg   type vapma-vkorg,
  vkbur   type vapma-vkbur,
  status  type c,

end of  s_elements.

* end of your structure

data lr_rtti_struc type ref to cl_abap_structdescr .
    zog                     like line of lr_rtti_struc->components .
    zogt                    like table of zog,
  wa_it_fldcat type lvc_s_fcat,
  it_fldcat type lvc_t_fcat ,
  dy_line            type ref to data,
  dy_table           type ref to data.

 data:  dref               type ref to data.
 field-symbols: <fs> type any,
    <dyn_table>    type  standard table,

data grid_container1 type ref to cl_gui_custom_container .
  data grid1 type ref to cl_gui_alv_grid .
data: ok_code type sy-ucomm.
data: struct_grid_lset type lvc_s_layo.

*Let's build the fieldcatalog AUTOMATICALLY from our data structure

* First get your data structure into a field symbol

create data dref type s_elements.

  assign dref->* to <fs>.

  lr_rtti_struc ?= cl_abap_structdescr=>describe_by_data( <fs> ).

* Now get the structure details into a table.
* table zogt[] contains the structure details
* From which we can build the field catalog

    zogt[]  = lr_rtti_struc->components.
 loop at zogt into zog.
      clear wa_it_fldcat.
      wa_it_fldcat-fieldname = zog-name .
      wa_it_fldcat-datatype = zog-type_kind.
      wa_it_fldcat-inttype = zog-type_kind.
      wa_it_fldcat-intlen = zog-length.
      wa_it_fldcat-decimals = zog-decimals.
      wa_it_fldcat-coltext = zog-name.
      wa_it_fldcat-lowercase = 'X'.
      append wa_it_fldcat to it_fldcat .

* You can perform any modifications / additions to your field catalog
* here such as your own column names etc.

* Now using the field catalog created above we can
* build a dynamic table
* and populate it

* First build the dynamic table
* the table will contain entries for
* our structure defined at the start of the program

call method cl_alv_table_create=>create_dynamic_table
            it_fieldcatalog = it_fldcat
            ep_table = dy_table.

* Now fill our table with data

assign dy_table->* to <dyn_table>.
  create data dy_line like line of <dyn_table>.
  assign dy_line->* to <dyn_wa>.

  select vbeln posnr matnr kunnr werks vkorg vkbur
         up to 200 rows
         from vapma
         into  corresponding fields of table <dyn_table>.

* Call the screen to display the grid

call screen 100.

* PBO module

module status_0100 output.

if grid_container1 is initial.
create object grid_container1
           container_name = 'CCONTAINER1'.
    create object  grid1
          i_parent = grid_container1.
  struct_grid_lset-edit = 'X'.    "To enable editing in ALV

call method grid1->set_table_for_first_display
  exporting is_layout =  struct_grid_lset
             it_outtab       = <dyn_table>
             it_fieldcatalog = it_fldcat.

set pf-status '001'.
     set titlebar '000'.


* PAI module

  module user_command_0100 input.
      case sy-ucomm.
    when 'BACK'.
      leave program.
    when 'EXIT'.
      leave program.
    when 'RETURN'.
      leave program.
    when others.