abap 添加alv上的工具栏的按钮_abap系列-ALV拦截标准工具栏的点击事件

本文介绍了如何在ABAP中重写ALV网格控件的`dispatch`方法,以便在用户点击标准工具栏按钮(如排序)前触发自定义事件。通过创建一个继承自`cl_gui_alv_grid`的类,并定义`before_sys_button_click`和`after_sys_button_click`事件,可以在用户点击按钮时进行预处理或后处理操作。示例代码展示了如何在排序按钮被点击时弹出消息框。
摘要由CSDN通过智能技术生成

在使用ALV进行输出时,有时需要在用户点击标准按钮前进行一些处理.

例如,用户点击排序按钮前,弹出一个消息框提示用户等,然而系统没有提供事件来捕获这样的点击事件.

通过查看代码标准代码发现,所有的事件都是通过dispatch这个公共方法处理的,于是我们可以通过继承来重定义这个方法,从而实现我们需要的效果.

代码如下:

report ysltest2.

class lcl_alv definition inheriting from cl_gui_alv_grid.

public section.

methods dispatch redefinition .

events: befor_sys_button_click exporting value(e_ucomm) type syst-ucomm,

after_sys_button_click exporting value(e_ucomm) type syst-ucomm.

endclass.

class lcl_alv implementation.

method dispatch.

data: action type sy-ucomm.

if eventid = evt_toolbar_button_click.

call method get_event_parameter

exporting

parameter_id = 0

queue_only = space

importing

parameter = action.

raise event befor_sys_button_click exporting e_ucomm = action.

super->dispatch( cargo = cargo eventid = eventid is_shellevent = is_shellevent is_systemdispatch = is_systemdispatch ).

raise event after_sys_button_click exporting e_ucomm = action.

else.

super->dispatch( cargo = cargo eventid = eventid is_shellevent = is_shellevent is_systemdispatch = is_systemdispatch ).

endif.

endmethod.

endclass.

class lcl_event definition .

public section.

class-methods: befor_sort_click for event befor_sys_button_click of lcl_alv importing e_ucomm sender.

endclass.

class lcl_event implementation.

method befor_sort_click.

case e_ucomm .

when sender->mc_fc_sort or

sender->mc_fc_sort_asc or

sender->mc_fc_sort_dsc.

message 'sort button clicked' type 'I'.

endcase.

endmethod.

endclass.

data o_con type ref to cl_gui_docking_container.

data gt_makt type table of makt.

data ls_row_no type lvc_s_roid.

data ls_col_info type lvc_s_col.

data gt_fcat type lvc_t_fcat.

data gs_fcat type lvc_s_fcat.

data o_alv type ref to lcl_alv.

define __fcat.

gs_fcat-fieldname = &1.

gs_fcat-reptext = &2.

gs_fcat-ref_table = 'MAKT'.

gs_fcat-ref_field = &1.

append gs_fcat to gt_fcat.

end-of-definition.

parameter p_p1 type c.

at selection-screen output.

if o_con is initial.

create object :

o_con exporting side = cl_gui_docking_container=>dock_at_bottom ratio = 90,

o_alv exporting i_parent = o_con .

__fcat:

'MATNR' '物料号',

'MAKTX' '物料描述'.

set handler lcl_event=>befor_sort_click for o_alv .

o_alv->set_table_for_first_display( changing it_outtab = gt_makt it_fieldcatalog = gt_fcat ).

endif.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值