Skip to Content

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

how to validate a field in alv based on enter key

Hi Team,

I am using the 'REUSE_ALV_GRID_DISPLAY' fm to display the ALV. I have one field is an editable and need to validate the same based on the enter key.

I did search online and found few things like changing the PF status and using Enter as the command (This things did not work). But my requirment once the user enters a value in the field and clicks the enter button it should trigger.

Please let me know how can we go about it.

Thanks in Advance.

Regards

Sunil Kumar

Former Member replied

REPORT  ztest.

TYPE-POOLS: slis.

TYPES: BEGIN OF t_data,
         netwr TYPE vbap-netwr,
       END   OF t_data.



DATA: gt_data TYPE TABLE OF t_data,
      gs_data TYPE          t_data.



DATAgt_fieldcatalog      TYPE lvc_t_fcat,
       gs_fieldcatalog      TYPE lvc_s_fcat,

       gt_events            TYPE slis_t_event,
       gs_events            TYPE slis_alv_event.

START-OF-SELECTION.
* Add same data
  gs_data-netwr = '0.12'.
  APPEND gs_data TO gt_data.

  gs_data-netwr = '0.21'.
  APPEND gs_data TO gt_data.

* Create fcat:
  CLEAR gs_fieldcatalog.
  gs_fieldcatalog-fieldname = 'NETWR'.
  gs_fieldcatalog-ref_field = 'NETWR'.
  gs_fieldcatalog-ref_table = 'VBAP'.
  gs_fieldcatalog-currency  = 'EUR'.
  gs_fieldcatalog-edit      = 'X'.
  APPEND gs_fieldcatalog TO gt_fieldcatalog.

* To set ENTER Event!
  gs_events-name = 'CALLER_EXIT'.
  gs_events-form = 'CALLER_EXIT'.
  APPEND gs_events TO gt_events.

* To change the ALV Changed Protocoll
  gs_events-name = 'DATA_CHANGED'.
  gs_events-form = 'DATA_CHANGED'.
  APPEND gs_events TO gt_events.

* Call ALV Grid
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_callback_program = sy-repid
      it_fieldcat_lvc    = gt_fieldcatalog
      it_events          = gt_events
    TABLES
      t_outtab           = gt_data
    EXCEPTIONS
      program_error      = 1
      OTHERS             = 2.


*&---------------------------------------------------------------------*
*&      Form  data_changed
*&---------------------------------------------------------------------*
FORM data_changed USING ir_data_changed TYPE REF TO cl_alv_changed_data_protocol.

  DATA ls_modi TYPE lvc_s_modi.

* Check each modification:
  LOOP AT ir_data_changed->mt_mod_cells INTO ls_modi.
    CASE ls_modi-fieldname.
      WHEN 'NETWR'.
        IF ls_modi-value CS '-'.     "No negative Values pls....
          CALL METHOD ir_data_changed->add_protocol_entry
            EXPORTING
              i_msgid     = '00'
              i_msgty     = 'E'
              i_msgno     = '398'
              i_msgv1     = 'No negativ Values allowed'
              i_msgv2     = ''
              i_msgv3     = ''
              i_msgv4     = ''
              i_fieldname = ls_modi-fieldname
              i_row_id    = ls_modi-row_id.
        ENDIF.
    ENDCASE.
  ENDLOOP.
ENDFORM.                    "data_changed

*&---------------------------------------------------------------------*
*&      Form  caller_exit
*&---------------------------------------------------------------------*
FORM caller_exit USING is_data TYPE slis_data_caller_exit.
* THIS FORM IS CALLED ONCE BEFORE THE ALV GRID IS SHOWN ON DYNPRO!!!!

* Switch to OO_ALV:
  DATA: lr_alv TYPE REF TO cl_gui_alv_grid.

  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = lr_alv.

* Register ENTER as edit event:
  CALL METHOD lr_alv->register_edit_event
    EXPORTING
      i_event_id = cl_gui_alv_grid=>mc_evt_enter.
* pls. use MC_EVT_MODIFIED if leaving the cell should trigger the edit event!
ENDFORM. "CALLER_EXIT

Hope the example make it more clear.

Hendrik

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