We have seen in the previous post how to Disable the DELETE-key in the keyboard in the editable ALV using the OO ALV class CL_GUI_ALV_GRID. Today, we will see how we can implement the same functionality using the classical ALV generated using the function module REUSE_ALV_GRID_DISPLAY.
As discussed in the previous post, to implement this we need to implement the DATA_CHANGED and the DATA_CHANGED_FINISHED event. The hurdle here is, we don’t have a event similar to DATA_CHANGED_FINISHED in OOALV in the Classical FM REUSE_ALV_GRID_DISPLAY ALV.
Whenever we generate an ALV with the FM REUSE_ALV_GRID_DISPLAY, it internally creates a object reference fo the CL_GUI_ALV_GRID. We will get that object in our program to overcome the hurdle of not having the DATA_CHANGED_FINISHED event. We will get the OO ALV object from this ALV using the FM GET_GLOBALS_FROM_SLVC_FULLSCR. We will implement the event handler class for that ALV object.
Here I am giving the full example code snippet to implement this functionality.
Code Snippet to Disable DELETE key in the Classical ALV
*&---------------------------------------------------------------------*
*& Code snippet for diabling the delete key on the keyboard in the
*& ALV created using FM REUSE_ALV_GRID_DISPLAY
*&---------------------------------------------------------------------*
REPORT zalv_dis_del_key.
*
TYPE-POOLS: slis.
* Data to be displayed
DATA: gt_sflight TYPE TABLE OF sflight.
DATA: gt_sflight1 TYPE TABLE OF sflight,
gs_layout TYPE slis_layout_alv,
l_deleted TYPE flag.
*
DATA: o_grid TYPE REF TO cl_gui_alv_grid.
*
*----------------------------------------------------------------------*
* Event Handler class for ALV Events
*----------------------------------------------------------------------*
CLASS lcl_event_handle DEFINITION.
PUBLIC SECTION.
METHODS handle_data_changed
FOR EVENT data_changed OF cl_gui_alv_grid
IMPORTING er_data_changed.
*
METHODS:
handle_changed_finished FOR EVENT data_changed_finished
OF cl_gui_alv_grid
IMPORTING e_modified
et_good_cells.
*
ENDCLASS. "lcl_event_handle DEFINITION
*
DATA: o_event_h TYPE REF TO lcl_event_handle.
*
START-OF-SELECTION.
*---------------------------------------------------------------------*
* Selection
SELECT * FROM sflight INTO CORRESPONDING FIELDS OF TABLE gt_sflight.
*
* Edit
gs_layout-edit = 'X'.
*
* Event for Top-of-page
DATA: lt_events TYPE slis_t_event.
DATA: la_events LIKE LINE OF lt_events.
*
la_events-name = 'TOP_OF_PAGE'.
la_events-form = 'TOP_OF_PAGE'.
APPEND la_events TO lt_events.
*
* Call ABAP List Viewer (ALV)
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = 'ZALV_DIS_DEL_KEY'
i_callback_user_command = 'USER_COMMAND'
i_structure_name = 'SFLIGHT'
is_layout = gs_layout
it_events = lt_events
TABLES
t_outtab = gt_sflight.
*
*&---------------------------------------------------------------------*
* TOP-OF-PAGE. Also used to get the Object reference
*----------------------------------------------------------------------*
FORM top_of_page.
*
DATA : lt_comment TYPE slis_t_listheader,
la_comment TYPE slis_listheader.
*
* Top of page
la_comment-typ = 'H'.
la_comment-info = 'Disabled Delete Key'.
APPEND la_comment TO lt_comment.
*
* Commenty write
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
it_list_commentary = lt_comment.
*
* Get the ALV object
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = o_grid.
*
* Register the Modified event ... Important
CALL METHOD o_grid->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_modified.
*
* Set Event handler
CREATE OBJECT o_event_h.
SET HANDLER o_event_h->handle_data_changed FOR o_grid.
SET HANDLER o_event_h->handle_changed_finished FOR o_grid.
*
ENDFORM. "top_of_page
*
*&---------------------------------------------------------------------*
* User Command
*----------------------------------------------------------------------*
FORM user_command USING r_ucomm TYPE sy-ucomm
rs_selfield TYPE slis_selfield.
IF r_ucomm = '&DATA_SAVE'.
MESSAGE 'You pressed the save button' TYPE 'I'.
ENDIF.
ENDFORM. "user_command
*
*----------------------------------------------------------------------*
* Event Handler Class implementation
*----------------------------------------------------------------------*
CLASS lcl_event_handle IMPLEMENTATION.
*
METHOD handle_data_changed.
*
*
DATA: ls_deleted_rows LIKE LINE OF er_data_changed->mt_deleted_rows.
*
* data is deleted or not. If yes, than fill the temporary table
* to its copy
DESCRIBE TABLE er_data_changed->mt_deleted_rows LINES sy-index.
IF sy-index IS NOT INITIAL.
l_deleted = 'X'.
gt_sflight1[] = gt_sflight[].
CLEAR er_data_changed->mt_deleted_rows.
ENDIF.
*
*
ENDMETHOD. "handle_data_changed
*
METHOD handle_changed_finished.
*
* data has been deleted than set the temp data back to the main table
* and refresh the table display
IF l_deleted = 'X'.
gt_sflight[] = gt_sflight1[].
CLEAR: l_deleted, gt_sflight1.
ENDIF.
* message
MESSAGE 'You can not delete any record' TYPE 'I'.
* refresh the list display
CALL METHOD o_grid->refresh_table_display
EXCEPTIONS
finished = 1
OTHERS = 2.
IF sy-subrc NE 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
*
ENDMETHOD. "handle_changed_finished
*
ENDCLASS. "lcl_event_handle IMPLEMENTATION
Related post using FM GET_GLOBALS_FROM_SLVC_FULLSCR