abap alv 事件

alv 事件的创建使用

function alv 最后 还是变成oo alv 来设置监听 事件

在实际开发中经常要用到 事件 event,在各个alv中的具体使用如下

REUSE_ALV_GRID_DISPLAY_LVC

显示代码

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
                  EXPORTING
                    i_callback_program                 = sy-repid          " 当前程序
                    i_callback_pf_status_set           = 'F_PF_STATUS'
                    i_callback_user_command            = 'F_USER_COMMAND'
                    is_layout_lvc                      = lx_layout
                    it_fieldcat_lvc                    = gt_fieldcatalog
*                    IT_EXCLUDING                      =
*                    IT_SPECIAL_GROUPS_LVC             =
*                    IT_SORT_LVC                       =
*                    it_filter_lvc                     = gt_filt
*                    IT_HYPERLINK                      =
*                    IS_SEL_HIDE                       =
*                    I_DEFAULT                         = 'X'
                    i_save                             = 'A'
                    it_events                         = gt_events
                   TABLES
                    t_outtab                          = lt_download.

创建事件

data: gt_events TYPE slis_t_event,"事件
      gs_events LIKE LINE OF gt_events.
      
        gs_events-name = 'CALLER_EXIT'.
        gs_events-form = 'CALLER_EXIT'.
        APPEND gs_events TO gt_events.

实现事件 (form 名称与前面的 gs_events-form 一致)

FORM caller_exit USING e_grid TYPE slis_data_caller_exit.
  DATA lv_ucomm TYPE sy-ucomm.

*  lv_ucomm = 'SAVE'.

  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = o_grid.

  CALL METHOD o_grid->register_edit_event     "注册GRID事件 修改单元格后事件
    EXPORTING
      i_event_id = cl_gui_alv_grid=>mc_evt_enter"事件回车触发
    EXCEPTIONS
      error      = 1
      OTHERS     = 2.


  CREATE OBJECT gt_event_receiver.
  SET HANDLER gt_event_receiver->handle_modify FOR o_grid.
*  SET HANDLER gt_event_receiver->handle_modify FOR o_grid.

ENDFORM.      

实现注册类

CLASS lcl_event_receiver DEFINITION. "定义类 捕捉各种事件
  PUBLIC SECTION.
    METHODS handle_modify                   "数据改变
    FOR EVENT data_changed OF cl_gui_alv_grid
    IMPORTING er_data_changed.
ENDCLASS.                    "LCL_EVENT_RECEIVER DEFINITION

CLASS lcl_event_receiver IMPLEMENTATION. "实现类 处理事件
* 界面修改事件
  METHOD handle_modify .
    PERFORM frm_data_changed USING er_data_changed.

  ENDMETHOD.                    "HANDLE_MODIFY
ENDCLASS.   

但是在现实中客户是没有那么按规则的行事的。他们根本不按回车键,一般都点击保存,或者ctrl + s 所以你还要写监听code 当按保存时的实现方式。i_callback_user_command = 'F_USER_COMMAND' 这个就是可以获取用户的动作 实现的form 如下代码

FORM f_user_command USING      p_ucomm        TYPE sy-ucomm
                               p_rs_selfield  TYPEs lis_selfield.
    case p_ucomm.
       when 'SAVE' ” 当保存时的动作
          “ 会触发数据更改 ,所以我就直接用代码 实现回车要做的事情
          CALL METHOD o_grid->check_changed_data.
       when '&IC1'” 一般双击操作  call transaction 用来跳转
       
       others.
ENDFORM.

salv设置事件

工具栏事件

添加自定义按钮

DATA:
      lr_functions TYPE REF TO cl_salv_functions_list,
      l_icon TYPE string,
      lr_event TYPE REF TO cl_salv_events_table.
      
lr_functions = lo_salv->get_functions( ).
    lr_functions->set_all( abap_true )."Activate All Generic ALV Functions,将激活所有的ALV内置通用按钮

    l_icon = icon_page_down.
    "附加按钮,只适用于 可控模式 下的SALV
    lr_functions->add_function(
      name = 'ERROR'
      icon = l_icon
      text = 'PO占用'
      tooltip = 'PO占用'
      "按钮存放的位置:这里在右边附加。该参数的其他取值可以查看该方法源码,可以
      "找到该参数其他取值的蛛丝马迹
      position = if_salv_c_function_position=>right_of_salv_functions ).

为alv 注册事件 实现事件

data: lr_event TYPE REF TO cl_salv_events_table.
    lr_event = lo_salv->get_event( ).
         "=====事件注册
    CREATE OBJECT lr_handle_event.
    SET HANDLER lr_handle_event->on_user_command FOR lr_event.
    
"=========== 实现事件
CLASS l_cl_handle_events DEFINITION.
  PUBLIC SECTION.
    METHODS: on_user_command FOR EVENT added_function OF cl_salv_events_table
                  IMPORTING e_salv_function.
ENDCLASS.

CLASS l_cl_handle_events IMPLEMENTATION.
  METHOD on_user_command.
    PERFORM handle_user_command USING e_salv_function.
  ENDMETHOD.
ENDCLASS.
 DATA    lr_handle_event TYPE REF TO l_cl_handle_events.
 
 
FORM handle_user_command USING p_function TYPE salv_de_function.
 case p_function.
 when ''.
 other ''.
 
ENDFORM.

你可能感兴趣的:(abap alv 事件)