SALV Table 13 – Apply Sorts

By | January 4, 2010 | SALV Table, SALV Tutorial, Tutorials | 25,912 | 0

Apply Sorts to SALV Table

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
    TYPESBEGIN OF ty_vbak,
           vbeln     TYPE vbak-vbeln,
           erdat     TYPE erdat,
           auart     TYPE auart,
           kunnr     TYPE kunnr,
           netwr     TYPE netwr,
           END   OF ty_vbak.
    TYPESty_t_vbak TYPE STANDARD TABLE OF ty_vbak.
*
    DATAt_vbak TYPE STANDARD TABLE OF ty_vbak.
*
*   ALV reference
    DATAo_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.
  DATAlo_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
    DATAlx_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.
*
    DATAlo_functions TYPE REF TO cl_salv_functions_list.
*
    lo_functions co_alv->get_functions).
    lo_functions->set_defaultabap_true ).
*
  ENDMETHOD.                    "set_pf_status
*
  METHOD set_sorts.
*
    DATAlo_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.
*
    DATAlo_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

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.