在使用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.