设为首页收藏本站

SAP Best Business Solution

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1976|回复: 0

Making ALV to react to Change data automatically

[复制链接]

20

主题

21

帖子

164

积分

注册会员

Rank: 2

积分
164
发表于 2016-11-16 17:32:03 | 显示全部楼层 |阅读模式
本帖最后由 kevin_huo 于 2016-11-16 17:33 编辑

Making ALV to react to Change data automatically

Skip to end of metadata

Go to start of metadata
The thread at https://forums.sdn.sap.com/thread.jspa?threadID=985676&tstart=0 has motivated me to write this mini tutorial which might be useful for others.

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.  
To register the edit event, call the method REGISTER_EDIT_EVENT
CALL METHOD cont_editalvgd -> register_edit_event
Exporting
{}I_event_id = cl_gui_alv_grid => mc_evt_modified.
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:
  SPFLI.                               " Flight Schedule Details
*" Data declarations...................................................
*"--------------------------------------------------------------------*
* Work variables                                                      *
*"--------------------------------------------------------------------*
DATA:
  W_GRID TYPE REF TO CL_GUI_ALV_GRID,  " Reference object for alv grid
  W_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER.
" Reference object for container
*"--------------------------------------------------------------------*
* Structure to hold Flight Schedule details                           *
*"--------------------------------------------------------------------*
DATA:
  FS_SPFLI TYPE SPFLI.
*"--------------------------------------------------------------------*
* Structure to hold Field Catalog details                             *
*"--------------------------------------------------------------------*
DATA:
  WA_FIELD_CATALOG TYPE LVC_S_FCAT.
*"--------------------------------------------------------------------*
* Structure to hold Layout of the ALV Report                          *
*"--------------------------------------------------------------------*
DATA:
  WA_LAYOUT TYPE LVC_S_LAYO.

*"--------------------------------------------------------------------*
* Internal table to hold Flight Schedule details from table SPFLI     *
*"--------------------------------------------------------------------*
DATA:
  T_SPFLI LIKE
STANDARD TABLE
       OF FS_SPFLI.
*"--------------------------------------------------------------------*
* Internal table to hold Field Catalog Details                        *
*"--------------------------------------------------------------------*
DATA:
  T_FIELD_CATALOG TYPE  LVC_T_FCAT.
*"--------------------------------------------------------------------*
*                       START-OF-SELECTION EVENT                      *
*"--------------------------------------------------------------------*
START-OF-SELECTION.
* Data retrieval from the database table
  SELECT * FROM SPFLI INTO TABLE T_SPFLI.
  CALL SCREEN 100.
*&---------------------------------------------------------------------*
*&      Module  set_layout  OUTPUT
*&---------------------------------------------------------------------*
*   This module is used to set the layout for the alv grid display     *
*----------------------------------------------------------------------*
MODULE SET_LAYOUT OUTPUT.
  WA_LAYOUT-GRID_TITLE = 'SPFLI TABLE DETAILS'.
  WA_LAYOUT-ZEBRA = 'X'.
  WA_LAYOUT-EDIT = 'X'.
ENDMODULE.                             " set_layout  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  field_catalog  OUTPUT
*&---------------------------------------------------------------------*
*   This module is used to populate the field catalog                  *
*----------------------------------------------------------------------*
MODULE FIELD_CATALOG OUTPUT.
  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.
  APPEND WA_FIELD_CATALOG TO T_FIELD_CATALOG.
  CLEAR WA_FIELD_CATALOG.
  WA_FIELD_CATALOG-FIELDNAME = 'CONNID'.
  WA_FIELD_CATALOG-REF_FIELD = 'CONNID'.
  WA_FIELD_CATALOG-REF_TABLE = 'SPFLI'.
  WA_FIELD_CATALOG-COL_POS   = 2.
  APPEND WA_FIELD_CATALOG TO T_FIELD_CATALOG.
  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.
  APPEND WA_FIELD_CATALOG TO T_FIELD_CATALOG.
  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.
  APPEND WA_FIELD_CATALOG TO T_FIELD_CATALOG.
ENDMODULE.                             " field_catalog  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*  This module is used to handle the PAI events
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
  CASE SY-UCOMM.
    WHEN 'OK'.
* Calling the check_changed_data method to trigger the data_changed
* event
*
      CALL METHOD W_GRID->CHECK_CHANGED_DATA
*  IMPORTING
*    E_VALID   =
*  CHANGING
*    C_REFRESH = 'X'
          .
      UPDATE SPFLI FROM TABLE T_SPFLI.
    WHEN 'BACK' OR 'EXIT' OR 'CANCEL'.
      LEAVE TO SCREEN 0.
  ENDCASE.                             " CASE SY-UCOMM
ENDMODULE.                             " USER_COMMAND_0100  INPUT

*&---------------------------------------------------------------------*
*&      Module  manage_alv_grid  OUTPUT
*&---------------------------------------------------------------------*
*   This module is used to manage the Grid display
*----------------------------------------------------------------------*
MODULE MANAGE_ALV_GRID OUTPUT.

  IF W_GRID IS INITIAL.
    CREATE OBJECT W_CONTAINER
      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
                 WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.                             " IF SY-SUBRC NE 0
    CREATE OBJECT W_GRID
      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.
      MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                 WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.                             " IF SY-SUBRC NE 0

    CALL METHOD W_GRID->SET_TABLE_FOR_FIRST_DISPLAY
      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.
      MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                 WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.                             " IF SY-SUBRC NE 0
  ENDIF.                               " IF W_GRID IS INITIAL
*  Registering the EDIT Event

  CALL METHOD W_GRID->REGISTER_EDIT_EVENT
    EXPORTING
      I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_MODIFIED
    EXCEPTIONS
      ERROR      = 1
      OTHERS     = 2.
  IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.                                 " IF SY-SUBRC NE 0
ENDMODULE.                             " manage_alv_grid  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*   This module is used to set the PF-Status and title bar             *
*----------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.
  SET PF-STATUS 'ALVS_GUI'.
  SET TITLEBAR 'ALV_TITLE'.
ENDMODULE.                             " STATUS_0100  OUTPUT





Scenario 1: When there is no call to the method REGISTER_EVT_MODIFIED and just go with the calling of the method CHECK_CHANGED_DATA in the PAI event.
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.



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



QQ|Archiver|手机版|小黑屋|www.sapbbs.com    

GMT+8, 2019-4-23 14:54 , Processed in 1.079416 second(s), 28 queries .

声明:本站严禁任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论!

本站内容由网友原创或转载,如果侵犯了您的合法权益,请及时联系处理!© admin@sapbbs.com

快速回复 返回顶部 返回列表