在ALV提供的标准工具栏上我们可以实现输出结果的聚合(最大值,最小值,汇总,平均数),排序,过滤功能,同样在程序里也可以达到这些功能。
1. 声明聚合,排序,过滤所需要的变量。
DATA: lr_aggregations TYPE REF TO cl_salv_aggregations,
lr_filters TYPE REF TO cl_salv_filters,
lr_sorts type ref to cl_salv_sorts.
2.汇总
lr_aggregations->add_aggregation(
columnname = 'DISTANCE'
aggregation =if_salv_c_aggregation=>total
).
3. 最小值
lr_aggregations->add_aggregation(
columnname = 'FLTIME'
aggregation = if_salv_c_aggregation=>minimum
).
4. 过滤
lr_filters->add_filter(
columnname = 'CITYFROM'
sign = 'I'
option = 'NE'
low = 'ROM'
high = ''
).
5. 排序
lr_sorts->add_sort(
columnname = 'CITYFROM'
sequence =if_salv_c_sort=>sort_down
subtotal = 'X'
obligatory = 'X'
).
下面是效果图
下面是程序的所有代码:
REPORT y_xin_002.
*变量定义
TYPES: BEGIN OF gs_spfli,
mandt TYPE spfli-mandt,
carrid TYPE spfli-carrid,
connid TYPE spfli-connid,
countryfr TYPE spfli-countryfr,
cityfrom TYPE spfli-cityfrom,
airpfrom TYPE spfli-airpfrom,
countryto TYPE spfli-countryto,
cityto TYPE spfli-cityto,
airpto TYPE spfli-airpto,
fltime TYPE spfli-fltime,
deptime TYPE spfli-deptime,
arrtime TYPE spfli-arrtime,
distance TYPE spfli-distance,
distid TYPE spfli-distid,
fltype TYPE spfli-fltype,
period TYPE spfli-period,
END OF gs_spfli.
TYPES ty_spfli TYPE gs_spfli OCCURS 0.
*----------------------------------------------------------------------*
* CLASS lcl_alv DEFINITION
*----------------------------------------------------------------------*
* ALV操作类(定义)
*----------------------------------------------------------------------*
CLASS lcl_alv DEFINITION.
PUBLIC SECTION.
METHODS: getdata "取得要显示的数据
RETURNING value(lt_tab) TYPEty_spfli,
alv_full "全屏Grid列表处理方法
IMPORTING value(lt_tab) TYPEty_spfli.
PRIVATE SECTION.
DATA: gr_table TYPE REF TO cl_salv_table.
ENDCLASS. "lcl_alv DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_alv IMPLEMENTATION
*----------------------------------------------------------------------*
* ALV操作类(实现)
*----------------------------------------------------------------------*
CLASS lcl_alvIMPLEMENTATION.
*取得要显示的数据
METHOD getdata.
SELECT * INTO CORRESPONDING FIELDS OF TABLElt_tab FROM spfli.
ENDMETHOD. "getdata
METHOD alv_full.
DATA: lr_functions TYPE REF TO cl_salv_functions_list,
lr_aggregations TYPE REF TO cl_salv_aggregations,
lr_filters TYPE REF TO cl_salv_filters,
lr_sorts type ref to cl_salv_sorts.
"创建实例
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = gr_table
CHANGING
t_table = lt_tab
).
CATCH cx_salv_msg.
ENDTRY.
"ALV标准功能
lr_functions = gr_table->get_functions().
lr_functions->set_all( 'X' ).
lr_aggregations =gr_table->get_aggregations( ).
lr_filters = gr_table->get_filters( ).
lr_sorts = gr_table->get_sorts( ).
TRY.
"汇总
lr_aggregations->add_aggregation(
columnname = 'DISTANCE'
aggregation =if_salv_c_aggregation=>total
).
"最小值
lr_aggregations->add_aggregation(
columnname = 'FLTIME'
aggregation =if_salv_c_aggregation=>minimum
).
"过滤
lr_filters->add_filter(
columnname = 'CITYFROM'
sign = 'I'
option = 'NE'
low = 'ROM'
high = ''
).
"排序
lr_sorts->add_sort(
columnname = 'CITYFROM'
sequence =if_salv_c_sort=>sort_down
subtotal = 'X'
obligatory = 'X'
).
CATCH cx_salv_data_error.
CATCH cx_salv_existing.
CATCH cx_salv_not_found.
ENDTRY.
"显示列表
gr_table->display( ).
ENDMETHOD. "alv_full
ENDCLASS. "lcl_alvIMPLEMENTATION
*&---------------------------------------------------------------------*
*& Formf_main
*&---------------------------------------------------------------------*
* 整合数据,执行
*----------------------------------------------------------------------*
FORM f_main.
DATA: lt_tab TYPE ty_spfli,
lr_alv TYPE REF TO lcl_alv.
CREATE OBJECT lr_alv.
"取得要显示的数据
lt_tab = lr_alv->getdata( ).
lr_alv->alv_full( lt_tab ).
ENDFORM. "f_main
*执行动作
START-OF-SELECTION.
PERFORM f_main.
注:ALV TABLE部份已经全部完成。