Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

OOPS ALV on enter

Hi,

When i hit enter some logic should be executed in OOPS ALV, could you please let me know how can i do this. I wrote the code in PAI, but it was not triggering when i hit enter.

Thanks

Tags:
Former Member
Former Member replied

Hello Jaya

When you register the EDIT event then pushing the ENTER button triggers even DATA_CHANGED.

*§3.Optionally register ENTER to raise event DATA_CHANGED.
*   (Per default the user may check data by using the check icon).
  CALL METHOD go_grid->register_edit_event
    EXPORTING
      i_event_id = cl_gui_alv_grid=>mc_evt_enter.

In order to trigger PAI after raising event DATA_CHANGED (which normally is NOT triggered) I use the following method call within the event handler method:

  METHOD handle_data_changed.

    " Just trigger PAI followed by PBO
    CALL METHOD cl_gui_cfw=>set_new_ok_code
      EXPORTING
        new_code = 'REFRESH'
*        IMPORTING
*          rc       =
        .

  ENDMETHOD.                    "handle_data_changed

A full-fledged sample report is shown below:

*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_ALV_EDITABLE_1B
*&
*&---------------------------------------------------------------------*
*& Thread: OOPS ALV on enter
*& <a class="jive_macro jive_macro_thread" href="" __jive_macro_name="thread" modifiedtitle="true" __default_attr="1067549"></a>
*&
*& Thread: Display only cell in ALV Grid in OO
*& <a class="jive_macro jive_macro_thread" href="" __jive_macro_name="thread" modifiedtitle="true" __default_attr="765083"></a>
*&---------------------------------------------------------------------*
* Flow logic of screen '100' (no elements, ok-code => gd_okcode ):
**PROCESS BEFORE OUTPUT.
**  MODULE STATUS_0100.
***
**PROCESS AFTER INPUT.
**  MODULE USER_COMMAND_0100.
*&
*& GUI-Status: ok-codes BACK, EXIT, CANC
*&---------------------------------------------------------------------*

REPORT  zus_sdn_alv_editable_1b.


TYPE-POOLS: abap.


CONSTANTS:
  gc_tabname       TYPE tabname  VALUE 'KNB1'.



TYPES: BEGIN OF ty_s_outtab.
INCLUDE TYPE knb1 AS knb1.
TYPES: celltab    TYPE lvc_t_styl.
TYPES: END OF ty_s_outtab.
TYPES: ty_t_outtab    TYPE STANDARD TABLE OF ty_s_outtab
                      WITH DEFAULT KEY.

DATA:
  gd_okcode        TYPE ui_func,
  gd_repid         TYPE syst-repid,
*
  gt_fcat          TYPE lvc_t_fcat,
  gs_layout        TYPE lvc_s_layo,
  gs_variant       TYPE disvariant,
  go_docking       TYPE REF TO cl_gui_docking_container,
  go_grid          TYPE REF TO cl_gui_alv_grid.


DATA:
  gs_outtab        TYPE ty_s_outtab,
  gt_outtab        TYPE ty_t_outtab,
  gt_outtab_pbo    TYPE ty_t_outtab.

DATA:
  gd_answer        TYPE c.


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

ENDCLASS.                    "lcl_eventhandler DEFINITION


*----------------------------------------------------------------------*
*       CLASS lcl_eventhandler IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_eventhandler IMPLEMENTATION.

  METHOD handle_data_changed.

    " Just trigger PAI followed by PBO
    CALL METHOD cl_gui_cfw=>set_new_ok_code
      EXPORTING
        new_code = 'REFRESH'
*        IMPORTING
*          rc       =
        .

  ENDMETHOD.                    "handle_data_changed
ENDCLASS.                    "lcl_eventhandler IMPLEMENTATION


START-OF-SELECTION.

  SELECT * FROM  (gc_tabname) INTO CORRESPONDING FIELDS
                              OF TABLE gt_outtab UP TO 99 ROWS.

  gt_outtab_pbo = gt_outtab.  " set PBO data


  PERFORM init_controls.



* ok-code field = GD_OKCODE
  CALL SCREEN '0100'.


