OOALV data_changed 與data_changed_finished事件

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

data_changed在可編輯字段的數據發生變化時才會觸發。可用來檢查輸入數據的正確性。 data_changed_finished在回車時和可編輯字段數據發生變化后,光標移動時觸發,如果可編輯字段數據檢查失敗,則不會觸發此事件。 這兩個事件都需要調用方法才能觸發。

代码语言:javascript复制
    call METHOD g_grid01->register_edit_event
      EXPORTING
        i_event_id = cl_gui_alv_grid=>mc_evt_enter.//回車觸發事件
代码语言:javascript复制
    call METHOD g_grid01->register_edit_event
      EXPORTING
        i_event_id = cl_gui_alv_grid=>mc_evt_modified.//修改觸發事件

下面是個完整例子,屏幕就只有一個customer control:

代码语言:javascript复制
REPORT Z_TALV.

data: gv_repid TYPE sy-repid,
      gv_sname TYPE sy-dynnr.
DATA: gs_data TYPE spfli.

DATA: gt_data LIKE TABLE OF gs_data.

DATA: g_con01  TYPE REF TO cl_gui_custom_container.

DATA: g_grid01 TYPE REF TO cl_gui_alv_grid.

DATA: gs_fieldcat TYPE lvc_s_fcat.
DATA: gt_fieldcat TYPE lvc_t_fcat.
data: ok_code type sy-ucomm,
      save_ok type sy-ucomm.
CONSTANTS: g_cus01 TYPE scrfname VALUE 'CUS01'.

*----------------------------------------------------------------------*
*       CLASS cl_event_receiver DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_event_receiver DEFINITION.
  PUBLIC SECTION.
    " 声明Toolbar事件方法
    METHODS handle_toolbar
      FOR EVENT toolbar OF cl_gui_alv_grid
      IMPORTING e_object e_interactive.
    METHODS handle_data_changed
      for EVENT data_changed of cl_gui_alv_grid
      IMPORTING
        er_data_changed
        e_onf4
        e_onf4_before
        e_onf4_after
        e_ucomm.
    METHODs handle_data_changed_finished
      for EVENT data_changed_finished of cl_gui_alv_grid
      IMPORTING
        e_modified
        et_good_cells.
ENDCLASS.                    "cl_event_receiver DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_event_receiver IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_event_receiver IMPLEMENTATION.
  " 实现Toolbar事件方法
  METHOD handle_toolbar.
    DATA: ls_toolbar TYPE stb_button.
    CLEAR: ls_toolbar.
    ls_toolbar-butn_type = 3. " 分隔符
    APPEND ls_toolbar TO e_object->mt_toolbar.
    CLEAR: ls_toolbar.
    ls_toolbar-function = 'RESH'.    " 功能码
    ls_toolbar-icon = icon_REFRESH.  " 图标名称
    ls_toolbar-quickinfo = 'Refresh'.   " 图标的提示信息
    ls_toolbar-disabled = ''.        " X表示灰色,不可用
    ls_toolbar-text = ''.       " 按钮上显示的文本
    APPEND ls_toolbar TO e_object->mt_toolbar.
  ENDMETHOD.                    "handle_toolbar
  METHOD handle_data_changed.
    PERFORM handle_data_changed using er_data_changed.
  ENDMETHOD.
  METHOD handle_data_changed_finished.
    MESSAGE 'it is work' TYPE 'I'.
  ENDMETHOD.
ENDCLASS.                    "cl_event_receiver IMPLEMENTATION
DATA: event_receiver TYPE REF TO cl_event_receiver.

START-OF-SELECTION.
  PERFORM get_data.

  CALL SCREEN 100.

*&---------------------------------------------------------------------*
*&      Form  GET_DATA
*&---------------------------------------------------------------------*
*       text  获取数据
*----------------------------------------------------------------------*
FORM get_data.
  SELECT *
    INTO CORRESPONDING FIELDS OF TABLE gt_data
    UP TO 30 ROWS
    FROM spfli.

ENDFORM.                    "GET_DATA

*&---------------------------------------------------------------------*
*&      Form  SET_FIELDCAT
*&---------------------------------------------------------------------*
*       text  ALV显示字段文本
*----------------------------------------------------------------------*
FORM set_fieldcat.
  CLEAR: gt_fieldcat[].
  DEFINE macro_fieldcat.
    clear gs_fieldcat.
    gs_fieldcat-fieldname = &1.
    gs_fieldcat-coltext   = &2.
    gs_fieldcat-hotspot   = &3.
    append gs_fieldcat to gt_fieldcat.
  END-OF-DEFINITION.

  macro_fieldcat 'CARRID'     '航线代码'   'X'.
  macro_fieldcat 'CONNID'     '航班连接Id' ''.
  macro_fieldcat 'COUNTRYFR'  '起飞国家'   ''.
  macro_fieldcat 'CITYFROM'   '起飞城市'   ''.
  macro_fieldcat 'AIRPFROM'   '起飞机场'   ''.
  macro_fieldcat 'COUNTRYTO'  '目标国家'   ''.
  macro_fieldcat 'CITYTO'     '目标城市'   ''.
  macro_fieldcat 'AIRPTO'     '目标机场'   ''.
  macro_fieldcat 'FLTIME'     '航班时间'   ''.
  macro_fieldcat 'DEPTIME'    '启程时间'   ''.
  macro_fieldcat 'ARRTIME'    '到达时间'   ''.
  macro_fieldcat 'DISTANCE'   '距离'       ''.

