ABAP - SALV教程16 合计、小计
虽然ALV的标准状态栏功能就能实现合计、小计、平均值、最大值等这些功能,但用户更希望一进去ALV就希望ALV已经对数量,金额的字段进行合计,小计。 SALV实现合计,调用CL_SALV_AGGREGATIONS的ADD_AGGREGATION即可 DATA(lo_aggrs) = co_alv->get_aggregations( ).
TRY.
"给NETWR字段添加合计
lo_aggrs->add_aggregation( EXPORTING
columnname = 'NETWR'
aggregation = if_salv_c_aggregation=>total
).
CATCH cx_salv_data_error .
CATCH cx_salv_not_found .
CATCH cx_salv_existing .
ENDTRY.
"将合计放置到NETWR字段的顶端
"lo_aggrs->set_aggregation_before_items(NETWR ).
"将合计跟随在NETWR字段的底部
lo_aggrs->set_aggregation_allowed( 'NETWR' ).
SALV在合计前提下排序时会自动小计,实调用类CL_SALV_SORTS的GET_SORT方法即可 DATA(lo_sorts) = co_alv->get_sorts( ).
TRY.
"对销售订单号进行排序,由于有总计,所以每个销售订单也会有小计
lo_sorts->add_sort( EXPORTING
columnname = 'VBELN'
subtotal = if_salv_c_bool_sap=>true
).
CATCH cx_salv_not_found . "#EC NO_HANDLER
CATCH cx_salv_existing . "#EC NO_HANDLER
CATCH cx_salv_data_error . "#EC NO_HANDLER
ENDTRY.
显示效果如下图所示
完整代码 CLASS lcl_model DEFINITION.
PUBLIC SECTION.
TYPES:BEGIN OF TY_dATA,
vbeln TYPE vbak-vbeln,
erdat TYPE erdat,
erzet TYPE erzet,
ernam TYPE ernam,
auart TYPE auart,
netwr TYPE netwr.
TYPES:END OF ty_data.
DATA mt_data TYPE TABLE OF ty_data.
DATA mo_salv TYPE REF TO cl_salv_table.
"获取数据的方法
METHODS get_data.
"展示SALV的方法
METHODS alv_show.
PRIVATE SECTION.
METHODS:
set_stats CHANGING co_alv TYPE REF TO cl_salv_table.
METHODS:
set_sorts CHANGING co_alv TYPE REF TO cl_salv_table.
METHODS:
set_aggrs CHANGING co_alv TYPE REF TO cl_salv_table.
ENDCLASS.
CLASS lcl_model IMPLEMENTATION.
METHOD get_data.
SELECT vbeln erdat erzet ernam auart netwr
INTO TABLE mt_data
FROM vbak
UP TO 10 ROWS.
ENDMETHOD.
METHOD alv_show.
DATA: lx_msg TYPE REF TO cx_salv_msg.
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = mo_salv
CHANGING
t_table = mt_data ).
CATCH cx_salv_msg INTO lx_msg.
ENDTRY.
me->set_stats( CHANGING co_alv = mo_salv ).
me->set_aggrs( CHANGING co_alv = mo_salv ).
me->set_sorts( CHANGING co_alv = mo_salv ).
mo_salv->display( ).
ENDMETHOD.
METHOD set_stats.
DATA(lo_functions) = co_alv->get_functions( ).
"cl_salv_functions_list类型的实例对象的添加状态方法
lo_functions->set_all( abap_true ).
"lo_functions->set_default( abap_true ).
ENDMETHOD.
METHOD set_sorts.
DATA(lo_sorts) = co_alv->get_sorts( ).
TRY.
"对销售订单号进行排序,由于有总计,所以每个销售订单也会有小计
lo_sorts->add_sort( EXPORTING
columnname = 'VBELN'
subtotal = if_salv_c_bool_sap=>true
).
CATCH cx_salv_not_found . "#EC NO_HANDLER
CATCH cx_salv_existing . "#EC NO_HANDLER
CATCH cx_salv_data_error . "#EC NO_HANDLER
ENDTRY.
ENDMETHOD.
METHOD set_aggrs.
DATA(lo_aggrs) = co_alv->get_aggregations( ).
TRY.
"给NETWR字段添加合计
lo_aggrs->add_aggregation( EXPORTING
columnname = 'NETWR'
aggregation = if_salv_c_aggregation=>total
).
CATCH cx_salv_data_error .
CATCH cx_salv_not_found .
CATCH cx_salv_existing .
ENDTRY.
"将合计放置到NETWR字段的顶端
"lo_aggrs->set_aggregation_before_items(NETWR ).
"将合计跟随在NETWR字段的底部
lo_aggrs->set_aggregation_allowed( 'NETWR' ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA(lo_model) = NEW lcl_model( ).
lo_model->get_data( ).
lo_model->alv_show( )
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1486835.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!