*&---------------------------------------------------------------------*
*& Report YTEST00A
*&---------------------------------------------------------------------*
*&Violin 2021/3/10
*&---------------------------------------------------------------------*
REPORT ytest00a.
*第8章 表控件ALV控件的使用---------------------------------------------------------------------*
*8.5處理ALV雙擊事件
*功能碼返回值
DATA: OK_CODE TYPE SY-UCOMM,
SAVE_OK TYPE SY-UCOMM.
*定義內表,變量需要傳遞,不加Header line
DATA WA_SPFLI TYPE TABLE OF 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.
*定義事件類型
CLASS LCL_EVENT_RECEIVER DEFINITION DEFERRED.
*定義事件
DATA EVENT_RECEIVER TYPE REF TO LCL_EVENT_RECEIVER.
*直接調用窗口
CALL SCREEN 580.
*定義事件的屬性和方法
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.
ENDCLASS.
*雙擊方法實現
CLASS LCL_EVENT_RECEIVER IMPLEMENTATION.
METHOD HANDLE_DOUBLE_CLICK.
DATA: LI_SPFLI LIKE LINE OF WA_SPFLI.
READ TABLE WA_SPFLI INDEX E_ROW-INDEX INTO LI_SPFLI.
* 將行列等信息合併到字符串
DATA: S1(100) TYPE C.
CONCATENATE '行:' E_ROW-INDEX ' 列名:' E_COLUMN-FIELDNAME INTO S1.
CONCATENATE S1 ' CONNID:' LI_SPFLI-CONNID INTO S1.
CONCATENATE S1 ' CARRID:' LI_SPFLI-CARRID INTO S1.
* 在狀態條顯示單擊的行與列信息
MESSAGE s208(00) WITH S1.
ENDMETHOD.
ENDCLASS.
MODULE STATUS_0100 OUTPUT.
SET PF-STATUS 'TESTSTA1'.
*如果窗口還沒有創建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.
*ALV對象分配雙擊事件
CREATE OBJECT EVENT_RECEIVER.
SET HANDLER EVENT_RECEIVER->HANDLE_DOUBLE_CLICK
FOR ALV_GRID.
ENDIF.
ENDMODULE.
*用戶交互
MODULE USER_COMMAND_0100 INPUT.
SAVE_OK = OK_CODE.
CLEAR OK_CODE.
CASE SAVE_OK.
WHEN 'EXIT'.
LEAVE PROGRAM.
ENDCASE.
ENDMODULE.
![](https://i-blog.csdnimg.cn/blog_migrate/1dde6e308c9f4be3bc1f11adb1c23077.png)