No products in the cart.
ALV
Simple Editable ALV with Save Logic.
To implement the save logic in a procedural ALV, we need to handle the User Command. This involves capturing the “Save” button click and updating the database with the modified contents of the internal table.
Since we are using REUSE_ALV_GRID_DISPLAY, we must pass the name of a subroutine to the parameter i_callback_user_command.
Simple Editable ALV with Save Logic.
REPORT z_editable_alv_save.
TYPE-POOLS: slis.
* 1. Data Declaration
DATA: it_sflight TYPE TABLE OF sflight,
it_fieldcat TYPE slis_t_fieldcat_alv,
wa_fieldcat TYPE slis_fieldcat_alv.
* 2. Data Retrieval
START-OF-SELECTION.
SELECT * FROM sflight INTO TABLE it_sflight UP TO 20 ROWS.
PERFORM build_fieldcat.
PERFORM display_alv.
*———————————————————————*
* 3. Build Field Catalog
*———————————————————————*
FORM build_fieldcat.
” Column 1: Carrier ID (Read Only)
wa_fieldcat-fieldname = ‘CARRID’.
wa_fieldcat-seltext_m = ‘Airline’.
APPEND wa_fieldcat TO it_fieldcat. CLEAR wa_fieldcat.
” Column 2: Price (Editable)
wa_fieldcat-fieldname = ‘PRICE’.
wa_fieldcat-seltext_m = ‘New Price’.
wa_fieldcat-edit = ‘X’. ” Enable editing
APPEND wa_fieldcat TO it_fieldcat. CLEAR wa_fieldcat.
ENDFORM.
*———————————————————————*
* 4. Display ALV
*———————————————————————*
FORM display_alv.
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY’
EXPORTING
i_callback_program = sy-repid
i_callback_user_command = ‘USER_COMMAND’ ” Subroutine name
it_fieldcat = it_fieldcat
TABLES
t_outtab = it_sflight.
ENDFORM.
*———————————————————————*
* 5. Handle User Command (Save Logic)
*———————————————————————*
FORM user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
” Force the ALV to copy cell changes from the screen to the internal table
DATA: lr_grid TYPE REF TO cl_gui_alv_grid.
CALL FUNCTION ‘GET_GLOBALS_FROM_SLVC_FULLSCREEN’
IMPORTING
e_grid = lr_grid.
IF lr_grid IS BOUND.
lr_grid->check_changed_data( ).
ENDIF.
” Check if the user clicked the standard ‘SAVE’ button or ‘Enter’
CASE r_ucomm.
WHEN ‘&DATA_SAVE’. ” Standard ALV Save function code
UPDATE sflight FROM TABLE it_sflight.
IF sy-subrc = 0.
MESSAGE ‘Database updated successfully!’ TYPE ‘S’.
COMMIT WORK.
ELSE.
MESSAGE ‘Error updating database.’ TYPE ‘E’.
ROLLBACK WORK.
ENDIF.
ENDCASE.
” Refresh the display
rs_selfield-refresh = ‘X’.
ENDFORM.
Key Components Explained
- i_callback_user_command: This tells the ALV to call our subroutine USER_COMMAND whenever a button is clicked.
- check_changed_data: This is a critical step. When a user types in a cell, the value stays in the “screen buffer.” Calling this method forces the system to move those new values into your internal table (it_sflight).
- &DATA_SAVE: This is the standard function code for the Save button in many SAP interfaces.
- UPDATE sflight FROM TABLE: This is an Array Update. It takes the entire internal table and updates the corresponding rows in the database based on the primary keys (MANDT, CARRID, CONNID, FLDATE).
- rs_selfield-refresh = ‘X’: This ensures the screen updates to reflect any changes or status messages after the logic is processed.
Note: In a production environment, you would usually define a custom PF-STATUS to show a specific “Save” button in the toolbar, as the default ALV toolbar might not always display the Save icon.