程序代码
REPORT z437_test_2025.
DATA gt_spfli TYPE STANDARD TABLE OF spfli. " 内表
DATA go_alv TYPE REF TO cl_gui_alv_grid. " 创建和管理ALV表格
DATA gs_layout TYPE lvc_s_layo. " 存储ALV表格的布局信息
DATA gs_sort TYPE lvc_s_sort.
DATA gt_sort TYPE lvc_t_sort. " 内表
*----------------------------------------------------------------------*
* DESC: INITIALIZATION 事件
*----------------------------------------------------------------------*
INITIALIZATION.
sy-title = 'OO ALV 基础显示'. " 设置报表标题
*----------------------------------------------------------------------*
* DESC: AT SELECTION-SCREEN OUTPUT 事件
*----------------------------------------------------------------------*
START-OF-SELECTION.
SELECT * FROM spfli
INTO TABLE gt_spfli
UP TO 30 ROWS.
CREATE OBJECT go_alv
EXPORTING
i_parent = cl_gui_container=>default_screen. " 默认屏幕
CLEAR gs_layout.
gs_layout-zebra = 'X'. " 斑马线
gs_layout-cwidth_opt = 'X'. " 自适应列宽
" 添加排序 <---
CLEAR gs_sort.
gs_sort-spos = '1'.
gs_sort-fieldname = 'CARRID'. " 指定排序字段
gs_sort-up = ''.
gs_sort-down = 'X'. " 指定按降序排序
APPEND gs_sort TO gt_sort.
" 将内表gt_spfli中的数据显示在ALV表格中
CALL METHOD go_alv->set_table_for_first_display
EXPORTING
is_layout = gs_layout
i_structure_name = 'SPFLI'
CHANGING
it_outtab = gt_spfli " 内表
it_sort = gt_sort. " 内表 <---
cl_abap_list_layout=>suppress_toolbar( ). " 隐藏标准的工具栏
WRITE space.
效果呈现: