本文主要是介绍十二、SALV 排序(sort)、小计(subtotal),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
排序(sort)、小计(subtotal)
一、主要功能
-
排序在ALV中也是一个比较重要的功能,在有合计的场合下,排序能实现排序字段的小计(subtotal).实现方法如下:
-
通过get_sorts方法得到类CL_SALV_SORTS的引用,
-
通过add_sort方法添加排序的字段,如果还要小计,输入参数subtotal就要传入’X’
-
二、实现与代码
*&---------------------------------------------------------------------*
*& 一个简单的通过类CL_SALV_TABLE生成SALV的例子
*&---------------------------------------------------------------------*
REPORT ZABAP_SALV12.
*
*----------------------------------------------------------------------*
* CLASS lcl_report DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_report DEFINITION.
*PUBLIC SECTION.
*
* 定义SALV输出内表TYPES: BEGIN OF ty_vbak,vbeln TYPE vbak-vbeln,posnr TYPE vbap-posnr,erdat TYPE erdat,auart TYPE auart,kunnr TYPE kunnr,netwr TYPE netwr,END OF ty_vbak.
*DATA: t_vbak TYPE STANDARD TABLE OF ty_vbak.
*
* ALV referenceDATA: o_alv TYPE REF TO cl_salv_table.
*METHODS:
* 抽取数据get_data,
*
* 生成输出内容generate_output.
*
*$*$*.....CODE_ADD_1 - Begin..................................1..*$*$*
*
* 定义private method来设定SALV不同特性PRIVATE SECTION.METHODS:set_pf_status CHANGING co_alv TYPE REF TO cl_salv_table.METHODS:set_aggregations CHANGING co_alv TYPE REF TO cl_salv_table.METHODS:set_sorts CHANGING co_alv TYPE REF TO cl_salv_table.
*$*$*.....CODE_ADD_1 - End....................................1..*$*$*ENDCLASS. "lcl_report DEFINITION*----------------------------------------------------------------------*
* CLASS lcl_report IMPLEMENTATION
*----------------------------------------------------------------------*
* 类实现
CLASS lcl_report IMPLEMENTATION.
*METHOD get_data.
* 抽取数据SELECT vbak~vbelnvbap~posnrvbak~erdatvbak~auartvbak~kunnrvbap~netwrINTO TABLE t_vbakFROM vbakINNER JOIN vbap ON vbap~vbeln = vbak~vbelnUP TO 20 ROWS.
*ENDMETHOD. "get_data*.......................................................................METHOD generate_output.
* New ALV instance
* We are calling the static Factory method which will give back
* the ALV object reference.
*
* exception classDATA: lx_msg TYPE REF TO cx_salv_msg.TRY.cl_salv_table=>factory(IMPORTINGr_salv_table = o_alvCHANGINGt_table = t_vbak ).CATCH cx_salv_msg INTO lx_msg.ENDTRY.
*
*$*$*.....CODE_ADD_2 - Begin..................................2..*$*$*
*
* In this area we will call the methods which will set the
* different properties to the ALV
* 调用status设置方法CALL METHOD set_pf_statusCHANGINGco_alv = o_alv.* 排序CALL METHOD set_sortsCHANGINGco_alv = o_alv.* 调用合计的设置方法CALL METHOD set_aggregationsCHANGINGco_alv = o_alv.*$*$*.....CODE_ADD_2 - End....................................2..*$*$*
*
*
* 调用Display方法将数据输出到屏幕上o_alv->display( ).
*ENDMETHOD. "generate_output
*
*$*$*.....CODE_ADD_3 - Begin..................................3..*$*$*
*
* In this area we will implement the methods which are defined in
* the class definition
* status设置方法实现METHOD set_pf_status.DATA: lo_functions TYPE REF TO cl_salv_functions_list.
* 设置默认的标准状态栏lo_functions = co_alv->get_functions( ).lo_functions->set_default( abap_true ).ENDMETHOD.
* 排序方法实现METHOD set_sorts.
*DATA: lo_sort TYPE REF TO cl_salv_sorts.
*
* get Sort objectlo_sort = co_alv->get_sorts( ).
*
* 对销售订单号进行排序,由于有总计,所以每个销售订单也会有小计TRY.CALL METHOD lo_sort->add_sortEXPORTINGcolumnname = '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. "set_sorts
* 合计方法的实现METHOD set_aggregations.
*DATA: lo_aggrs TYPE REF TO cl_salv_aggregations.
*lo_aggrs = co_alv->get_aggregations( ).
*
* 给NETWR添加列TRY.CALL METHOD lo_aggrs->add_aggregationEXPORTINGcolumnname = 'NETWR'aggregation = if_salv_c_aggregation=>total.CATCH cx_salv_data_error . "#EC NO_HANDLERCATCH cx_salv_not_found . "#EC NO_HANDLERCATCH cx_salv_existing . "#EC NO_HANDLERENDTRY.
*
* 将合计放置到SALV的顶端lo_aggrs->set_aggregation_before_items( ).
*ENDMETHOD. "set_aggregations
*$*$*.....CODE_ADD_3 - End....................................3..*$*$*
*
ENDCLASS. "lcl_report IMPLEMENTATIONSTART-OF-SELECTION.DATA: lo_report TYPE REF TO lcl_report.CREATE OBJECT lo_report.
* 抽取数据lo_report->get_data( ).
* 生成SALVlo_report->generate_output( ).
三、执行结果
这篇关于十二、SALV 排序(sort)、小计(subtotal)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!