本文主要是介绍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( EXPORTINGcolumnname = '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( EXPORTINGcolumnname = 'VBELN'subtotal = if_salv_c_bool_sap=>true).CATCH cx_salv_not_found . "#EC NO_HANDLERCATCH cx_salv_existing . "#EC NO_HANDLERCATCH cx_salv_data_error . "#EC NO_HANDLERENDTRY.
- 显示效果如下图所示
- 完整代码
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 netwrINTO TABLE mt_dataFROM vbakUP TO 10 ROWS.ENDMETHOD.METHOD alv_show.DATA: lx_msg TYPE REF TO cx_salv_msg.TRY.cl_salv_table=>factory(IMPORTINGr_salv_table = mo_salvCHANGINGt_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( EXPORTINGcolumnname = 'VBELN'subtotal = if_salv_c_bool_sap=>true).CATCH cx_salv_not_found . "#EC NO_HANDLERCATCH cx_salv_existing . "#EC NO_HANDLERCATCH cx_salv_data_error . "#EC NO_HANDLERENDTRY.ENDMETHOD.METHOD set_aggrs.DATA(lo_aggrs) = co_alv->get_aggregations( ).TRY."给NETWR字段添加合计lo_aggrs->add_aggregation( EXPORTINGcolumnname = '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( )
这篇关于ABAP - SALV教程16 合计、小计的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!