Wednesday, November 10, 2010

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.  
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.

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.
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
ref: http://wiki.sdn.sap.com/wiki/display/ABAP/Making+ALV+to+react+to+Change+data+automatically

0 comments: