Function ALV添加data_changed事件

2021-02-24 14:24:04 浏览数 (1)

data_changed在可編輯字段的數據發生變化時才會觸發。可用來檢查輸入數據的正確性。

代码语言:javascript复制
REPORT z_tfalv_data_changed.
TYPE-POOLS: slis.
DATA: gs_fieldcat TYPE slis_fieldcat_alv,
      gt_fieldcat TYPE slis_t_fieldcat_alv.
DATA: gs_layout TYPE slis_layout_alv.
DATA: gt_events TYPE slis_t_event,
      gs_event  TYPE slis_alv_event.
DATA: gt_itab LIKE TABLE OF spfli WITH HEADER LINE.
DEFINE macro_fieldcat.
  CLEAR: gs_fieldcat.
  gs_fieldcat-col_pos = &1.
  gs_fieldcat-fieldname = &2.
  gs_fieldcat-seltext_m   = &3.
  gs_fieldcat-edit   = &4.
  APPEND gs_fieldcat TO gt_fieldcat.
END-OF-DEFINITION.

macro_fieldcat '2' 'CARRID'    'CARRID'    'X'.
macro_fieldcat '3' 'CONNID'    'CONNID'    'X'.
macro_fieldcat '4' 'COUNTRYFR' 'COUNTRYFR' 'X'.
macro_fieldcat '5' 'CITYFROM'  'CITYFROM'  'X'.
macro_fieldcat '6' 'AIRPFROM'  'AIRPFROM'  'X'.
macro_fieldcat '7' 'COUNTRYTO' 'COUNTRYTO' 'X'.
macro_fieldcat '8' 'CITYTO'    'CITYTO'    'X'.
macro_fieldcat '9' 'AIRPTO'    'AIRPTO'    'X'.
macro_fieldcat '10' 'FLTIME'   'FLTIME'    'X'.
macro_fieldcat '11' 'DEPTIME'  'DEPTIME'   'X'.
macro_fieldcat '12' 'ARRTIME'  'ARRTIME'   'X'.
macro_fieldcat '13' 'DISTANCE' 'DISTANCE'  'X'.

MOVE 'DATA_CHANGED' TO gs_event-name.
MOVE 'HANDLE_DATA_CHANGED' TO gs_event-form.
APPEND gs_event TO gt_events.

gs_layout-edit = 'X'.

*START-OF-SELECTION.
  SELECT * FROM spfli
           INTO CORRESPONDING FIELDS OF TABLE gt_itab.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program      = sy-repid
      it_fieldcat   = gt_fieldcat
      is_layout     = gs_layout
      it_events     = gt_events
    TABLES
      t_outtab      = gt_itab
    EXCEPTIONS
      program_error = 1
      OTHERS        = 2.
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.

FORM HANDLE_DATA_CHANGED USING
        pcl_data TYPE REF TO
          cl_alv_changed_data_protocol.
    data: mod_data TYPE lvc_t_modi,
        wa_mod_data TYPE lvc_s_modi.
    data: count type n.
    mod_data = pcl_data->mt_mod_cells.

  LOOP AT mod_data INTO wa_mod_data WHERE fieldname = 'CONNID'.
    IF strlen( wa_mod_data-value ) > 4.
      call METHOD pcl_data->add_protocol_entry
        EXPORTING
          i_msgid = '00'
          i_msgty = 'E'
          i_msgno = '001'
          i_msgv1 = 'length is too123 long'
          i_fieldname = wa_mod_data-fieldname.
    ENDIF.
  ENDLOOP.

ENDFORM.

0 人点赞