本文主要讲解添加自定义工具栏。
跟双击一样,添加自定义的工具栏其实也是在CLASS里面添加方法并实现之。
引进图标:
INCLUDE <ICON>.
定义:
* 定义事件属性跟方法
CLASS LCL_EVENT_RECEIVER DEFINITION.
PUBLIC SECTION.
METHODS:
* 双击方法
HANDLE_DOUBLE_CLICK
FOR EVENT DOUBLE_CLICK OF CL_GUI_ALV_GRID
IMPORTING E_ROW E_COLUMN,
* 按钮处理方法
HANDLE_TOOLBAR
FOR EVENT TOOLBAR OF CL_GUI_ALV_GRID
IMPORTING E_OBJECT E_INTERACTIVE,
* 用户交互事件处理
HANDLE_USER_COMMAND
FOR EVENT USER_COMMAND OF CL_GUI_ALV_GRID
IMPORTING E_UCOMM.
ENDCLASS. "LCL_EVENT_RECEIVER DEFINITION
实现:
*----------------------------------------------------------------------*
* CLASS LCL_EVENT_RECEIVER IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS LCL_EVENT_RECEIVER IMPLEMENTATION.
* 双击方法实现
METHOD HANDLE_DOUBLE_CLICK.
PERFORM GETRECORD USING E_ROW-INDEX E_COLUMN-FIELDNAME.
ENDMETHOD. "HANDLE_DOUBLE_CLICK
* 处理按钮事件
METHOD HANDLE_TOOLBAR.
DATA: LS_TOOLBAR TYPE STB_BUTTON.
CLEAR LS_TOOLBAR.
MOVE 3 TO LS_TOOLBAR-BUTN_TYPE.
* 取工具条接口
APPEND LS_TOOLBAR TO E_OBJECT->MT_TOOLBAR.
CLEAR LS_TOOLBAR.
*加入功能码
MOVE 'SHOW_DETA' TO LS_TOOLBAR-FUNCTION.
*图标,可以用SHOWICON系统程序查阅使用的图标
MOVE ICON_PPE_ENODE TO LS_TOOLBAR-ICON.
*按钮文本和提示
MOVE '明细显示' TO LS_TOOLBAR-QUICKINFO.
MOVE '明细显示'(112) TO LS_TOOLBAR-TEXT.
MOVE '' TO LS_TOOLBAR-DISABLED.
*添加到工具条
APPEND LS_TOOLBAR TO E_OBJECT->MT_TOOLBAR.
ENDMETHOD. "HANDLE_TOOLBAR
* 处理功能码事件
METHOD HANDLE_USER_COMMAND.
DATA: LT_ROWS TYPE LVC_T_ROW.
CASE E_UCOMM.
* 定义的按钮事件
WHEN 'SHOW_DETA'.
*取得所有选择行
CALL METHOD GR_ALVGRID->GET_SELECTED_ROWS
IMPORTING
ET_INDEX_ROWS = LT_ROWS.
CALL METHOD CL_GUI_CFW=>FLUSH.
IF SY-SUBRC EQ 0.
* 提示成功
MESSAGE '已经选择行!' TYPE 'S'.
*调用子程序显示行内容
PERFORM READDATA TABLES LT_ROWS.
ENDIF.
ENDCASE.
ENDMETHOD. "HANDLE_USER_COMMAND "HANDLE_DOUBLE_CLICK
ENDCLASS. "LCL_EVENT_RECEIVER IMPLEMENTATION
* 显示选择行内容模版
FORM READDATA TABLES P_ET_INDEX_ROWS STRUCTURE LVC_S_ROW.
* 定义选择行
DATA: LS_SELECTED_LINE LIKE LVC_S_ROW,
LF_ROW_INDEX TYPE LVC_INDEX.
*定义合并字段及分割字段
DATA: S1(200) TYPE C,
S2(3) TYPE C.
S2 = '-'.
*读取所有选择行
LOOP AT P_ET_INDEX_ROWS INTO LS_SELECTED_LINE.
LF_ROW_INDEX = LS_SELECTED_LINE-INDEX.
* 从内表中按顺序读取内容
READ TABLE IT_SFLIGHT INDEX LF_ROW_INDEX.
S1 = '选择行内容:'.
* 合并
CONCATENATE S1 IT_SFLIGHT-CONNID IT_SFLIGHT-CARRID
INTO S1 SEPARATED BY S2.
* 弹出窗口提示
MESSAGE S1 'I'.
ENDLOOP.
ENDFORM.
当然,最后还得在刷新alv前添加:
* 将用户交互事件分配ALV
SET HANDLER EVENT_RECEIVER->HANDLE_USER_COMMAND FOR GR_ALVGRID.
* 将按钮相关事件分配ALV
SET HANDLER EVENT_RECEIVER->HANDLE_TOOLBAR FOR GR_ALVGRID.
* 调用处理工具条处理方法,使自定义工具条生效
CALL METHOD GR_ALVGRID->SET_TOOLBAR_INTERACTIVE.