SAP ALV Tutorial – Make ALV editable
Sometimes, we need to make our ALV editable. SAP has already provide table control to modify table data(previously we use loop step instead). But ALV can provide more functionality to support sort, total,filter, etc.
As far as the edit feature is concerned, a cell of the alv grid control can have the following states:
1. non-editable : edit feature not set for this cell
2. editable : edit feature set for this cell
The second state (”editable”) has two substates:
1. editable and not ready for input: edit feature set but not active
2. editable and ready for input: edit feature set and active
You switch between “editable and not ready for input” and “editable and ready for input” using method SET_READY_FOR_INPUT. Below is a piece of code.
CALL METHOD g_grid->set_table_for_first_display EXPORTING i_structure_name = 'SFLIGHT' is_layout = gs_layout CHANGING it_outtab = gt_outtab. *2.Use SET_READY_FOR_INPUT to allow editing initially. * (state ”editable and ready for input”). CALL METHOD g_grid->set_ready_for_input EXPORTING i_ready_for_input = 1.
As mentioned above, we can set the editable feature to a cell level. It means that we can set some cells to be editable and some other in display mode according your requirement. Below is the step to achive this.
Step 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.
DATA: BEGIN OF gt_outtab occurs 0. "with header line include structure sflight. DATA: celltab type LVC_T_STYL. DATA: END OF gt_outtab.
Step 2. After selecting data, set edit status for each row in a loop according to your requirement.
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_disabled. INSERT ls_celltab INTO TABLE pt_celltab.
Step 3. Provide the fieldname of the celltab field by using field STYLEFNAME of the layout structure.
gs_layout-stylefname = 'CELLTAB'.
Now we can control the editable of the ALV using method.
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
Standard programs ‘BCALV_EDIT_01′ and ‘BCALV_EDIT_02′ are very good example for ALV edit functionality. After this article, we can make the ALV grid to be editable, but no event is registered and no error handling is performed. In next article, we use other SAP standard program to do more explaination.