ALV 监听事件

转载:http://t.zoukankan.com/rainysblog-p-11980396.html

对ALV栏位值进行编辑后,鼠标点到别的栏位时触发事件

如下代码要先创建屏幕号100,不然会报错

DATA: BEGIN OF itab OCCURS 0 ,
        mblnr LIKE mseg-mblnr ,
        kostl LIKE mseg-kostl ,
      END OF itab .

DATA: ok_code LIKE sy-ucomm .
DATA: l_valid(1) TYPE c.

DATA: go_grid             TYPE REF TO cl_gui_alv_grid,
      go_custom_container TYPE REF TO cl_gui_custom_container.
DATA: gt_fieldcat TYPE lvc_t_fcat,
      gs_fieldcat TYPE lvc_s_fcat,
      gs_variant  TYPE disvariant ,
      gt_filt     TYPE lvc_t_filt,
      gs_filt     TYPE lvc_s_filt,
      sla         TYPE lvc_s_layo .

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.
    PERFORM handle_data_changed USING er_data_changed.
  ENDMETHOD.                    "handle_data_changed
ENDCLASS.                    "lcl_event_receiver IMPLEMENTATION

INITIALIZATION.
  PERFORM fieldcat_init USING gt_fieldcat[].

START-OF-SELECTION.
  CALL SCREEN 0100 .

*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
  CASE ok_code.
    WHEN 'BACK' OR 'EXIT'.
      LEAVE PROGRAM.
    WHEN 'ENTE'.
      PERFORM enter.
    WHEN 'CHANGE'.
      PERFORM change.
  ENDCASE.
  CLEAR ok_code .
ENDMODULE.                 " USER_COMMAND_0100  INPUT

*&---------------------------------------------------------------------*
*&      Form  load_data_into_grid
*&---------------------------------------------------------------------*
FORM load_data_into_grid.
  SELECT * FROM mseg INTO CORRESPONDING FIELDS OF TABLE itab
  UP TO 20 ROWS .

  sla-cwidth_opt = 'X'.
  sla-zebra      = 'X'.

  CALL METHOD go_grid->set_table_for_first_display
    EXPORTING
      is_variant                    = gs_variant
      i_save                        = 'A'
      is_layout                     = sla
    CHANGING
      it_outtab                     = itab[]
      it_fieldcatalog               = gt_fieldcat[]
    EXCEPTIONS
      invalid_parameter_combination = 1
      program_error                 = 2
      too_many_lines                = 3
      OTHERS                        = 4.

  CALL METHOD go_grid->set_ready_for_input "处于编辑状态
    EXPORTING
      i_ready_for_input = 1.

  CALL METHOD cl_gui_control=>set_focus    "设置焦点在go_grid 上
    EXPORTING
      control = go_grid .
ENDFORM.                    " load_data_into_grid


*&--------------------------------------------------------------------*
*&      Form  fieldcat_init
*&--------------------------------------------------------------------*
FORM fieldcat_init USING rt_fieldcat TYPE lvc_t_fcat.
  DATA: ls_fieldcat TYPE lvc_s_fcat.

  PERFORM frm_catlg_set USING:
  'MBLNR'  'X' '凭证'       '' 'MBLNR' 'MSEG' rt_fieldcat,
  'KOSTL'  '' '成本中心'   'X'  ''      ''     rt_fieldcat.
ENDFORM.   "fieldcat_init

*---------------------------------------------------------------------*
*       FORM frm_catlg_set                                            *
*---------------------------------------------------------------------*
FORM frm_catlg_set USING p_field p_key p_text p_edit ref_f ref_t
                          rt_fieldcat  TYPE lvc_t_fcat .
  DATA:  tmp_fieldcat TYPE lvc_s_fcat.

  tmp_fieldcat-fieldname     =  p_field.
  tmp_fieldcat-key           =  p_key .
  tmp_fieldcat-scrtext_l     =  p_text.
  tmp_fieldcat-edit          =  p_edit.
  tmp_fieldcat-ref_field     = ref_f.
  tmp_fieldcat-ref_table     = ref_t.

  APPEND tmp_fieldcat TO rt_fieldcat .
  CLEAR tmp_fieldcat .
ENDFORM.                    " FRM_CATLG_SET

