Making ALV to react to Change data automatically
Making ALV to react to Change data automatically
Scenario:
To make ALV to react to data change automatically without any need for the user to click on
ENTER or any other button/menu item.
Procedure:
In order to make the system react to an edit event, we need to first register the edit event.
Exporting
When user press 'ENTER' the event MC_EVT_ENTER is triggered which automatically sets the
variable M_cell_edit to 'X'.
But if the user bypasses the enter key then the variable M_CELL_EDIT has to be set explicitly
which is done by passing mc_evt_modified to the exporting parameter I_EVENT_ID instead
of mc_evt_enter.
In the PAI event, call the method CHECK_CHANGED_DATA. This method automatically triggers
the data_changed event.
By default, SAP recognizes 'ENTER' event. When only the CHECK_CHANGED_DATA is called in
PAI event, then the 'ENTER' event is not recognized by the system. In order to have both the
events recognized by the system, we need to register the edit event.
Sample code:
REPORT Z_ALV_EDIT_EVENT.
*"Table declarations...................................................
TABLES:
*"--------------------------------------------------------------------*
* Work variables *
*"--------------------------------------------------------------------*
DATA:
W_GRID TYPE REF TO CL_GUI_ALV_GRID, " Reference object for alv grid
*"--------------------------------------------------------------------*
*"--------------------------------------------------------------------*
DATA:
*"--------------------------------------------------------------------*
*"--------------------------------------------------------------------*
DATA:
*"--------------------------------------------------------------------*
*"--------------------------------------------------------------------*
DATA:
*"--------------------------------------------------------------------*
*"--------------------------------------------------------------------*
DATA:
T_SPFLI LIKE
STANDARD TABLE
OF FS_SPFLI.
*"--------------------------------------------------------------------*
*"--------------------------------------------------------------------*
DATA:
*"--------------------------------------------------------------------*
* START-OF-SELECTION EVENT *
*"--------------------------------------------------------------------*
START-OF-SELECTION.
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
* This module is used to set the layout for the alv grid display *
*----------------------------------------------------------------------*
WA_LAYOUT-ZEBRA = 'X'.
WA_LAYOUT-EDIT = 'X'.
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
CLEAR WA_FIELD_CATALOG.
WA_FIELD_CATALOG-FIELDNAME = 'CARRID'.
WA_FIELD_CATALOG-REF_FIELD = 'CARRIDS'.
WA_FIELD_CATALOG-REF_TABLE = 'SPFLI'.
WA_FIELD_CATALOG-COL_POS = 1.
WA_FIELD_CATALOG-FIELDNAME = 'CONNID'.
WA_FIELD_CATALOG-REF_FIELD = 'CONNID'.
WA_FIELD_CATALOG-REF_TABLE = 'SPFLI'.
WA_FIELD_CATALOG-COL_POS = 2.
CLEAR WA_FIELD_CATALOG.
WA_FIELD_CATALOG-FIELDNAME = 'CITYFROM'.
WA_FIELD_CATALOG-REF_FIELD = 'CITYFROM'.
WA_FIELD_CATALOG-REF_TABLE = 'SPFLI'.
WA_FIELD_CATALOG-COL_POS = 3.
CLEAR WA_FIELD_CATALOG.
WA_FIELD_CATALOG-FIELDNAME = 'CITYTO'.
WA_FIELD_CATALOG-REF_FIELD = 'CITYTO'.
WA_FIELD_CATALOG-REF_TABLE = 'SPFLI'.
WA_FIELD_CATALOG-COL_POS = 4.
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
CASE SY-UCOMM.
WHEN 'OK'.
* event
* E_VALID =
* CHANGING
* C_REFRESH = 'X'
LEAVE TO SCREEN 0.
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
IF W_GRID IS INITIAL.
EXPORTING
CONTAINER_NAME = 'CONTAINER1'
EXCEPTIONS
CNTL_ERROR =1
CNTL_SYSTEM_ERROR =2
CREATE_ERROR =3
LIFETIME_ERROR =4
LIFETIME_DYNPRO_DYNPRO_LINK = 5
OTHERS =6
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
EXPORTING
I_PARENT = W_CONTAINER
EXCEPTIONS
ERROR_CNTL_CREATE = 1
ERROR_CNTL_INIT = 2
ERROR_CNTL_LINK = 3
ERROR_DP_CREATE = 4
OTHERS = 5.
IF SY-SUBRC <> 0.
EXPORTING
I_STRUCTURE_NAME = 'SPFLI'
IS_LAYOUT = WA_LAYOUT
CHANGING
IT_OUTTAB = T_SPFLI[]
IT_FIELDCATALOG = T_FIELD_CATALOG
EXCEPTIONS
INVALID_PARAMETER_COMBINATION = 1
PROGRAM_ERROR =2
TOO_MANY_LINES =3
OTHERS = 4.
IF SY-SUBRC <> 0.
EXPORTING
I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_MODIFIED
EXCEPTIONS
ERROR =1
OTHERS = 2.
IF SY-SUBRC <> 0.
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
The whole scenario would work fine when you change the data and press the "OK" button on
the application toolbar.
But, when you change the data and press the 'ENTER' key then the event data_changed is not
triggered.
1. Change the data in the editable ALV and press 'ENTER' key.
The changes are not reflected in the ALV as no data_changed event has been triggered.
Scenario 2: When you register the edit event and call the method CHECK_CHANGED_DATA in
the PAI event.
1. Change the data in the editable ALV and press the 'OK' button on the application toolbar.
The changed data is recognized and is reflected on to the editable ALV
In the same case, when you change the data in the ALV and press 'ENTER' key, you can still see
the changes reflected.
Click on ENTER.
The change event is triggered and the data has been changed accordingly.