ENDFORM.                    "SET_FIELDCAT

*&---------------------------------------------------------------------*
*&      Form  SET_LAYOUT
*&---------------------------------------------------------------------*
*       text  布局
*----------------------------------------------------------------------*
*      -->P_LAYOUT   text
*----------------------------------------------------------------------*
FORM set_layout CHANGING p_layout TYPE lvc_s_layo.
  p_layout-cwidth_opt = 'X'.   " 自动调节字段长度
  p_layout-sel_mode   = 'D'.   " 选择模式,可选择多行
  p_layout-zebra      = 'X'.   " 颜色交替显示
  p_layout-edit = 'X'.
ENDFORM.                      "SET_LAYOUT

*&---------------------------------------------------------------------*
*&      Module  INIT_CON  OUTPUT
*&---------------------------------------------------------------------*
*       text  创建ALV
*----------------------------------------------------------------------*
MODULE init_con OUTPUT.

  DATA: ls_layout TYPE lvc_s_layo.

  IF g_con01 IS INITIAL.
    PERFORM set_fieldcat.
    PERFORM set_layout CHANGING ls_layout.

    " 创建SAP容器实例
    CREATE OBJECT g_con01
      EXPORTING
        container_name              = g_cus01 " 自定义控件名称
      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.
    " 创建GRID实例
    CREATE OBJECT g_grid01
      EXPORTING
        i_parent          = g_con01
      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.

    CREATE OBJECT event_receiver.
    " 注册事件handler方法
    SET HANDLER event_receiver->handle_toolbar FOR g_grid01.
    set HANDLER event_receiver->handle_data_changed for g_grid01.
    set HANDLER event_receiver->handle_data_changed_finished FOR g_grid01.
    call METHOD g_grid01->register_edit_event
      EXPORTING
        i_event_id = cl_gui_alv_grid=>mc_evt_enter."回車觸發事件
    call METHOD g_grid01->register_edit_event
      EXPORTING
        i_event_id = cl_gui_alv_grid=>mc_evt_modified."修改完成后光標移動觸發事件,但修改完后,data_changed檢查輸入錯誤,就不會走這個事件
    " 调用ALV 显示方法
    CALL METHOD g_grid01->set_table_for_first_display
      EXPORTING
*       i_structure_name              = 'SPFLI' " 参照表结构字段显示
        is_layout                     = ls_layout " 布局
      CHANGING
        it_fieldcatalog               = gt_fieldcat " 显示字段
        it_outtab                     = gt_data[]
      EXCEPTIONS
        invalid_parameter_combination = 1
        program_error                 = 2
        too_many_lines                = 3
        OTHERS                        = 4.
    IF sy-subrc <> 0.
*     Implement suitable error handling here
    ENDIF.

  ENDIF.

ENDMODULE.                 " INIT_CON  OUTPUT

*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  SET PF-STATUS 'S0001'.
IF gv_repid is INITIAL.
  gv_repid = 'Z_TALV'.
  gv_sname = '0101'.
ENDIF.
ENDMODULE.                 " STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_EXIT_0100  INPUT
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
MODULE user_exit_0100 INPUT.
  save_ok = ok_code.
  clear ok_code.
  case save_ok.
    when 'BACK'.
      leave program.
    when 'BTN'.
      MESSAGE 'btn' TYPE 'I'.
  endcase.
ENDMODULE.                 " USER_EXIT_0100  INPUT

form handle_data_changed using
                  p_er_data_changed  TYPE ref to cl_alv_changed_data_protocol.
  data: mod_data TYPE lvc_t_modi,
        wa_mod_data TYPE lvc_s_modi.
  mod_data = p_er_data_changed->mt_mod_cells.
  LOOP AT mod_data INTO wa_mod_data WHERE fieldname = 'CONNID'.
    IF strlen( wa_mod_data-value ) > 4.
      call METHOD p_er_data_changed->add_protocol_entry
        EXPORTING
          i_msgid = '00'
          i_msgty = 'E'
          i_msgno = '001'
          i_msgv1 = 'length is too long'
          i_fieldname = wa_mod_data-fieldname.
    ENDIF.
  ENDLOOP.
ENDFORM.

0 人点赞