END-OF-SELECTION.



*&---------------------------------------------------------------------*
*&      Form  INIT_CONTROLS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM init_controls .

* Create docking container
  CREATE OBJECT go_docking
    EXPORTING
      parent = cl_gui_container=>screen0
      ratio  = 90
    EXCEPTIONS
      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.


* Create ALV grid
  CREATE OBJECT go_grid
    EXPORTING
      i_parent = go_docking
    EXCEPTIONS
      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.

  " NOTE: not required
*  set handler:
*    lcl_eventhandler=>handle_data_changed for go_grid.


* Build fieldcatalog and set hotspot for field KUNNR
  PERFORM build_fieldcatalog.

  PERFORM set_layout_and_variant.

  PERFORM set_cell_editable.


*§3.Optionally register ENTER to raise event DATA_CHANGED.
*   (Per default the user may check data by using the check icon).
  CALL METHOD go_grid->register_edit_event
    EXPORTING
      i_event_id = cl_gui_alv_grid=>mc_evt_enter.

  SET HANDLER: lcl_eventhandler=>handle_data_changed FOR go_grid.

* Display data
  CALL METHOD go_grid->set_table_for_first_display
    EXPORTING
      is_layout       = gs_layout
      is_variant      = gs_variant
      i_save          = 'A'
    CHANGING
      it_outtab       = gt_outtab
      it_fieldcatalog = gt_fcat
    EXCEPTIONS
      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.

* NOTE:
* Documenation of I_SAVE ("An Easy Reference for ALV Grid Control")
*I_SAVE
*Determines the options available to the user for saving a layout:
*? 'X': global saving only
*? 'U': user-specific saving only
*? 'A': corresponds to 'X' and 'U'
*? SPACE: no saving



* Link the docking container to the target dynpro
  gd_repid = syst-repid.
  CALL METHOD go_docking->link
    EXPORTING
      repid                       = gd_repid
      dynnr                       = '0100'
*      CONTAINER                   =
    EXCEPTIONS
      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.

ENDFORM.                    " INIT_CONTROLS

*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  SET PF-STATUS 'STATUS_0100'.
*  SET TITLEBAR 'xxx'.


ENDMODULE.                 " STATUS_0100  OUTPUT

*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.

  " NOTE: retrieve changed data from frontend (grid control) into
  "       the backend (itab in ABAP)
  go_grid->check_changed_data( ).

  CASE gd_okcode.
    WHEN 'BACK'  OR
         'EXIT'  OR
         'CANC'.
      SET SCREEN 0. LEAVE SCREEN.

    WHEN 'REFRESH'.
      PERFORM set_cell_editable.
      " NOTE: Refresh required
      CALL METHOD go_grid->refresh_table_display
*        EXPORTING
*          is_stable      =
*          i_soft_refresh =
*        EXCEPTIONS
*          finished       = 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.



    WHEN 'SAVE'.
      IF ( gt_outtab = gt_outtab_pbo ).
        MESSAGE 'No data changed' TYPE 'S'.

      ELSE.
        CLEAR: gd_answer.
        CALL FUNCTION 'POPUP_TO_CONFIRM'
          EXPORTING
*             TITLEBAR                    = ' '
*             DIAGNOSE_OBJECT             = ' '
            text_question               = 'Save data?'
*             TEXT_BUTTON_1               = 'Ja'(001)
*             ICON_BUTTON_1               = ' '
*             TEXT_BUTTON_2               = 'Nein'(002)
*             ICON_BUTTON_2               = ' '
*             DEFAULT_BUTTON              = '1'
*             DISPLAY_CANCEL_BUTTON       = 'X'
*             USERDEFINED_F1_HELP         = ' '
*             START_COLUMN                = 25
*             START_ROW                   = 6
*             POPUP_TYPE                  =
*             IV_QUICKINFO_BUTTON_1       = ' '
*             IV_QUICKINFO_BUTTON_2       = ' '
          IMPORTING
            answer                      = gd_answer
