Tuesday, November 23, 2010

OO ALV Event DATA_CHANGED

Download .nugg file using link below and install this to your system using SAPLink.
SAPLink Package of Application

After installing active
1. ZFSL_OO_ALV_DATA_CHANGED_EVENT Main Program
2. 9001 Screen
3. MAIN_PF_STATUS PF-STATUS
4. MAIN_TITLE TITLEBAR

* Main Program
*&---------------------------------------------------------------------*
*& Report  ZFSL_OO_ALV_DATA_CHANGED_EVENT
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zfsl_oo_alv_data_changed_event.

DATA: ok_code_9001  LIKE sy-ucomm,
      field         TYPE c LENGTH 50,
      line          TYPE i,
      offset        TYPE i.

DATA: alv_grid  TYPE REF TO cl_gui_alv_grid,
      alv_custom_container TYPE REF TO cl_gui_custom_container.

DATA: it_fcat TYPE lvc_t_fcat,
      wa_fcat LIKE LINE OF it_fcat.

DATA: l_valid TYPE char1.

DATA: it_t247 LIKE STANDARD TABLE OF t247,
      wa_t257 LIKE t247,
      it_outtab LIKE STANDARD TABLE OF t247,
      wa_outtab LIKE t247.

CLASS: lcl_event_receiver DEFINITION DEFERRED.
DATA: event_receiver TYPE REF TO lcl_event_receiver.

*----------------------------------------------------------------------*
*       CLASS lcl_event_receiver DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event_receiver DEFINITION.
  PUBLIC SECTION.
    METHODS:
          handle_data_changed FOR EVENT data_changed OF cl_gui_alv_grid IMPORTING er_data_changed.
ENDCLASS.                    "lcl_event_receiver DEFINITION


*----------------------------------------------------------------------*
*       CLASS lcl_event_receiver IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event_receiver IMPLEMENTATION.
  METHOD handle_data_changed.
    BREAK-POINT.
  ENDMETHOD.                    "handle_data_changed
ENDCLASS.                    "lcl_event_receiver IMPLEMENTATION


************************************************************************
*                        START-OF-SELECTION                            *
************************************************************************
START-OF-SELECTION.
  CALL SCREEN 9001.

************************************************************************
*                       PROCESS BEFORE OUTPUT                          *
************************************************************************
*&---------------------------------------------------------------------*
*&      Module  DATA_SELECTION_9001  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE data_selection_9001 OUTPUT.

  IF it_t247[] IS INITIAL.
    SELECT *
      INTO CORRESPONDING FIELDS OF TABLE it_t247
      FROM t247
      WHERE spras = 'E'.
    it_outtab[] = it_t247[].
  ENDIF.

ENDMODULE.                 " DATA_SELECTION_9001  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  STATUS_9001  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE status_9001 OUTPUT.
  SET PF-STATUS 'MAIN_PF_STATUS'.
  SET TITLEBAR 'MAIN_TITLE'.

  IF it_fcat[] IS INITIAL.
    PERFORM fill_fcat.
  ENDIF.

  IF alv_custom_container IS INITIAL.
    PERFORM show_alv.
  ENDIF.

ENDMODULE.                 " STATUS_9001  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  CLEAR_OK_CODE_9001  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE clear_ok_code_9001 OUTPUT.
  CLEAR: ok_code_9001.
ENDMODULE.                 " CLEAR_OK_CODE_9001  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  SET_CURSOR_FIELD_9001  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE set_cursor_field_9001 OUTPUT.
  SET CURSOR FIELD field LINE line OFFSET offset.
ENDMODULE.                 " SET_CURSOR_FIELD_9001  OUTPUT

************************************************************************
*                        PROCESS AFTER INPUT                           *
************************************************************************
*&---------------------------------------------------------------------*
*&      Module  EXIT_COMMAND_9001  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE exit_command_9001 INPUT.
  CASE ok_code_9001.
    WHEN 'BACK' OR 'EXIT' OR 'CANCEL' .
      LEAVE PROGRAM.
  ENDCASE.
ENDMODULE.                 " EXIT_COMMAND_9001  INPUT
*&---------------------------------------------------------------------*
*&      Module  GET_CURSOR_FIELD_9001  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE get_cursor_field_9001 INPUT.
  GET CURSOR FIELD field LINE line OFFSET offset.
ENDMODULE.                 " GET_CURSOR_FIELD_9001  INPUT
*&---------------------------------------------------------------------*
*&      Module  IT_OPERATION_9001  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE it_operation_9001 INPUT.

ENDMODULE.                 " IT_OPERATION_9001  INPUT

*&---------------------------------------------------------------------*
*&      Module  SAVE_DATA_9001  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE save_data_9001 INPUT.
  CASE ok_code_9001.
    WHEN 'SAVE'.
      CALL METHOD alv_grid->check_changed_data
        IMPORTING
          e_valid = l_valid.
      IF l_valid = 'X'.
        BREAK-POINT.
      ENDIF.
    WHEN OTHERS.
  ENDCASE.
ENDMODULE.                 " SAVE_DATA_9001  INPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_9001  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_9001 INPUT.

ENDMODULE.                 " USER_COMMAND_9001  INPUT


************************************************************************
*                                FORMs                                 *
************************************************************************
*&---------------------------------------------------------------------*
*&      Form  SHOW_ALV
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM show_alv .
  CREATE OBJECT alv_custom_container
    EXPORTING
      container_name = 'ALV_CONTROL'.

  CREATE OBJECT alv_grid
    EXPORTING
      i_parent = alv_custom_container.

  CREATE OBJECT event_receiver.
  SET HANDLER event_receiver->handle_data_changed FOR alv_grid.

  CALL METHOD alv_grid->set_table_for_first_display
    CHANGING
      it_outtab       = it_outtab
      it_fieldcatalog = it_fcat.
ENDFORM.                    " SHOW_ALV

*&---------------------------------------------------------------------*
*&      Form  FILL_FCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM fill_fcat .
  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
      i_structure_name       = 'T247'
    CHANGING
      ct_fieldcat            = it_fcat
    EXCEPTIONS
      inconsistent_interface = 1
      program_error          = 2
      OTHERS                 = 3.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  LOOP AT it_fcat INTO wa_fcat WHERE fieldname = 'LTX'.
    wa_fcat-edit = 'X'.
    MODIFY it_fcat FROM wa_fcat INDEX sy-tabix.
  ENDLOOP.
ENDFORM.                    "FILL_FCAT

* Flow Logic of Screen 9001
*********************************************************************
*                    PROCESS BEFORE OUTPUT                          *
*********************************************************************
PROCESS BEFORE OUTPUT.
  MODULE data_selection_9001.
  MODULE status_9001.
  MODULE clear_ok_code_9001.
  MODULE set_cursor_field_9001.

*********************************************************************
*                     PROCESS AFTER INPUT                           *
*********************************************************************
PROCESS AFTER INPUT.
  MODULE exit_command_9001 AT EXIT-COMMAND.
  MODULE get_cursor_field_9001.
  MODULE it_operation_9001.
  MODULE save_data_9001.
  MODULE user_command_9001.