有时候用户有这样的一个需求,需要在同一屏幕上同时显示选择框与ALV结果,这样的需求可以通过容器内存传值的方式来实现,效果如下图:
下面直接上代码:
*&---------------------------------------------------------------------*
*& Report ZBW_DEMO_CX
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zbw_demo_cx.
*———————————————————————-*
* Local class for report
*———————————————————————-*
CLASS lcl_report DEFINITION.
PUBLIC SECTION.
"声明数据和方法
DATA: t_data TYPE STANDARD TABLE OF sflight, " 输出数据
r_carrid TYPE RANGE OF sflight-carrid. " 选择选项
METHODS:
get_data, " 获取数据方法
generate_output. " 生成输出方法
ENDCLASS.
DATA: lo_report TYPE REF TO lcl_report.
DATA: w_carrid TYPE sflight-carrid.
*选择屏幕
SELECTION-SCREEN: BEGIN OF BLOCK blk1 WITH FRAME TITLE aaa.
SELECT-OPTIONS: s_carrid FOR w_carrid. " 选择航空公司代码
SELECTION-SCREEN: END OF BLOCK blk1.
*初始化
INITIALIZATION.
"设置选择屏幕标题
aaa = '选择屏幕'.
"创建报告对象
CREATE OBJECT lo_report.
"生成输出
lo_report->generate_output( ).
** Start of Selection
*开始选择
START-OF-SELECTION.
"获取数据
lo_report->r_carrid = s_carrid[].
lo_report->get_data( ).
*———————————————————————-*
* Local Class Implementation
*———————————————————————-*
CLASS lcl_report IMPLEMENTATION.
METHOD get_data.
"从SFLIGHT表中选择数据
SELECT * FROM sflight
INTO TABLE me->t_data
WHERE carrid IN s_carrid.
"检查是否有数据
IF sy-dbcnt IS INITIAL.
MESSAGE s398(00) WITH '没有您要查询的数据'.
ENDIF.
"将数据导出到内存
EXPORT data = me->t_data TO MEMORY ID sy-cprog.
ENDMETHOD.
METHOD generate_output.
"声明变量和对象
DATA: lo_dock TYPE REF TO cl_gui_docking_container,
lo_cont TYPE REF TO cl_gui_container,
lt_fcat TYPE lvc_t_fcat,
gr_grid_d1001 TYPE REF TO cl_gui_alv_grid.
"从内存导入数据
IMPORT data = me->t_data FROM MEMORY ID sy-cprog.
"释放内存
FREE MEMORY ID sy-cprog.
"检查数据是否非空
CHECK me->t_data IS NOT INITIAL.
"检查Docking容器是否为空
CHECK lo_dock IS INITIAL.
"创建Docking容器
CREATE OBJECT lo_dock
EXPORTING
repid = sy-cprog
dynnr = sy-dynnr
ratio = 80
side = cl_gui_docking_container=>dock_at_bottom
name = 'DOCK_CONT'.
"检查创建是否成功
IF sy-subrc <> 0.
MESSAGE 'Error in the Docking control' TYPE 'S'.
ENDIF.
"获取Docking容器对象
lo_cont ?= lo_dock.
"创建ALV网格对象
CREATE OBJECT gr_grid_d1001
EXPORTING
i_parent = lo_cont.
"合并字段目录
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_buffer_active = space
i_structure_name = 'SFLIGHT'
i_client_never_display = 'X'
i_bypassing_buffer = space
CHANGING
ct_fieldcat = lt_fcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
"设置ALV网格的表格和字段目录
CALL METHOD gr_grid_d1001->set_table_for_first_display
EXPORTING
i_buffer_active = space
i_bypassing_buffer = space
CHANGING
it_outtab = me->t_data
it_fieldcatalog = lt_fcat.
ENDMETHOD.
ENDCLASS.
有需要的小伙伴可以尝试一下哦 !!