In this tutorial I’ll show how to enable only the specific cells of a grid to be editable.
First I’ll prepare some demonstration data. It will be a list of materials and their maintenance status. If status is set to ‘A’ we would like to disable editing of status in the grid.
MATNR (type MATNR, Primary key) | PSTAT (type PSTAT_D) |
---|---|
123456 | K |
654321 | E |
567890 | D |
876543 | B |
900000 | A |
Now we create a function group with screen (i.e. 0200) where we’ll draw our ALV grid.
In the screen layout editor create a custom container and name it i.e. GO_GRID_CONTAINER.
Also create a custom PF-STATUS with two functions: UPDATE and REFRESH
The flow logic for the screen will be simple:
PROCESS BEFORE OUTPUT.
MODULE pbo_0200.
*
PROCESS AFTER INPUT.
MODULE pai_0200.
The PBO_0200 module is used to gather data, create field catalog and display the ALV. Here follows its code
MODULE pbo_0200 OUTPUT.
DATA:
ls_layout TYPE lvc_s_layo,
ls_variant TYPE disvariant.
FIELD-SYMBOLS:
<fs_fcat> TYPE lvc_s_fcat.
* In this user status we create two functions:
* UPDATE - save all changes and refresh grid
* REFRESH - refresh grid with data from Z-table
SET PF-STATUS 'PF_STATUS'.
* Initial creation of container and grid
IF gr_container IS INITIAL.
CREATE OBJECT gr_container
EXPORTING
* This is reference to the object we made in Layout editor
container_name = 'GO_GRID_CONTAINER'.
* Parent is our container created above
CREATE OBJECT gr_grid
EXPORTING
i_parent = gr_container.
* Simplest field catalog creation
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'ZMATSTAT'
CHANGING
ct_fieldcat = gt_fieldcat.
* !!! IMPORTANT !!!
* We have to mark all required columns as editable !!!
READ TABLE gt_fieldcat ASSIGNING <fs_fcat> WITH KEY fieldname = 'PSTAT'.
<fs_fcat>-edit = abap_true.
* Load data from database and mark the
* appropriate field as R/O
PERFORM reload_data.
* Set the field name carrying info about cell styles (RW / RO)
ls_layout-stylefname = 'CELL_STYLES'.
ls_variant-report = sy-repid.
ls_variant-username = sy-uname.
* !!! IMPORTANT !!!
* We register the ENTER event so the manual changes
* are propagated back to GT_DATA
gr_grid->register_edit_event( i_event_id = cl_gui_alv_grid=>mc_evt_enter ).
* Draw the ALV
gr_grid->set_table_for_first_display(
EXPORTING
is_layout = ls_layout
is_variant = ls_variant
i_save = 'A'
i_default = 'X'
CHANGING
it_fieldcatalog = gt_fieldcat
it_outtab = gt_data ).
ENDIF.
ENDMODULE. " PBO_0200 OUTPUT
Module PAI_0200 is used to process user interaction and here follows its code
MODULE pai_0200 INPUT.
DATA:
l_matstat TYPE zmatstat.
FIELD-SYMBOLS:
<fs_data> LIKE LINE OF gt_data.
CASE sy-ucomm.
WHEN 'BACK' OR 'LEAVE' OR 'CANCEL'.
SET SCREEN 0.
WHEN 'UPDATE'.
* Update DB Z-table
LOOP AT gt_data ASSIGNING <fs_data> WHERE matnr IS NOT INITIAL.
MOVE-CORRESPONDING <fs_data> TO l_matstat.
MODIFY zmatstat FROM l_matstat.
ENDLOOP.
* Reload data database and mark the
* appropriate fields as R/O
PERFORM reload_data.
gr_grid->refresh_table_display( ).
WHEN 'REFRESH'.
PERFORM reload_data.
gr_grid->refresh_table_display( ).
ENDCASE.
ENDMODULE. " PAI_0200 INPUT
The helper FORM RELOAD_DATA is used to gather data from database and mark the appropriate fields as read/only
FORM reload_data.
DATA:
l_lvc_s_styl TYPE lvc_s_styl.
FIELD-SYMBOLS:
<fs_data> LIKE LINE OF gt_data.
CLEAR gt_data[].
* Read the data
SELECT *
INTO CORRESPONDING FIELDS OF TABLE gt_data
FROM zmatstat.
* Loop over all rows of data and set PSTAT
* as R/O in case its value = 'A'
LOOP AT gt_data
ASSIGNING <fs_data>
WHERE pstat = 'A'.
l_lvc_styl-style = cl_gui_alv_grid=>mc_style_disabled.
l_lvc_styl-fieldname = 'PSTAT'.
* INSERT command MUST be used
* because field CELL_STYLES is a SORTED TABLE
INSERT l_lvc_styl INTO TABLE <fs_data>-cell_styles.
ENDLOOP.
ENDFORM. "reload_data
If we now call our screen from ABAP, we’ll get the following:
If we modify some data, change the value of PSTAT in some row to ‘A’ and save the changes:
…and data in database:
In this case, only one column is editable, how can we edit more columns ?
Found, just have to duplicate the corresponding lines.
READ TABLE gt_fieldcat ASSIGNING WITH KEY fieldname = ‘PSTAT1’.
-edit = abap_true.
READ TABLE gt_fieldcat ASSIGNING WITH KEY fieldname = ‘PSTAT2’.
-edit = abap_true.
etc…
Hello, WITH KEY fieldname = ‘PSTAT’.
-edit = abap_true.
I guess you read the code…and especially this part:
* !!! IMPORTANT !!!
* We have to mark all required columns as editable !!!
READ TABLE gt_fieldcat ASSIGNING
…so if you want to make another column editable, just modify the corresponding field in your field catalog similarly to the code mentioned above.