cl_gui_alv_grid 是 SAP 第二代 ALV 框架,也被称作 ALV Grid。ALV Grid 是基于面向对象的技术实现的,主要的类是 cl_gui_alv_grid。本篇介绍 cl_gui_alv_grid 的基本使用方法。
cl_gui_alv_grid 继承关系
设置屏幕
cl_gui_alv_grid 不能直接在 selection screen 中使用,需要有一个容器 (container) 来存放该控件,所以一般需要创建一个 dialog screen。新建一个 名为 zalvgrid_01 的程序,在程序中创建 screen number 为 100 的 dialog screen
切换到 element list,设置 OK_CODE 字段。
ok_code acts just as a temporary variable that stores the value of sy-ucomm. When user interacts with the screen elements, the function code that you have assigned is filled in the sy-ucomm field which is turn gets reflected in OK_CODE. In your ABAP programs, you should work with the OK_CODE field instead of SY-UCOMM.
点击屏幕的 Layout 按钮,进入 Screen painter,在 Screen painter 中放置一个 Custom Control 控件,将 name 属性设置为 container1:
创建 GUI Status
创建一个 GUI status 用于定义菜单、工具栏,处理与用户的交互。
编写 PBO 和 PAI
PBO 和 PAI 代表有 dialog screen 的控制流。需要在 Flow logic 选项卡中激活相关的 module。在该程序中,PBO 负责数据显示,以及工具栏的显示。
此时运行程序,数据已经能够被显示,但因为未编写 PAI,程序不能作出任何响应,没有退出程序的操作。在 PAI 模块中编写与用户的交互。这在任何一个 dialog screen 程序中基本都是不可少的。
完整代码
*&---------------------------------------------------------------------*
*& Report ZALVGRID_01
*&
*&---------------------------------------------------------------------*
report zalvgrid_01.
data: gr_container type ref to cl_gui_custom_container.
data: gr_alvgrid type ref to cl_gui_alv_grid.
* 使用OK_CODE,在Screen和Program之间交换数据
data: ok_code like sy-ucomm,
save_ok like sy-ucomm.
data: gt_spfli type standard table of spfli.
start-of-selection.
perform get_data.
call screen 100.
*&---------------------------------------------------------------------*
*& Form get_data
*&---------------------------------------------------------------------*
form get_data.
select * into table gt_spfli from spfli.
endform. "get_data
* 初始化container和ALV grid
form init_components.
create object gr_container
exporting
container_name = 'CONTAINER1'.
create object gr_alvgrid
exporting
i_parent = gr_container .
endform. "init_components
* Show data in ALV
form alv_show.
call method gr_alvgrid->set_table_for_first_display
exporting
i_structure_name = 'SPFLI'
changing
it_outtab = gt_spfli[].
endform. "alv_show
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
module status_0100 output.
perform init_components.
perform alv_show.
set pf-status 'Z100'.
endmodule. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
module user_command_0100 input.
save_ok = ok_code.
clear ok_code.
if save_ok = '&EXIT'.
leave program.
endif.
endmodule. " USER_COMMAND_0100 INPUT