No products in the cart.
ABAP
The OO ABAP Editable ALV Code
In the Object-Oriented (OOPS) approach, we use the class CL_GUI_ALV_GRID. Unlike the simpler CL_SALV_TABLE (which is mostly read-only), CL_GUI_ALV_GRID is the industry standard for building interactive and editable applications.
To handle the “Save” logic, we use Event Handling.
The OO ABAP Editable ALV Code.
REPORT z_oo_editable_alv.
*——————————————————————–*
* CLASS lcl_handler DEFINITION (Local class for Events)
*——————————————————————–*
CLASS lcl_handler DEFINITION.
PUBLIC SECTION.
METHODS:
handle_toolbar FOR EVENT toolbar OF cl_gui_alv_grid
IMPORTING e_object e_interactive,
handle_user_command FOR EVENT user_command OF cl_gui_alv_grid
IMPORTING e_ucomm.
ENDCLASS.
*——————————————————————–*
* DATA DECLARATION
*——————————————————————–*
DATA: it_sflight TYPE TABLE OF sflight,
lo_container TYPE REF TO cl_gui_custom_container,
lo_grid TYPE REF TO cl_gui_alv_grid,
lo_handler TYPE REF TO lcl_handler,
lt_fcat TYPE lvc_t_fcat,
ls_fcat TYPE lvc_s_fcat.
START-OF-SELECTION.
SELECT * FROM sflight INTO TABLE it_sflight UP TO 20 ROWS.
” 1. Create Field Catalog manually for Editability
ls_fcat-fieldname = ‘PRICE’.
ls_fcat-edit = ‘X’.
ls_fcat-scrtext_m = ‘Unit Price’.
APPEND ls_fcat TO lt_fcat.
” 2. Initialize ALV (Using the full screen as container)
CREATE OBJECT lo_grid
EXPORTING
i_parent = cl_gui_container=>screen0.
” 3. Register Events
CREATE OBJECT lo_handler.
SET HANDLER lo_handler->handle_toolbar FOR lo_grid.
SET HANDLER lo_handler->handle_user_command FOR lo_grid.
” 4. Display the Grid
lo_grid->set_table_for_first_display(
CHANGING
it_outtab = it_sflight
it_fieldcatalog = lt_fcat ).
” Keep the program alive to see the output
CALL SCREEN 0.
*——————————————————————–*
* CLASS lcl_handler IMPLEMENTATION
*——————————————————————–*
CLASS lcl_handler IMPLEMENTATION.
METHOD handle_toolbar.
” Add a Save Button to the Toolbar
DATA: ls_button TYPE stb_button.
ls_button-function = ‘SAVE_DATA’.
ls_button-icon = icon_system_save.
ls_button-text = ‘Save Changes’.
APPEND ls_button TO e_object->mt_toolbar.
ENDMETHOD.
METHOD handle_user_command.
IF e_ucomm = ‘SAVE_DATA’.
” Force the grid to update the internal table
lo_grid->check_changed_data( ).
UPDATE sflight FROM TABLE it_sflight.
IF sy-subrc = 0.
MESSAGE ‘Data Saved Successfully!’ TYPE ‘S’.
ENDIF.
ENDIF.
ENDMETHOD.
ENDCLASS.
OOPS Components Explained
1.Classes and Objects:
- cl_gui_alv_grid: The “Blueprint” for the grid.
- lo_grid: The actual “Object” (instance) we interact with.
2.Containers (cl_gui_container):
- OOPS ALV needs a “place to live.” In this code, we used screen0 (the default screen). In complex apps, you might use a cl_gui_custom_container to put the ALV inside a specific box on a screen.
3.Encapsulation (The Handler Class):
- We created a local class lcl_handler. This keeps all the logic for “what happens when I click a button” inside one unit, separate from the main data selection.
4.Event Handling (SET HANDLER):
- The Grid object “triggers” an event (like toolbar or user_command).
- Our handler class “listens” for those events. This is much more flexible than the procedural callback because you can have multiple listeners for one event.
5.Methods:
- Instead of calling function modules, we call methods like set_table_for_first_display. This is cleaner and allows the object to maintain its own “state” (memory of what is currently on screen).