实战ALV OO技术实现添加工具条按钮


REPORT  Z_ALV_ERIC.

*定义使用图标,按钮使用
INCLUDE .

*定义事件类型
CLASS LCL_EVENT_RECEIVER DEFINITION DEFERRED.

DATA: OK_CODE TYPE SY-UCOMM,
      SAVE_OK LIKE OK_CODE.

*定义事件
DATA EVENT_RECEIVER TYPE REF TO LCL_EVENT_RECEIVER.

*内表
DATA: WA_SPFLI TYPE TABLE OF SPFLI,
      A_SPFLI LIKE SPFLI.

SELECT * INTO TABLE WA_SPFLI FROM SPFLI.

*定义窗口定制控制,定义ALV对象
DATA: WA_CONTAINER TYPE SCRFNAME VALUE 'ALVDATA',
      ALV_GRID TYPE REF TO CL_GUI_ALV_GRID,
      WA_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER.

CALL SCREEN 1001.


* 事件属性跟方法
CLASS LCL_EVENT_RECEIVER DEFINITION.

*公共事件
  PUBLIC SECTION.
    METHODS:
*     按钮处理方法
      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.
*  PRIVATE SECTION.
ENDCLASS.                    "LCL_EVENT_RECEIVER DEFINITION

*实现方法
CLASS LCL_EVENT_RECEIVER IMPLEMENTATION.

* 处理按钮事件
  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 ALV_GRID->GET_SELECTED_ROWS
          IMPORTING
            ET_INDEX_ROWS = LT_ROWS.
        CALL METHOD CL_GUI_CFW=>FLUSH.
        IF SY-SUBRC EQ 0.
* 提示成功
          MESSAGE S005(YMESS) WITH '已经选择行!'.
*调用子程序显示行内容
          PERFORM  MESSDETA TABLES LT_ROWS.
        ENDIF.
    ENDCASE.
  ENDMETHOD.                    "HANDLE_USER_COMMAND
ENDCLASS.                    "LCL_EVENT_RECEIVER IMPLEMENTATION


* PBO 定义状态条,包括菜单,工具条按钮,系统按钮等
MODULE STATUS_1001 OUTPUT.
  SET PF-STATUS 'STATUS1'.
*  SET TITLEBAR ''.
* 如果窗口还没创建ALV对象则创建
  IF WA_CUSTOM_CONTAINER IS INITIAL.
    CREATE OBJECT WA_CUSTOM_CONTAINER
      EXPORTING
        CONTAINER_NAME = WA_CONTAINER.
    CREATE OBJECT ALV_GRID
      EXPORTING
        I_PARENT = WA_CUSTOM_CONTAINER.
    CALL METHOD ALV_GRID->SET_TABLE_FOR_FIRST_DISPLAY
      EXPORTING
        I_STRUCTURE_NAME = 'SPFLI'
      CHANGING
        IT_OUTTAB        = WA_SPFLI.
*   创建事件
    CREATE OBJECT EVENT_RECEIVER.
*   将用户交互事件分配ALV
    SET HANDLER EVENT_RECEIVER->HANDLE_USER_COMMAND FOR ALV_GRID.
*   将按钮相关事件分配ALV
    SET HANDLER EVENT_RECEIVER->HANDLE_TOOLBAR FOR ALV_GRID.
*   调用处理工具条处理方法,使自定义工具条生效
    CALL METHOD ALV_GRID->SET_TOOLBAR_INTERACTIVE.
  ENDIF.
ENDMODULE.                 " STATUS_1001  OUTPUT

* PAI
MODULE USER_COMMAND_1001 INPUT.
  SAVE_OK = OK_CODE.
  CLEAR OK_CODE.
  CASE SAVE_OK.
    WHEN 'EXT'.
      LEAVE PROGRAM.
  ENDCASE.
ENDMODULE.                 " USER_COMMAND_1001  INPUT


* 显示选择行内容模版
FORM MESSDETA 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 WA_SPFLI INDEX LF_ROW_INDEX INTO A_SPFLI.
    S1 = '选择行内容:'.
*  合并
    CONCATENATE S1 A_SPFLI-CONNID A_SPFLI-CARRID A_SPFLI-CITYFROM
    INTO S1 SEPARATED BY S2.
* 弹出窗口提示
    MESSAGE I005(YMESS) WITH S1.
  ENDLOOP.
ENDFORM.                    "messdeta

转载于:https://www.cnblogs.com/eric0701/archive/2012/01/06/2314184.html

你可能感兴趣的:(实战ALV OO技术实现添加工具条按钮)