ABAP学习(11):ALV显⽰之OOALV使⽤⽰例2、OO ALV
OOALV主要通过CL_GUI_ALV_GRID这个类来控制alv的显⽰。
ALV显⽰需要屏幕容器,容器对应类:
1、cl_gui_custom_container,默认容器alv⾃动占满整个容器;
2、cl_gui_docking_container,docking容器alv宽度可以直接调整;
3、cl_gui_splitter_contianer,splitter容器,可以将屏幕划分区域显⽰多个alv;
2.1、cl_gui_custom_container容器
⽰例1:OO ALV使⽤cl_gui_custom_container容器
TABLES:spfli.
CLASS cl_event_handle DEFINITION."事件处理类定义
PUBLIC SECTION.
"初始化ALV⼯具栏对象事件,如增加按钮并设定属性
METHODS handle_toolbar FOR EVENT toolbar OF cl_gui_alv_grid
IMPORTING
e_object
e_interactive.
"该事件⽤于在下ALV⼯具栏的下拉菜单按钮中增加选项
METHODS handle_menu_button FOR EVENT menu_button OF cl_gui_alv_grid
IMPORTING
e_object
e_ucomm.
"ALV⼯具栏按钮的点击事件
METHODS handle_user_command FOR EVENT user_command OF cl_gui_alv_grid
IMPORTING
e_ucomm.
"ALV表格双击事件
METHODS handle_double_click FOR EVENT double_click OF cl_gui_alv_grid
IMPORTING
e_row "作废
e_column "通过e_column-fieldname 获取点击字段名,点击列
es_row_no. "通过es_row_no-row_id 返回当前⾏号
"字段的字段⽬录HOTSPOT设置为"X",热点单击事件
METHODS handle_hotspot_click FOR EVENT HOTSPOT_CLICK of cl_gui_alv_grid
IMPORTING
E_ROW_ID "作废
E_COLUMN_ID "通过e_column_id-fieldname 获取点击字段名,点击列
ES_ROW_NO. "通过es_row_no-row_id 返回当前⾏号
ENDCLASS.
DATA: gs_toolbar TYPE stb_button.
DATA:t_sflight LIKE TABLE OF sflight WITH HEADER LINE.
CLASS cl_event_handle IMPLEMENTATION."事件处理类实现部分
METHOD handle_toolbar.
gs_toolbar-function = 'B_SUM'."按钮的FunctionCode
gs_toolbar-icon = icon_display."按钮图标,通过Tcode:ICON可以查看
gs_toolbar-text = '总⾏数'."按钮标签
gs_toolbar-butn_type = '0'."定义按钮类型,0为标准按钮,具体取值可参考这⾥
APPEND gs_toolbar TO e_object->mt_toolbar."添加按钮到⼯具栏中
gs_toolbar-function = 'B_LIST'."按钮的FunctionCode
gs_toolbar-quickinfo = '⾃定义下拉菜单按钮'."按钮的冒泡提⽰
gs_toolbar-icon = icon_biw_report_view."按钮图标
gs_toolbar-text = '下拉菜单按钮'."按钮标签
gs_toolbar-butn_type = '1'."定义按钮类型,1为下拉菜单按钮
APPEND gs_toolbar TO e_object->mt_toolbar."添加下拉菜单按钮到⼯具栏中
ENDMETHOD.
METHOD handle_menu_button.
IF e_ucomm = 'B_LIST'."给下拉菜单按钮增加选项,可以多次调⽤该⽅法以增加多⾏
CALL METHOD e_object->add_function
EXPORTING
icon = icon_display
fcode = 'B_SUM'"字菜单按钮的FunCode
text = '显⽰ALV总⾏数'.
ENDIF.
ENDMETHOD.
METHOD handle_user_command.
DATA: sum TYPE i .
IF e_ucomm = 'B_SUM'.
DESCRIBE TABLE t_sflight[] LINES sum.
MESSAGE i001(00) WITH'当前ALV表格中的数据总⾏数为:' sum.
ENDIF.
ENDMETHOD.
"双击事件
METHOD handle_double_click.
DATA:temp_message TYPE String.
READ TABLE t_sflight INTO t_sflight INDEX es_row_no-row_id.
temp_message = '点击列:' && e_column-fieldname && ',⾏号:' && es_row_no-row_id. MESSAGE i001(00) WITH temp_message.
ENDMETHOD.
"字段的字段⽬录HOTSPOT设置为"X",热点单击事件
METHOD handle_hotspot_click.
DATA:temp_message TYPE String.
temp_message = '点击列:' && e_column_id-fieldname && ',⾏号:' && es_row_no-row_id. MESSAGE i001(00) WITH temp_message.
ENDMETHOD.
ENDCLASS.
"定义cl_gui_alv_grid 类引⽤
DATA:alv_grid TYPE REF TO cl_gui_alv_grid.
"定义cl_gui_custom_container 容器
DATA:alv_container TYPE REF TO cl_gui_custom_container.
"alv显⽰参数
"字段⽬录HOTSPOT,热点单击
DATA:fieldcat TYPE lvc_t_fcat.
DATA:s_fieldcat TYPE lvc_s_fcat.
DATA:layout TYPE lvc_s_layo.
"事件
DATA: event_handle TYPE REF TO cl_event_handle."定义类对象的引⽤
SELECTION-SCREEN BEGIN OF SCREEN101 as SUBSCREEN.
SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE title.
SELECT-OPTIONS:
s_carrid FOR spfli-carrid,
s_connid FOR spfli-connid.
SELECTION-SCREEN END OF BLOCK blk1.
SELECTION-SCREEN END OF SCREEN101.
*初始化
INITIALIZATION.
title = '查询条件'.
START-OF-SELECTION.
CALL SCREEN100.
MODULE STATUS_0100 OUTPUT.
SET PF-STATUS'STA_0100'.
* SET TITLEBAR 'xxx'.
ENDMODULE. " STATUS_0100 OUTPUT
MODULE USER_COMMAND_0100 INPUT.
CASE sy-ucomm.
WHEN 'EXEC'.
"查询数据
SELECT * FROM sflight INTO CORRESPONDING FIELDS OF TABLE t_sflight
WHERE carrid IN s_carrid AND connid IN s_connid.
IF alv_container IS INITIAL.
CREATE OBJECT alv_container"创建ALV容器对象
EXPORTING container_name = 'CONTAINER1'.
CREATE OBJECT alv_grid"创建ALV控件
EXPORTING i_parent = alv_container.
CALL METHOD alv_grid->set_table_for_first_display
EXPORTING
i_structure_name = 'sflight'
is_layout = layout
i_save = 'X'"可以保存变式
CHANGING
it_outtab = t_sflight[]
it_fieldcatalog = fieldcat[]."如果fieldcat内表为空,则相当于没有配置,采⽤默认⽅式显⽰CREATE OBJECT :event_handle.
"为ALV按钮注册监听事件
SET HANDLER :event_handle->handle_toolbar FOR alv_grid,
event_handle->handle_menu_button FOR alv_grid,
event_handle->handle_user_command FOR alv_grid,
event_handle->handle_double_click FOR alv_grid,
event_handle->handle_hotspot_click FOR alv_grid.
"调⽤此⽅法才能激活⼯具栏上增加的⾃定义按钮
CALL METHOD alv_grid->set_toolbar_interactive.
ELSE.
CALL METHOD alv_grid->refresh_table_display.
ENDIF.
WHEN 'BACK'or'EXIT'or'QUIT'.
LEAVE TO SCREEN0.
ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT
创建屏幕100逻辑代码:
PROCESS BEFORE OUTPUT.
"屏幕输出前操作
MODULE STATUS_0100.
"调⽤⼦屏幕
CALL SUBSCREEN sub1 INCLUDING sy-repid '101'.
*
PROCESS AFTER INPUT.
"先调⽤⼦屏幕,后⾯module使⽤到⼦屏幕数据
CALL SUBSCREEN sub1.
"屏幕输⼊后操作
MODULE USER_COMMAND_0100.
2.2、cl_gui_docking_container容器
⽰例2:使⽤cl_gui_docking_container容器:
TABLES:sflight.
"alv显⽰设置
DATA:fieldcat TYPE lvc_t_fcat.
DATA:layout TYPE lvc_s_layo.
"cl_gui_alv_grid类引⽤
DATA:alv_grid TYPE REF TO cl_gui_alv_grid.
DATA:alv_container TYPE REF TO cl_gui_docking_container.
DATA:t_sflight LIKE TABLE OF sflight WITH HEADER LINE.
START-OF-SELECTION.
CALL SCREEN100.
MODULE USER_COMMAND_0100 INPUT.
CASE sy-ucomm.
WHEN 'BACK'or'QUIT'or'EXIT'.
LEAVE TO SCREEN0.
ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT
MODULE STATUS_0100 OUTPUT.
SET PF-STATUS'STA_0100'.
* SET TITLEBAR 'xxx'.
ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Module OUT_SCREEN OUTPUT
*&---------------------------------------------------------------------*
* 查询数据,⽣成docking container对象,显⽰alv
*----------------------------------------------------------------------*
MODULE OUT_SCREEN OUTPUT.
"查询数据
SELECT * FROM sflight INTO CORRESPONDING FIELDS OF TABLE t_sflight WHERE carrid = 'AA'. "创建container对象
CREATE OBJECT alv_container
EXPORTING
repid = sy-repid
dynnr = sy-dynnrcontainer容器用法
extension = 300."alv宽度
"创建alv对象
CREATE OBJECT alv_grid
EXPORTING
i_parent = alv_container.
"调⽤显⽰⽅法
CALL METHOD alv_grid->set_table_for_first_display
i_structure_name = 'sflight'
is_layout = layout
i_save = 'X'"可以保存变式
CHANGING
it_outtab = t_sflight[]
it_fieldcatalog = fieldcat[]."如果fieldcat内表为空,则相当于没有配置,采⽤默认⽅式显⽰ENDMODULE. " OUT_SCREEN OUTPUT
2.3、cl_gui_splitter_container容器
⽰例3:使⽤cl_gui_splitter_container容器
TABLES:sflight.
"alv显⽰设置
DATA:fieldcat TYPE lvc_t_fcat.
DATA:layout TYPE lvc_s_layo.
"cl_gui_alv_grid类引⽤
DATA:alv_grid TYPE REF TO cl_gui_alv_grid.
DATA:alv_grid1 TYPE REF TO cl_gui_alv_grid.
DATA:alv_container TYPE REF TO cl_gui_docking_container.
"spillter container 对象引⽤
DATA:alv_splitter_container TYPE REF TO cl_gui_splitter_container.
"屏幕容器
DATA:ref_container TYPE REF TO cl_gui_container.
DATA:t_sflight LIKE TABLE OF sflight WITH HEADER LINE.
START-OF-SELECTION.
CALL SCREEN100.
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
CASE sy-ucomm.
WHEN 'BACK'or'QUIT'or'EXIT'.
LEAVE TO SCREEN0.
ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.
SET PF-STATUS'STA_0100'.
* SET TITLEBAR 'xxx'.
ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Module OUT_SCREEN OUTPUT
*&---------------------------------------------------------------------*
* 查询数据,⽣成docking container对象,显⽰alv
*----------------------------------------------------------------------*
MODULE OUT_SCREEN OUTPUT.
"查询数据
SELECT * FROM sflight INTO CORRESPONDING FIELDS OF TABLE t_sflight WHERE carrid = 'AA'. "创建container对象
CREATE OBJECT alv_container
EXPORTING
repid = sy-repid
dynnr = sy-dynnr
extension = 1200."alv宽度
"创建spillter container对象
CREATE OBJECT alv_splitter_container
EXPORTING
parent = alv_container
rows = 1
columns = 2. "将⽗容器分为1⾏两列,两个容器
"调⽤cl_splitter_container对象⽅法,获取容器
CALL METHOD alv_splitter_container->get_container
EXPORTING
row = 1
column = 1
container = ref_container.
"创建左边容器alv对象
"创建alv对象
CREATE OBJECT alv_grid
EXPORTING
i_parent = ref_container.
"调⽤显⽰⽅法
CALL METHOD alv_grid->set_table_for_first_display
EXPORTING
i_structure_name = 'sflight'
is_layout = layout
i_save = 'X'"可以保存变式
CHANGING
it_outtab = t_sflight[]
it_fieldcatalog = fieldcat[]."如果fieldcat内表为空,则相当于没有配置,采⽤默认⽅式显⽰"调⽤cl_splitter_container对象⽅法,获取容器
CALL METHOD alv_splitter_container->get_container
EXPORTING
row = 1
column = 2
RECEIVING
container = ref_container.
"创建右边容器alv对象
"创建alv对象
CREATE OBJECT alv_grid1
EXPORTING
i_parent = ref_container.
"调⽤显⽰⽅法
CALL METHOD alv_grid1->set_table_for_first_display
EXPORTING
i_structure_name = 'sflight'
is_layout = layout
i_save = 'X'"可以保存变式
CHANGING
it_outtab = t_sflight[]
it_fieldcatalog = fieldcat[]."如果fieldcat内表为空,则相当于没有配置,采⽤默认⽅式显⽰ENDMODULE. " OUT_SCREEN OUTPUT
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论