*           TABLES
*             PARAMETER                   =
          EXCEPTIONS
            text_not_found              = 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 ( gd_answer = '1' ).  " yes
          MESSAGE 'Data successfully saved' TYPE 'S'.

          gt_outtab_pbo = gt_outtab.  " update PBO data !!!
        ELSE.
          MESSAGE 'Action cancelled by user'  TYPE 'S'.
        ENDIF.

      ENDIF.


    WHEN OTHERS.
  ENDCASE.

  CLEAR: gd_okcode.

ENDMODULE.                 " USER_COMMAND_0100  INPUT


*&---------------------------------------------------------------------*
*&      Form  BUILD_FIELDCATALOG
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM build_fieldcatalog .
* define local data
  DATA:
    ls_fcat        TYPE lvc_s_fcat.

  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
*     I_BUFFER_ACTIVE              =
      i_structure_name             = gc_tabname
*     I_CLIENT_NEVER_DISPLAY       = 'X'
*     I_BYPASSING_BUFFER           =
*     I_INTERNAL_TABNAME           =
    CHANGING
      ct_fieldcat                  = gt_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.

  ls_fcat-edit = abap_true.
  MODIFY gt_fcat FROM ls_fcat
      TRANSPORTING edit
    WHERE ( key NE abap_true ).

  READ TABLE gt_fcat INTO ls_fcat
       WITH KEY fieldname = 'UPTIM'.
  IF ( syst-subrc = 0 ).
    DELETE gt_fcat INDEX syst-tabix.

    ls_fcat-no_zero = abap_true.  " suppresses 00:00:00
    INSERT ls_fcat INTO gt_fcat INDEX 7.
  ENDIF.

  READ TABLE gt_fcat INTO ls_fcat
   WITH KEY fieldname = 'UPDAT'.
  IF ( syst-subrc = 0 ).
    DELETE gt_fcat INDEX syst-tabix.
    INSERT ls_fcat INTO gt_fcat INDEX 7.
  ENDIF.

  LOOP AT gt_fcat INTO ls_fcat.
    ls_fcat-col_pos = syst-tabix.

    MODIFY gt_fcat FROM ls_fcat INDEX syst-tabix.
    IF ( syst-tabix > 10 ).
      DELETE gt_fcat INDEX syst-tabix.
    ENDIF.
  ENDLOOP.

ENDFORM.                    " BUILD_FIELDCATALOG


*&---------------------------------------------------------------------*
*&      Form  SET_LAYOUT_AND_VARIANT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM set_layout_and_variant .

  CLEAR: gs_layout,
         gs_variant.

  gs_layout-cwidth_opt = abap_true.
  gs_layout-zebra      = abap_true.
  "
*§3.Provide the fieldname of the celltab field by using field
*   STYLEFNAME of the layout structure.
  gs_layout-stylefname = 'CELLTAB'.

  gs_variant-report = syst-repid.
  gs_variant-handle = 'GRID'.

ENDFORM.                    " SET_LAYOUT_AND_VARIANT
*&---------------------------------------------------------------------*
*&      Form  SET_CELL_EDITABLE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM set_cell_editable .
* define local data
  DATA: ld_idx      TYPE i,
        ls_outtab   TYPE ty_s_outtab,
        ls_style    TYPE lvc_s_styl.


  LOOP AT gt_outtab INTO ls_outtab.
    ld_idx = syst-tabix.

    REFRESH: ls_outtab-celltab.


    IF ( ls_outtab-erdat+0(4) = '2008' ).
      CLEAR: ls_style.
      ls_style-fieldname = 'UPDAT'.
      ls_style-style     = cl_gui_alv_grid=>mc_style_disabled.
      INSERT ls_style INTO TABLE ls_outtab-celltab.
*
      CLEAR: ls_style.
      ls_style-fieldname = 'UPTIM'.
      ls_style-style     = cl_gui_alv_grid=>mc_style_disabled.
      INSERT ls_style INTO TABLE ls_outtab-celltab.
    ELSE.
    ENDIF.

    MODIFY gt_outtab FROM ls_outtab INDEX ld_idx
      TRANSPORTING celltab.
  ENDLOOP.

ENDFORM.                    " SET_CELL_EDITABLE

Regards

Uwe

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question