- 当用户要根据ALV进行某些功能操作比如打印表单时,OOALV标准按钮无法满足用户需求的时候,就要用到自定义按钮来实现了。
- 思路:在OOALV增加一个自定义按钮,类CL_GUI_ALV_GRID提供了内置事件toolbar来完成,通过自定义按钮的命令码实现OOALV联动,类CL_GUI_ALV_GRID提供了内置事件user_command来完成。
- 实现步骤:
- 增加自定义按钮:
CLASS cl_event DEFINITION. PUBLIC SECTION. METHODS handle_toolbar FOR EVENT toolbar OF cl_gui_alv_grid IMPORTING e_object e_interactive. ENDCLASS. CLASS cl_event IMPLEMENTATION. METHOD handle_toolbar. DATA ls_button TYPE stb_button. CLEAR:ls_button. ls_button-function = 'PRINT'. "功能码 ls_button-text = '打印成绩单'. "文本描述 ls_button-icon = icon_print. "图标 ls_button-quickinfo = '以PDF格式打印'."鼠标悬停在按钮时的简要说明 " ls_button-disabled = 1. "是否可用 1/0 "按钮类型:0默认 1菜单和缺省 2菜单 3分隔符 4单选按钮 5复选框 6菜单项 ls_button-butn_type = 0. APPEND ls_button TO e_object->mt_toolbar. ENDMETHOD. ENDCLASS.
- 按钮命令事件处理
CLASS cl_event DEFINITION. PUBLIC SECTION. METHODS handle_user_command FOR EVENT user_command OF cl_gui_alv_grid IMPORTING e_ucomm. " 用户命令码 ENDCLASS. CLASS cl_event IMPLEMENTATION. METHOD handle_user_command. CASE e_ucomm. WHEN 'PRINT'. MESSAGE '你点击了自定义打印按钮' TYPE 'I'. WHEN OTHERS. ENDCASE. ENDMETHOD. ENDCLASS.
- 通过事件处理类的实例对象给OOALVDUI的实例对象设置注册事件
CREATE OBJECT go_con EXPORTING container_name = 'CONTORL'. CREATE OBJECT go_alv EXPORTING I_parent = go_con. DATA(lo_event) = NEW cl_event( ). SET HANDLER lo_event->handle_toolbar FOR go_alv. SET HANDLER lo_event->handle_user_command FOR go_alv.