之前发过一个利用alv公共方法重定义来拦截标准工具栏点击事件的方法。
abap系列-ALV拦截标准工具栏的点击事件
现在介绍另一种方法:
通过自定义按钮替换标准按钮,然后在点击自定义按钮时发送标准按钮事件命令。
这两种方法各有优点:
- 第一种方法不需要隐藏标准按钮,可以针对工具栏所有按钮实现,
- 第二种方法不需要重写类方法,对于只需要处理某个功能时较简单.
方法二实现:
1. 隐藏需要拦截的标准按钮;
2. 添加自定义按钮及相关事件;
3. 使用方法在响应自定义按钮事件的代码里,调用set_user_command方法发送标准按钮事件命令。
示例代码如下图
class lcl_event definition .
public section.
class-methods:
toolbar for event toolbar of cl_gui_alv_grid importing e_object e_interactive,
befor_sort_click for event user_command of cl_gui_alv_grid importing e_ucomm sender.
endclass.
class lcl_event implementation.
method befor_sort_click.
message 'Before sort button click.' type 'I'.
sender->set_user_command( sender->mc_fc_sort ).
endmethod.
method toolbar.
data ls_toolbar type stb_button.
ls_toolbar-function = 'SORT'.
ls_toolbar-text = 'SORT'.
append ls_toolbar to e_object->mt_toolbar .
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 cl_gui_alv_grid.
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 lcl_event=>toolbar for o_alv .
o_alv->set_table_for_first_display( changing it_outtab = gt_makt it_fieldcatalog = gt_fcat ).
endif.
网友评论