首先,各位ABAPer程序猿们,节日快乐鸭。
进入正题
上一篇文介绍了 OOALV的报表实现,基础中的基础运用
前面介绍使用类CL_GUI_DOCKING_CONTAINER生成容器,使用类CL_GUI_ALV_GRID生成alv输出。如果说这两个类是OOALV的灵魂,那事件就相当于是OOALV的躯干。
下面先贴上相关代码:
TABLES: SFLIGHT.
"输出结构
TYPES: BEGIN OF TY_OUTPUT.
INCLUDE STRUCTURE SFLIGHT.
TYPES: ZBOX TYPE C,
SEATSOCC_O TYPE SFLIGHT-SEATSOCC, "经济舱剩余座位
ZSTYLE TYPE LVC_T_STYL. "编辑状态字段
TYPES: END OF TY_OUTPUT.
DATA: GT_OUTPUT TYPE TABLE OF TY_OUTPUT. "输出内表
DATA: GS_OUTPUT TYPE TY_OUTPUT. "输出工作区
DATA: GS_LAYOUT TYPE LVC_S_LAYO. "布局
DATA: GT_FIELDCAT TYPE LVC_T_FCAT. "字段属性内表
DATA: GS_FIELDCAT TYPE LVC_S_FCAT. "
DATA: GV_COL TYPE I.
"隐藏工具栏图标
DATA: GS_UI_FUNC TYPE UI_FUNC.
DATA: GT_UI_FUNC TYPE UI_FUNCTIONS.
DATA: OK_CODE TYPE SY-UCOMM. "用户功能码接收
DATA: SAVE_OK TYPE SY-UCOMM. "用户功能码接收
DATA: GS_ALV TYPE REF TO CL_GUI_ALV_GRID. "屏幕alv对象
*DATA: GS_CON TYPE REF TO CL_GUI_CUSTOM_CONTAINER.
DATA: GS_DOCK TYPE REF TO CL_GUI_DOCKING_CONTAINER. "屏幕容器对象
"事件接收类定义
CLASS LCL_EVENTS_RECEIVER DEFINITION.
PUBLIC SECTION.
"工具栏事件
METHODS TOOLBAR FOR EVENT TOOLBAR OF CL_GUI_ALV_GRID
IMPORTING E_OBJECT.
"处理命令事件
METHODS USER_COMMAND FOR EVENT USER_COMMAND OF CL_GUI_ALV_GRID
IMPORTING E_UCOMM.
"数据改变事件
METHODS DATA_CHANGED FOR EVENT DATA_CHANGED OF CL_GUI_ALV_GRID
IMPORTING ER_DATA_CHANGED.
"单击事件
METHODS HOTSPOT_CLICK FOR EVENT HOTSPOT_CLICK OF CL_GUI_ALV_GRID
IMPORTING E_ROW_ID
E_COLUMN_ID.
"双击事件
METHODS DOUBLE_CLICK FOR EVENT DOUBLE_CLICK OF CL_GUI_ALV_GRID
IMPORTING E_ROW
E_COLUMN.
ENDCLASS.
"事件接收类实现
CLASS LCL_EVENTS_RECEIVER IMPLEMENTATION.
"工具栏事件实现
METHOD TOOLBAR.
PERFORM FRM_TOOLBAR USING E_OBJECT.
ENDMETHOD.
"处理命令事件实现
METHOD USER_COMMAND.
PERFORM FRM_USER_COMMAND USING E_UCOMM.
ENDMETHOD.
"数据改变事件实现
METHOD DATA_CHANGED.
PERFORM FRM_DATA_CHANGED USING ER_DATA_CHANGED.
ENDMETHOD.
"单击事件实现
METHOD HOTSPOT_CLICK.
PERFORM FRM_HOTSPOT_CLICK USING E_ROW_ID E_COLUMN_ID.
ENDMETHOD.
"双击事件
METHOD DOUBLE_CLICK.
PERFORM FRM_DOUBLE_CLICK USING E_ROW E_COLUMN.
ENDMETHOD.
ENDCLASS.
"事件接收对象
DATA: GS_EVENTS_RECEIVER TYPE REF TO LCL_EVENTS_RECEIVER.
"选择屏幕
SELECTION-SCREEN BEGIN OF BLOCK BLK1 WITH FRAME.
SELECT-OPTIONS: S_CARRID FOR SFLIGHT-CARRID.
SELECTION-SCREEN END OF BLOCK BLK1 .
START-OF-SELECTION.
"取数
PERFORM FRM_SELECT_DATA.
END-OF-SELECTION.
"调用100屏幕输出
CALL SCREEN 0100.
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.
SET PF-STATUS 'ZSTATUS_0100'.
SET TITLEBAR 'ZTITLE_0100'.
ENDMODULE.
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
"赋值一份OK_CODE
SAVE_OK = OK_CODE.
CLEAR OK_CODE.
CASE SAVE_OK.
WHEN 'ZEDIT'.
PERFORM FRM_SET_EDIT.
WHEN OTHERS.
ENDCASE.
ENDMODULE.
*&---------------------------------------------------------------------*
*& Module EXIT INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE EXIT_UCOMMAND INPUT.
SAVE_OK = OK_CODE.
CLEAR OK_CODE.
CASE SAVE_OK.
WHEN 'ZBACK' .
LEAVE TO SCREEN 0. "返回上一个屏幕
WHEN 'ZCANCEL'.
LEAVE TO SCREEN 0.
WHEN 'EXIT'.
LEAVE PROGRAM. "返回程序
WHEN OTHERS.
ENDCASE.
ENDMODULE.
*&---------------------------------------------------------------------*
*& Module INIT_ALV OUTPUT
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
MODULE INIT_ALV OUTPUT.
IF GS_ALV IS INITIAL.
"实例化
PERFORM FRM_SET_GRID.
"布局
PERFORM FRM_SET_LAYOUT.
"字段属性
PERFORM FRM_SET_FIELDCAT.
"按钮隐藏
PERFORM FRM_UI_FUNCIONS.
"展示数据
PERFORM FRM_DISPLAY_ALV.
ELSE.
"刷新屏幕
PERFORM FRM_REFRESH_ALV.
ENDIF.
ENDMODULE.
*&---------------------------------------------------------------------*
*& Form FRM_SELECT_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_SELECT_DATA .
DATA: LS_STYL TYPE LVC_S_STYL.
DATA: LT_STYL TYPE LVC_T_STYL.
SELECT *
FROM SFLIGHT
INTO CORRESPONDING FIELDS OF TABLE GT_OUTPUT
WHERE CARRID IN S_CARRID.
"数据处理
LOOP AT GT_OUTPUT ASSIGNING FIELD-SYMBOL(<FW_OUTPUT>).
<FW_OUTPUT>-SEATSOCC_O = <FW_OUTPUT>-SEATSMAX - <FW_OUTPUT>-SEATSOCC.
LS_STYL-FIELDNAME = 'SEATSMAX'.
LS_STYL-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.
APPEND LS_STYL TO <FW_OUTPUT>-ZSTYLE.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SET_ALV
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_SET_GRID .
"实例化屏幕容器
CREATE OBJECT GS_DOCK
EXPORTING
* PARENT =
REPID = SY-REPID "当前程序
DYNNR = '0100' "屏幕编号
SIDE = CL_GUI_DOCKING_CONTAINER=>DOCK_AT_LEFT "容器吸附左侧
EXTENSION = 1300 "ALV的宽度
* STYLE =
* LIFETIME = lifetime_default
* CAPTION =
METRIC = 0
* RATIO = 100 "ALV的比率,优先级高于上面的EXTENSION
* NO_AUTODEF_PROGID_DYNNR =
* NAME =
EXCEPTIONS
CNTL_ERROR = 1
CNTL_SYSTEM_ERROR = 2
CREATE_ERROR = 3
LIFETIME_ERROR = 4
LIFETIME_DYNPRO_DYNPRO_LINK = 5
OTHERS = 6.
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
*
* CREATE OBJECT GS_CON
* EXPORTING
** PARENT =
* CONTAINER_NAME = 'GC_CON'
** STYLE =
** LIFETIME = lifetime_default
** REPID = SY-REPID
** DYNNR = '0100'
** NO_AUTODEF_PROGID_DYNNR =
* EXCEPTIONS
* CNTL_ERROR = 1
* CNTL_SYSTEM_ERROR = 2
* CREATE_ERROR = 3
* LIFETIME_ERROR = 4
* LIFETIME_DYNPRO_DYNPRO_LINK = 5
* OTHERS = 6.
* IF SY-SUBRC <> 0.
** MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
** WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
* ENDIF.
"实例化alv展示对象
CREATE OBJECT GS_ALV
EXPORTING
* I_SHELLSTYLE = 0
* I_LIFETIME =
I_PARENT = GS_DOCK
* I_APPL_EVENTS = space
* I_PARENTDBG =
* I_APPLOGPARENT =
* I_GRAPHICSPARENT =
* I_NAME =
* I_FCAT_COMPLETE = SPACE
EXCEPTIONS
ERROR_CNTL_CREATE = 1
ERROR_CNTL_INIT = 2
ERROR_CNTL_LINK = 3
ERROR_DP_CREATE = 4
OTHERS = 5.
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
"注入数据变化方法
CALL METHOD GS_ALV->REGISTER_EDIT_EVENT
EXPORTING
I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_MODIFIED
* I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_ENTER
EXCEPTIONS
ERROR = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.
"实例化接收类
CREATE OBJECT GS_EVENTS_RECEIVER.
"为alv注入事件
SET HANDLER GS_EVENTS_RECEIVER->TOOLBAR FOR GS_ALV.
SET HANDLER GS_EVENTS_RECEIVER->USER_COMMAND FOR GS_ALV.
SET HANDLER GS_EVENTS_RECEIVER->DATA_CHANGED FOR GS_ALV.
SET HANDLER GS_EVENTS_RECEIVER->HOTSPOT_CLICK FOR GS_ALV.
SET HANDLER GS_EVENTS_RECEIVER->DOUBLE_CLICK FOR GS_ALV.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SET_LAYOUT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_SET_LAYOUT .
CLEAR: GS_LAYOUT.
GS_LAYOUT-CWIDTH_OPT = 'X'. "自动列宽
GS_LAYOUT-ZEBRA = 'X'. "斑马纹
GS_LAYOUT-BOX_FNAME = 'ZBOX'. "选择块
GS_LAYOUT-SEL_MODE = 'A'. "显示选择块
* GS_LAYOUT-EDIT = 'X'.
GS_LAYOUT-DETAILINIT = 'X'.
GS_LAYOUT-DETAILTITL = '详细信息'.
GS_LAYOUT-STYLEFNAME = 'ZSTYLE'. "用于改变字段属性
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SET_FIELDCAT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_SET_FIELDCAT .
DEFINE M_FIELDCAT.
CLEAR GS_FIELDCAT.
GS_FIELDCAT-FIELDNAME = &1.
GS_FIELDCAT-REF_TABLE = &2.
GS_FIELDCAT-REF_FIELD = &3.
GS_FIELDCAT-SCRTEXT_L = &4.
GV_COL = GV_COL + 1.
GS_FIELDCAT-COL_POS = GV_COL.
IF &1 EQ 'SEATSMAX'.
GS_FIELDCAT-EDIT = 'X'.
ENDIF.
IF &1 EQ 'CARRID'.
GS_FIELDCAT-HOTSPOT = 'X'.
ENDIF.
APPEND GS_FIELDCAT TO GT_FIELDCAT.
END-OF-DEFINITION.
REFRESH GT_FIELDCAT.
CLEAR GV_COL.
M_FIELDCAT 'CARRID' 'SFLIGHT' 'CARRID' '航线'.
M_FIELDCAT 'CONNID' 'SFLIGHT' 'CONNID' '航班'.
M_FIELDCAT 'FLDATE' 'SFLIGHT' 'FLDATE' '航班日期'.
M_FIELDCAT 'PRICE' 'SFLIGHT' 'PRICE' '航空运费'.
M_FIELDCAT 'CURRENCY' 'SFLIGHT' 'CURRENCY' '价格'.
M_FIELDCAT 'PLANETYPE' 'SFLIGHT' 'PLANETYPE' '飞机类型'.
M_FIELDCAT 'SEATSMAX' 'SFLIGHT' 'SEATSMAX' '经济舱位'.
M_FIELDCAT 'SEATSOCC' 'SFLIGHT' 'SEATSOCC' '经济舱占据位'.
M_FIELDCAT 'SEATSOCC_O' 'SFLIGHT' 'SEATSOCC' '经济舱剩余位'.
M_FIELDCAT 'PAYMENTSUM' 'SFLIGHT' 'PAYMENTSUM' '当前预定总数'.
M_FIELDCAT 'SEATSMAX_B' 'SFLIGHT' 'SEATSMAX_B' '商务舱位'.
M_FIELDCAT 'SEATSOCC_B' 'SFLIGHT' 'SEATSOCC_B' '商务舱占据位'.
M_FIELDCAT 'SEATSMAX_F' 'SFLIGHT' 'SEATSMAX_F' '头等舱位'.
M_FIELDCAT 'SEATSOCC_F' 'SFLIGHT' 'SEATSOCC_F' '头等舱占据位'.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DISPLAY_ALV
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_DISPLAY_ALV .
"调用方法生成alv
CALL METHOD GS_ALV->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
* I_BUFFER_ACTIVE =
* I_BYPASSING_BUFFER =
* I_CONSISTENCY_CHECK =
* I_STRUCTURE_NAME =
* IS_VARIANT =
I_SAVE = 'A'
I_DEFAULT = 'X'
IS_LAYOUT = GS_LAYOUT
* IS_PRINT =
* IT_SPECIAL_GROUPS =
IT_TOOLBAR_EXCLUDING = GT_UI_FUNC
* IT_HYPERLINK =
* IT_ALV_GRAPHICS =
* IT_EXCEPT_QINFO =
* IR_SALV_ADAPTER =
CHANGING
IT_OUTTAB = GT_OUTPUT
IT_FIELDCATALOG = GT_FIELDCAT
* IT_SORT =
* IT_FILTER =
EXCEPTIONS
INVALID_PARAMETER_COMBINATION = 1
PROGRAM_ERROR = 2
TOO_MANY_LINES = 3
OTHERS = 4.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_UI_FUNCIONS
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_UI_FUNCIONS .
CLEAR GS_UI_FUNC.
GS_UI_FUNC = CL_GUI_ALV_GRID=>MC_FC_LOC_APPEND_ROW. "附加行
APPEND GS_UI_FUNC TO GT_UI_FUNC.
CLEAR GS_UI_FUNC.
GS_UI_FUNC = CL_GUI_ALV_GRID=>MC_FC_LOC_INSERT_ROW."插入行
APPEND GS_UI_FUNC TO GT_UI_FUNC.
CLEAR GS_UI_FUNC.
GS_UI_FUNC = CL_GUI_ALV_GRID=>MC_FC_LOC_DELETE_ROW."删除行
APPEND GS_UI_FUNC TO GT_UI_FUNC.
CLEAR GS_UI_FUNC.
GS_UI_FUNC = CL_GUI_ALV_GRID=>MC_FC_LOC_COPY_ROW."复制行
APPEND GS_UI_FUNC TO GT_UI_FUNC.
CLEAR GS_UI_FUNC.
GS_UI_FUNC = CL_GUI_ALV_GRID=>MC_FC_INFO. "文本信息
APPEND GS_UI_FUNC TO GT_UI_FUNC.
*
*CLEAR GS_UI_FUNC.
*GS_UI_FUNC = CL_GUI_ALV_GRID=>MC_FC_LOC_APPEND_ROW.
*APPEND GS_UI_FUNC TO GT_UI_FUNC.
*
*CLEAR GS_UI_FUNC.
*GS_UI_FUNC = CL_GUI_ALV_GRID=>MC_FC_LOC_APPEND_ROW.
*APPEND GS_UI_FUNC TO GT_UI_FUNC.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_TOOLBAR
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_TOOLBAR USING P_OBJECT TYPE REF TO CL_ALV_EVENT_TOOLBAR_SET.
DATA: LS_TOOLBAR TYPE STB_BUTTON.
"插入分隔符
CLEAR LS_TOOLBAR.
LS_TOOLBAR-BUTN_TYPE = 3.
APPEND LS_TOOLBAR TO P_OBJECT->MT_TOOLBAR.
CLEAR LS_TOOLBAR.
LS_TOOLBAR-FUNCTION = 'EDIT'. "function code
* LS_TOOLBAR-ICON = ICON_EXPORT. "按钮图标
LS_TOOLBAR-QUICKINFO = '可编辑'. "按钮信息 鼠标停在按钮上显示的信息
LS_TOOLBAR-BUTN_TYPE = 0. "按钮类型
LS_TOOLBAR-DISABLED = ''. "按钮是否可用 空或者X
LS_TOOLBAR-TEXT = '可编辑'. "按钮名称
* LS_TOOLBAR-CHECKED = ''.
APPEND LS_TOOLBAR TO P_OBJECT->MT_TOOLBAR.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_USER_COMMAND
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> E_UCOMM
*&---------------------------------------------------------------------*
FORM FRM_USER_COMMAND USING P_UCOMM.
CASE P_UCOMM.
WHEN 'EDIT'.
"设置按钮功能
PERFORM FRM_SET_EDIT.
WHEN OTHERS.
ENDCASE.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SET_EDIT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_SET_EDIT .
DATA: LT_ROWS TYPE LVC_T_ROW.
DATA: LS_STYL TYPE LVC_S_STYL.
DATA: LT_STYL TYPE LVC_T_STYL.
"取出选中行的行相关信息
CALL METHOD GS_ALV->GET_SELECTED_ROWS
IMPORTING
ET_INDEX_ROWS = LT_ROWS
* ET_ROW_NO =
.
LOOP AT LT_ROWS ASSIGNING FIELD-SYMBOL(<LW_ROWS>).
READ TABLE GT_OUTPUT INTO GS_OUTPUT INDEX <LW_ROWS>-INDEX.
IF SY-SUBRC EQ 0.
LS_STYL-FIELDNAME = 'SEATSMAX'.
"将该行的SEATSMAX字段变为可编辑
LS_STYL-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_ENABLED.
APPEND LS_STYL TO LT_STYL.
"将属性表赋值到工作区字段中
GS_OUTPUT-ZSTYLE = LT_STYL.
"更新内表 注意:因为用的是read,所以这里更新内表时要根据行号去更新
MODIFY GT_OUTPUT FROM GS_OUTPUT INDEX <LW_ROWS>-INDEX.
ENDIF.
ENDLOOP.
"最后,刷新alv,显示效果
PERFORM FRM_REFRESH_ALV.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_REFRESH_ALV
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_REFRESH_ALV .
DATA: LS_STABLE TYPE LVC_S_STBL.
LS_STABLE-COL = 'X'.
LS_STABLE-ROW = 'X'.
CALL METHOD GS_ALV->REFRESH_TABLE_DISPLAY
EXPORTING
IS_STABLE = LS_STABLE
* I_SOFT_REFRESH =
EXCEPTIONS
FINISHED = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DATA_CHANGED
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_DATA_CHANGED USING PR_DATA_CHANGED TYPE REF TO CL_ALV_CHANGED_DATA_PROTOCOL.
DATA: LS_MODI TYPE LVC_S_MODI.
LOOP AT PR_DATA_CHANGED->MT_MOD_CELLS INTO LS_MODI.
IF LS_MODI-FIELDNAME EQ 'SEATSMAX'.
READ TABLE GT_OUTPUT INTO GS_OUTPUT INDEX LS_MODI-ROW_ID.
IF SY-SUBRC EQ 0.
GS_OUTPUT-SEATSMAX = LS_MODI-VALUE.
GS_OUTPUT-SEATSOCC_O = GS_OUTPUT-SEATSMAX - GS_OUTPUT-SEATSOCC.
MODIFY GT_OUTPUT FROM GS_OUTPUT INDEX LS_MODI-ROW_ID.
ENDIF.
ENDIF.
ENDLOOP.
PERFORM FRM_REFRESH_ALV.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_HOTSPOT_CLICK
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_HOTSPOT_CLICK USING P_ROW_ID TYPE LVC_S_ROW
P_COLUMN_ID TYPE LVC_S_COL.
DATA: LV_STR TYPE STRING.
READ TABLE GT_OUTPUT INTO GS_OUTPUT INDEX P_ROW_ID-INDEX.
IF SY-SUBRC EQ 0.
IF P_COLUMN_ID-FIELDNAME EQ 'CARRID'.
CONCATENATE '您点击了' GS_OUTPUT-CARRID '航线' GS_OUTPUT-CONNID '航班' INTO LV_STR SEPARATED BY SPACE.
MESSAGE LV_STR TYPE 'S' ."DISPLAY LIKE 'E'.
ENDIF.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DOUBLE_CLICK
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> E_ROW
*& --> E_COLUMN
*&---------------------------------------------------------------------*
FORM FRM_DOUBLE_CLICK USING P_ROW TYPE LVC_S_ROW
P_COLUMN TYPE LVC_S_COL.
DATA: LV_STR TYPE STRING.
READ TABLE GT_OUTPUT INTO GS_OUTPUT INDEX P_ROW-INDEX.
IF SY-SUBRC EQ 0.
CONCATENATE '您双击了' P_ROW-INDEX '行' P_COLUMN-FIELDNAME '列' INTO LV_STR SEPARATED BY SPACE .
MESSAGE LV_STR TYPE 'S' ."DISPLAY LIKE 'E'.
ENDIF.
ENDFORM.
事件有很多,这里只列举了几个,其中TOOLBAR和USER_COMMAND事件是基础的,另外DATA_CHANGED事件是个人认为比较重要的。
事件的实现步骤大致可以分为以下几步:
1、创建事件接收类(类的定义与实现)
"事件接收类定义
CLASS LCL_EVENTS_RECEIVER DEFINITION.
PUBLIC SECTION.
"工具栏事件
METHODS TOOLBAR FOR EVENT TOOLBAR OF CL_GUI_ALV_GRID
IMPORTING E_OBJECT.
"处理命令事件
METHODS USER_COMMAND FOR EVENT USER_COMMAND OF CL_GUI_ALV_GRID
IMPORTING E_UCOMM.
"数据改变事件
METHODS DATA_CHANGED FOR EVENT DATA_CHANGED OF CL_GUI_ALV_GRID
IMPORTING ER_DATA_CHANGED.
"单击事件
METHODS HOTSPOT_CLICK FOR EVENT HOTSPOT_CLICK OF CL_GUI_ALV_GRID
IMPORTING E_ROW_ID
E_COLUMN_ID.
"双击事件
METHODS DOUBLE_CLICK FOR EVENT DOUBLE_CLICK OF CL_GUI_ALV_GRID
IMPORTING E_ROW
E_COLUMN.
ENDCLASS.
"事件接收类实现
CLASS LCL_EVENTS_RECEIVER IMPLEMENTATION.
"工具栏事件实现
METHOD TOOLBAR.
PERFORM FRM_TOOLBAR USING E_OBJECT.
ENDMETHOD.
"处理命令事件实现
METHOD USER_COMMAND.
PERFORM FRM_USER_COMMAND USING E_UCOMM.
ENDMETHOD.
"数据改变事件实现
METHOD DATA_CHANGED.
PERFORM FRM_DATA_CHANGED USING ER_DATA_CHANGED.
ENDMETHOD.
"单击事件实现
METHOD HOTSPOT_CLICK.
PERFORM FRM_HOTSPOT_CLICK USING E_ROW_ID E_COLUMN_ID.
ENDMETHOD.
"双击事件
METHOD DOUBLE_CLICK.
PERFORM FRM_DOUBLE_CLICK USING E_ROW E_COLUMN.
ENDMETHOD.
ENDCLASS.
2、声明事件接收类的对象
"事件接收对象
DATA: GS_EVENTS_RECEIVER TYPE REF TO LCL_EVENTS_RECEIVER.
3、实例化事件接收类的对象
"实例化接收类
CREATE OBJECT GS_EVENTS_RECEIVER.
4、给ALV注册事件
"为alv注入事件
SET HANDLER GS_EVENTS_RECEIVER->TOOLBAR FOR GS_ALV.
SET HANDLER GS_EVENTS_RECEIVER->USER_COMMAND FOR GS_ALV.
SET HANDLER GS_EVENTS_RECEIVER->DATA_CHANGED FOR GS_ALV.
SET HANDLER GS_EVENTS_RECEIVER->HOTSPOT_CLICK FOR GS_ALV.
SET HANDLER GS_EVENTS_RECEIVER->DOUBLE_CLICK FOR GS_ALV.
这里DATA_CHANGED事件比较特殊,除了上述的步骤,还需要增加一个步骤,需要用一个方法去检测ALV上的数据是否改变,方法如下:
CALL METHOD GS_ALV->REGISTER_EDIT_EVENT
EXPORTING
I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_MODIFIED
* I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_ENTER
EXCEPTIONS
ERROR = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.
这个需要特别注意。
OVER。