Because of the ease-of-use nature of CL_SALV_TABLE, we have long wanted to use CL_SALV_TABLE to handle editing as well. A post by SCN Blogger Naimesh Patel surprised many and saw potential.
https://blogs.sap.com/2015/06/25/salv-editable-with-single-custom-method/
Paul Hardy has suggested a more moderate way to make columns editable.
https://blogs.sap.com/2015/08/07/salv-and-pepper-editing-individual-columns-in-the-salv/
Even if the original design ruled out modifications, many developers wanted to, so the road was finally opened. It is known that CL_SALV_TABLE is now editable since Release 756. (See https://blogs.sap.com/2022/08/01/editable-cl_salv_table-after-release-756/)
Now that the fields are modifiable, the question here is whether you can handle up to the DATA_CHANGED event like ALV Grid does. The answer lies in the IF_SALV_GUI_OM_EDIT_RESTRICTED interface, which is important to the edit function. It is now possible to implement by specifying a listener that corresponds to an existing event handler.
See class CL_SALV_GUI_OM_EDITABLE_HNDLR for actual implementation information.
Method set_listener( ) receives an instance implementing the interface IF_SALV_GUI_OM_EDIT_STRCT_LSTR as an argument, and the interface has the following two methods.
Notice here is the method on_check_changed_data( ). We can use this method to achieve what we want.
To handle the event DATA_CHANGED, proceed as follows.
- Build editable SALV GRID
https://blogs.sap.com/2022/08/01/editable-cl_salv_table-after-release-756/
- Implement LISTENER Class
Implements two methods of interface IF_SALV_GUI_OM_EDIT_STRCT_LSTR
- Assign LISTENER
method IF_SALV_GUI_OM_EDIT_RESTRICTED~set_listener( )
1. Build editable SALV GRID
CLASS lcl_main IMPLEMENTATION.
METHOD show_salv.
select_data( ).
cl_salv_table=>factory( EXPORTING r_container = cl_gui_container=>default_screen
IMPORTING r_salv_table = mo_table
CHANGING t_table = mt_data ).
mo_table->get_display_settings( )->set_list_header( 'SALV Edit Example' ).
set_columns( ).
set_function_keys( ).
" Add custom handler method to the table (implementation details follows in the next step)
SET HANDLER handle_added_function FOR mo_table->get_event( ).
mo_edit = mo_table->extended_grid_api( )->editable_restricted( ).
mo_table->display( ).
ENDMETHOD.
...
2. Implement LISTENER Class
The LISTENER class simply implements two methods of interface IF_SALV_GUI_OM_EDIT_STRCT_LSTR. This time we will only implement the method on_check_changed_data( ).
In ALV GRID, the function supported by class CL_ALV_CHANGED_DATA_PROTOCOL, which is the reference type of ER_DATA_CHANGED parameter of event DATA_CHANGED, is divided into two interfaces, IF_SALV_GUI_OM_EDIT_UI_MODIFY and IF_SALV_GUI_OM_EDIT_UI_PROTCOL.
Interface | Main function | Method |
IF_SALV_GUI_OM_EDIT_UI_MODIFY | Function for changed data | GET_CELL_VALUE
MODIFY_CELL_VALUE GET_UI_CHANGES |
IF_SALV_GUI_OM_EDIT_UI_PROTCOL | User interface related functions such as error display | IS_VISIBLE
ADD_PROTOCOL_ENTRY DISPLAY_PROTOCOL FREE |
CLASS lcl_listener DEFINITION.
PUBLIC SECTION.
INTERFACES if_salv_gui_om_edit_strct_lstr.
ENDCLASS.
CLASS lcl_listener IMPLEMENTATION.
METHOD if_salv_gui_om_edit_strct_lstr~on_f4_request.
ENDMETHOD.
METHOD if_salv_gui_om_edit_strct_lstr~on_check_changed_data.
o_ui_data_modify->get_ui_changes( IMPORTING t_modified_cells = DATA(lt_modified) ).
DATA: seatsmax TYPE ysflight-seatsmax,
seatsocc TYPE ysflight-seatsocc.
LOOP AT lt_modified ASSIGNING FIELD-SYMBOL(<data>)
GROUP BY ( value = <data>-row_id )
INTO DATA(lt_group).
LOOP AT GROUP lt_group ASSIGNING FIELD-SYMBOL(<row_id>).
o_ui_data_modify->get_cell_value( EXPORTING row_id = <row_id>-row_id
fieldname = c_field-seatsmax
IMPORTING cell_value = seatsmax ).
o_ui_data_modify->get_cell_value( EXPORTING row_id = <row_id>-row_id
fieldname = c_field-seatsocc
IMPORTING cell_value = seatsocc ).
IF seatsmax < seatsocc.
o_ui_data_modify->modify_cell_value( row_id = <row_id>-row_id
fieldname = c_field-except
cell_value = light-red ).
DATA(error) = abap_true.
o_ui_edit_protocol->add_protocol_entry( msgid = '00'
msgty = 'E'
msgno = '001'
msgv1 = 'Line no. '
msgv2 = |{ <row_id>-row_id }|
msgv3 = ': exceeds the maximum'
fieldname = c_field-except ).
ELSE.
o_ui_data_modify->modify_cell_value( row_id = <row_id>-row_id
fieldname = c_field-except
cell_value = light-green ).
ENDIF.
ENDLOOP.
ENDLOOP.
IF error = abap_true .
o_ui_edit_protocol->add_protocol_entry( msgid = '00'
msgty = 'E'
msgno = '001'
msgv1 = 'Has Error'
msgv2 = '. Check red light(s)'
fieldname = c_field-except ).
ENDIF.
ENDMETHOD.
ENDCLASS.
3. Assign LISTENER
The method set_application_log_container( ) causes error messages to be looked up in the specified container when errors occur. If not specified, it appears as a popup as before.
mo_listener = NEW lcl_listener( ).
mo_edit->set_listener( mo_listener ).
mo_docking = NEW #( parent = cl_gui_container=>default_screen
side = cl_gui_docking_container=>dock_at_bottom
ratio = 20 ).
mo_edit->set_application_log_container( mo_docking ).
Execution
The SAVE button is not activated due to the application of display mode applied at the first execution.
If an error occurs during editing, it appears at the bottom and a red traffic light appears.
Save after error correcting.
Closing
The simplest CL_SALV_TABLE for query is now legally capable of processing modifications and validation of input information, and the system even supports CBO processing for DATA_CHAHED and ONF4 events. Now, the CL_SALV_TABLE, rather than the OO ALV(CL_GUI_ALV_GRID class) that many developers wanted, can perfectly implement the modification function. Hopefully this will help those who want to add editing to SALV in release 756 and later in the future. For older versions, see the link at the beginning of this blog.
Thank you for reading my first blog. There are many shortcomings, but I hope it helped you. If you have any questions or improvements, please feel free to leave a comment. Thank you.