ABAP DEMO ALV-监听数据修改

*&---------------------------------------------------------------------*
*& Report  YDEMO_006
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  YDEMO_006.

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.
BREAK LZH.

    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.
  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 LZH.

  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.
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编辑字段的检查,通常包括输入字段的重复性检查和存在性检查,想一想怎么实现
************************************************************************

原文地址:https://www.cnblogs.com/rainysblog/p/11980396.html

时间: 2024-11-05 23:07:49

ABAP DEMO ALV-监听数据修改的相关文章

Angularjs【监听数据的变化】和【如何修改数据】和【数据变化的传播】

一:监听数据的变化: 由于编译仅仅在启动引导时执行一次,这意味着我们的link函数只会被调用一次,那么, 如果数据变化,在界面上将不会有任何反馈,即界面和数据将变得不同步了. 这需要持续监听数据的变化. 好在AngularJS的scope对象可以使用$watch()方法,对建立在其上的变量的变化进行监听: watch(watchExpression,listener,[objectEquality]);watch方法要求传入三个参数: watchExpression - 要监听的表达式,比如:"

$scope.$watch()——监听数据变化

$scope.$watch(watchFn, watchAction, [deepWatch]):监听数据变化,三个参数 --watchFn:监听的对象,一个带有Angular 表达式或者函数的字符串 --watchAction:是一个函数或者表达式,当watchFn 发生变化时会被调用.如果是函数的形式,它将会接收到watchFn 的新旧两个值,以及作用域对象的引用.其函数签名为function(newValue, oldValue, scope) --deepWatch:是否深度监听,可选.

vue教程2-08 自定义键盘信息、监听数据变化vm.$watch

vue教程2-08 自定义键盘信息 @keydown.up @keydown.enter @keydown.a/b/c.... 自定义键盘信息: Vue.directive('on').keyCodes.ctrl=17; Vue.directive('on').keyCodes.myenter=13; @keydown.a/b/c.... <input type="text" @keydown.c="show"> 自定义键盘信息: Vue.directi

Oracle Enterprise Linux 64-bit 下Oracle11g的监听配置修改及测试步骤

测试环境:Oracle Enterprise Linux 64-bit (5.8版本) + Oracle 11g 64位 相关说明: Oracle11g64位软件的安装位置为/u01/app/oracle/product/11.2.0/dbhome_1 ,数据库名为默认的orcl,Linux虚拟机的IP设置为192.168.1.121 一.修改listener.ora文件内容 命令:[[email protected] ~]$ vi /u01/app/oracle/product/11.2.0/

赵雅智_ProviderContent监听数据变化

当程序A在执行insert.update.delete时,通过getContext().getContentResolver().notifyChange(uri, observer)方法来告诉所有注册在该Uri的监听者数据发生改变 参数1uri:注册的uri 参数2observer:注册的监听者 /** * 插入操作 */ @Override public Uri insert(Uri uri, ContentValues values) { if (uriMatcher.match(uri)

vue.js之过滤器,自定义指令,自定义键盘信息以及监听数据变化

一.监听数据变化 1.监听数据变化有两种,深度和浅度,形式如下: vm.$watch(name,fnCb); //浅度 vm.$watch(name,fnCb,{deep:true}); //深度监视 2.实例用法 2.1-1浅度监听:当点击页面,弹出发生变化了,a的值变为1,b的值变为101 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8">

ng 监听数据的变化

$scope.$watch('监听的变量的名称',func) 在angularJs之所以能够实现绑定,是因为angularJS框架在背后为每一个模型数据添加了一个监听,与$watch其实是一个道理. 结果: 代码: <html ng-app="myModule"> <head lang="en"> <meta charset="UTF-8"> <script src="js/angular.js

mvc 缓存 sqlCacheDependency 监听数据变化

对于MVC有Control缓存和Action缓存. 一.Control缓存 Control缓存即是把缓存应用到整个Control上,该Control下的所有Action都会被缓存起来. [OutputCache(Duration = 10)] public class HomeController : Controller { // GET: Home public ActionResult Index() { ViewBag.CurrentTime = DateTime.Now; return

computed 和 watch 组合使用,监听数据全局数据状态

我要实现的就是,当接口返回数据时,我在任何组件中都能感知到到该数据的变化,然后根据业务逻辑进行处理.展示. 实现这个效果的方式很多,比如当接口返回数据后,就emit这数据,在另外组件中on接收渲染即可,但是我不想用这种, 所以就换了另外一种方式:当接口返回数据时,将数据commit到state,需要对其作出反应的组件中,使用computed将 计算属性将混入到 Vue 实例中.同时使用watch对其属性变化进行监听,且进行相关处理. 相关代码如下: 接口返回数据后,commit更新state中的

vue17 $watch 监听数据变化

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>智能社--http://www.zhinengshe.com</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, ma