In the series of the SALV Model Table display in SAP ABAP, today we will see how to apply Sorting to the ALV Grid. You can find all the Previous discussion at Tutorials > SALV Table Display .
Sorts are very important in the reporting. Sorting also provides us the functionality to have the Subtotal on amount / quantity fields. To achieve the sort functions, we have the class CL_SALV_SORTS.
We need to use the method ADD_SORTS of the class CL_SALV_SORTS to apply a filter on a specific column. We need to pass the column name e.g. AUART in the parameter COLUMNNAME. We can also set the of sort direction by setting up the parameter SEQUENCE.
For this example, I have added a sort on the AUART column and the subtotal for the NETWR column.
REPORT ztest_oo_alv_sorts.
*
*----------------------------------------------------------------------*
* CLASS lcl_report DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_report DEFINITION.
*
PUBLIC SECTION.
*
* Final output table
TYPES: BEGIN OF ty_vbak,
vbeln TYPE vbak-vbeln,
erdat TYPE erdat,
auart TYPE auart,
kunnr TYPE kunnr,
netwr TYPE netwr,
END OF ty_vbak.
TYPES: ty_t_vbak TYPE STANDARD TABLE OF ty_vbak.
*
DATA: t_vbak TYPE STANDARD TABLE OF ty_vbak.
*
* ALV reference
DATA: o_alv TYPE REF TO cl_salv_table.
*
METHODS:
* data selection
get_data,
*
* Generating output
generate_output.
*
*$*$*.....CODE_ADD_1 - Begin..................................1..*$*$*
*
* In this section we will define the private methods which can
* be implemented to set the properties of the ALV and can be
* called in the
*
PRIVATE SECTION.
METHODS:
set_pf_status
CHANGING
co_alv TYPE REF TO cl_salv_table.
*
METHODS:
set_sorts
CHANGING
co_alv TYPE REF TO cl_salv_table.
*
METHODS:
set_aggregations
CHANGING
co_alv TYPE REF TO cl_salv_table.
*
*$*$*.....CODE_ADD_1 - End....................................1..*$*$*
*
ENDCLASS. "lcl_report DEFINITION
*
*
START-OF-SELECTION.
DATA: lo_report TYPE REF TO lcl_report.
*
CREATE OBJECT lo_report.
*
lo_report->get_data( ).
*
lo_report->generate_output( ).
*
*----------------------------------------------------------------------*
* CLASS lcl_report IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_report IMPLEMENTATION.
*
METHOD get_data.
* data selection
SELECT vbeln erdat auart kunnr netwr
INTO CORRESPONDING FIELDS OF TABLE t_vbak
FROM vbak
UP 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 class
DATA: lx_msg TYPE REF TO cx_salv_msg.
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = o_alv
CHANGING
t_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
*
* Set default PF status
CALL METHOD set_pf_status
CHANGING
co_alv = o_alv.
*
* Set SORT
CALL METHOD set_sorts
CHANGING
co_alv = o_alv.
*
* Set the Aggregations
CALL METHOD set_aggregations
CHANGING
co_alv = o_alv.
*$*$*.....CODE_ADD_2 - End....................................2..*$*$*
*
*
* Displaying the ALV
* Here we will call the DISPLAY method to get the output on the screen
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
*
*
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. "set_pf_status
*
METHOD set_sorts.
*
DATA: lo_sort TYPE REF TO cl_salv_sorts.
*
* get Sort object
lo_sort = co_alv->get_sorts( ).
*
* Set the SORT on the AUART with Subtotal
TRY.
CALL METHOD lo_sort->add_sort
EXPORTING
columnname = 'AUART'
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. "set_sorts
*
METHOD set_aggregations.
*
DATA: lo_aggrs TYPE REF TO cl_salv_aggregations.
*
lo_aggrs = co_alv->get_aggregations( ).
*
* Add TOTAL for COLUMN NETWR
TRY.
CALL METHOD lo_aggrs->add_aggregation
EXPORTING
columnname = 'NETWR'
aggregation = if_salv_c_aggregation=>total.
CATCH cx_salv_data_error . "#EC NO_HANDLER
CATCH cx_salv_not_found . "#EC NO_HANDLER
CATCH cx_salv_existing . "#EC NO_HANDLER
ENDTRY.
*
ENDMETHOD. "set_aggregations
*
*
*
*$*$*.....CODE_ADD_3 - End....................................3..*$*$*
ENDCLASS. "lcl_report IMPLEMENTATION
SAP Help on SALV Column Sorting:
ALV Object Model – Sorting by Columns