*&---------------------------------------------------------------------*
*&      Form  change
*&---------------------------------------------------------------------*
FORM change .
  DATA: ls_stable TYPE lvc_s_stbl.
  ls_stable-row = 'X'.
  ls_stable-col = 'X'.

  IF go_grid->is_ready_for_input( ) = 0.
    CALL METHOD go_grid->set_ready_for_input
      EXPORTING
        i_ready_for_input = 1.
  ELSE.
    CALL METHOD go_grid->check_changed_data  "把修改的内容更改到内表里
      IMPORTING
        e_valid = l_valid.
    CALL METHOD go_grid->set_ready_for_input
      EXPORTING
        i_ready_for_input = 0.
    CALL METHOD go_grid->refresh_table_display
      EXPORTING
        is_stable = ls_stable
      EXCEPTIONS
        finished  = 1
        OTHERS    = 2.
  ENDIF.
ENDFORM.                    " change


*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
*  SET PF-STATUS 'MAIN100'.
  gs_variant-report = sy-repid.

  IF go_grid IS INITIAL.
    CREATE OBJECT go_grid
      EXPORTING
        i_parent = cl_gui_container=>screen0.
    CREATE OBJECT event_receiver.

    SET HANDLER event_receiver->handle_data_changed FOR go_grid.

    IF sy-batch IS INITIAL.
      CALL METHOD go_grid->register_edit_event
        EXPORTING
*          i_event_id = cl_gui_alv_grid=>mc_evt_enter. "回车时触发
          i_event_id = cl_gui_alv_grid=>mc_evt_modified. "单元格更改触发
    ENDIF.

    PERFORM load_data_into_grid.
  ELSE.
    CALL METHOD go_grid->refresh_table_display.
  ENDIF.
ENDMODULE.                 " STATUS_0100  OUTPUT

*&---------------------------------------------------------------------*
*&      Form  ENTER
*&---------------------------------------------------------------------*
FORM enter .
  DATA: ucomm LIKE sy-ucomm.

  ucomm = '&DATA_SAVE'." '&REFRESH'.
  CALL METHOD go_grid->set_function_code
    CHANGING
      c_ucomm = ucomm.
ENDFORM.                    " ENTER

*&---------------------------------------------------------------------*
*&      Form  HANDLE_DATA_CHANGED
*&---------------------------------------------------------------------*
FORM handle_data_changed USING data_changed TYPE REF TO cl_alv_changed_data_protocol..
  FIELD-SYMBOLS: <fs_mod_rows> TYPE STANDARD TABLE,
                 <fs_cells> TYPE lvc_t_modi ,
                 <fs_cell_wa> TYPE lvc_s_modi ,
                 <fs>    LIKE LINE OF itab.
 BREAK-POINT.
  IF NOT data_changed->mp_mod_rows IS INITIAL.
    ASSIGN data_changed->mp_mod_rows->* TO <fs_mod_rows>.
    ASSIGN data_changed->mt_mod_cells TO <fs_cells>.
    LOOP AT <fs_mod_rows> ASSIGNING <fs>.
      IF <fs>-kostl = 'ABC' .
        READ TABLE <fs_cells> INDEX sy-tabix ASSIGNING <fs_cell_wa>.
        CALL METHOD data_changed->add_protocol_entry
          EXPORTING
            i_msgid     = 'OO'
            i_msgty     = 'E'
            i_msgno     = '000'
            i_msgv1     = '不可以为ABC'
            i_fieldname = 'KOSTL'
            i_row_id    = <fs_cell_wa>-row_id
*            i_tabix     = tabix
             .
      ENDIF.
    ENDLOOP.
  ENDIF.
*  CALL METHOD go_grid->refresh_table_display.   "进入这个事件,如果对内表做了任何操作,要刷新一下才能显示最新结果
ENDFORM.                    " HANDLE_DATA_CHANGED

****如下不知道做啥的~~~~~~~
************************************************************************
*5、拓展内容
*CL_ALV_CHANGED_DATA_PROTOCOL参数的常用方法和属性
*
*方法                   用途
*MODIFY_CELL            修改指定位置单元格的值
*GET_CELL_VALUE         获取单元格的值
*ADD_PROTOCOL_ENTRY     弹出一个消息,必须使用系统标准的消息类
*MODIFY_PROTOCOL_ENTRY  修改消息
*PROTOCOL_IS_VISIBLE    是否显示错误信息提示
*REFRESH_PROTOCOL       删除错误消息
*
*属性                   用途
*MT_PROTOCOL            LOGs
*MT_MOD_CELLS           更新的单元格信息
*MP_MOD_ROWS            更新了的行的信息
*MT_GOOD_CELLS          正确的CELL的值
*MT_DELETED_ROWS        被删除的行信息
*MT_INSERTED_ROWS       新插入的行信息

*PS:alv编辑字段的检查,通常包括输入字段的重复性检查和存在性检查,想一想怎么实现
************************************************************************

 

上一篇:闭关修炼:Ansible-二进制-K8S:第二篇


下一篇:WebStorm + Egg.js调试