效果图:

设置抬头,列颜色,排序,汇总,

ALV <wbr>demo:cl_salv_table

设置筛选

ALV <wbr>demo:cl_salv_table

code:
*&---------------------------------------------------------------------*
*& Report ZLM_ALV_001
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT zlm_alv_001 .

*ALV Object Model –
DATA : gt_spfli TYPE TABLE OF spfli .
DATA : gr_table TYPE REF TO cl_salv_table .

"display setting
DATA : gr_display TYPE REF TO cl_salv_display_settings .

"tool bar
DATA : gr_functions TYPE REF TO cl_salv_functions .

"Columns – CL_SALV_COLUMNS_TABLE and CL_SALV_COLUMN_TABLE
DATA : gr_columns TYPE REF TO cl_salv_columns_table .
DATA : gr_column TYPE REF TO cl_salv_column_table .
DATA : color TYPE lvc_s_colo .

*Sorts – CL_SALV_SORTS
DATA : gr_sorts TYPE REF TO cl_salv_sorts .

*Aggregations – CL_SALV_AGGREGATIONS
DATA : gr_agg TYPE REF TO cl_salv_aggregations .

*Filters – CL_SALV_FILTERS
DATA : gr_filter TYPE REF TO cl_salv_filters .

*Layouts – CL_SALV_LAYOUT
DATA : gr_layout TYPE REF TO cl_salv_layout .
DATA : key TYPE salv_s_layout_key .


START-OF-SELECTION .

SELECT * INTO TABLE gt_spfli FROM spfli .

cl_salv_table =>factory ( IMPORTING r_salv_table = gr_table CHANGING t_table = gt_spfli ) .

*tool bar
gr_functions = gr_table ->get_functions ( ) .
gr_functions ->set_all ( abap_true ) .

*display setting ~title
gr_display = gr_table ->get_display_settings ( ) .
gr_display ->set_striped_pattern ( cl_salv_display_settings =>true ) .
gr_display ->set_list_header ( 'This is the Title' ) .

"Columns – CL_SALV_COLUMNS_TABLE and CL_SALV_COLUMN_TABLE
gr_columns = gr_table ->get_columns ( ) .
gr_column ?= gr_columns ->get_column ( 'CITYTO' ) .
gr_column ->set_long_text ( 'This is long text' ) .
gr_column ->set_medium_text ( 'This is med text' ) .
gr_column ->set_short_text ( 'This is sh' ) .
gr_column ?= gr_columns ->get_column ( 'CITYFROM' ) .
color -col = '6' .
color -int = '1' .
color -inv = '0' .
gr_column ->set_color ( color ) .

*Sorts – CL_SALV_SORTS
gr_sorts = gr_table ->get_sorts ( ) .

* "Aggregations – CL_SALV_AGGREGATIONS
gr_sorts ->add_sort ( columnname = 'CITYTO' subtotal = abap_true ) .
gr_agg = gr_table ->get_aggregations ( ) .

gr_agg ->add_aggregation ( 'DISTANCE' ) .

* Filters – CL_SALV_FILTERS
gr_filter = gr_table ->get_filters ( ) .
gr_filter ->add_filter ( columnname = 'CARRID' low = 'LH' ) .


*Layouts – CL_SALV_LAYOUT

gr_layout = gr_table ->get_layout ( ) .
key - report = sy -repid .
gr_layout ->set_key ( key ) .
gr_layout ->set_save_restriction ( cl_salv_layout =>restrict_none ) .

gr_table ->display ( ) .