SALV Table 12 – Apply Filters

By | December 28, 2009 | SALV Table, SALV Tutorial, Tutorials | 20,440 | 0

Apply Filters to SALV Table

In the series of the SALV Model Table display in SAP ABAP, today we will see how to apply Filters to the ALV Grid. You can find all the Previous discussion at Tutorials > SALV Table Display .

Filters are important to display only values required by the users. i.e. See only the “Red” Lights from the Traffic Light report. To achieve this functionality, we have the CL_SALV_FILTERS class of SAP OM.

We can use the method ADD_FILTERS of the class CL_SALV_FILTERS to apply the filter on a specific column. The values in the filters would work exactly like values in a Range or Select-options i.e. SIGN, OPTION, LOW & HIGH. For this example, I have applied filters on the ERDAT column to only show records with a specific date.

REPORT  ztest_oo_alv_filters.
*
*----------------------------------------------------------------------*
*       Code to Apply Filters to SALV Table Display
*----------------------------------------------------------------------*
*       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_filters
        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
           WHERE erdat GE '20090101'.
*
  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 Filters
    CALL METHOD set_filters
      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_filters.
*
    DATAlo_filters TYPE REF TO cl_salv_filters.
*
    lo_filters co_alv->get_filters).
*
*   Set the filter for the column ERDAT
*     the filter criteria works exactly same as any
*     RANGE or SELECT-OPTIONS works.
    TRY.
        CALL METHOD lo_filters->add_filter
          EXPORTING
            columnname 'ERDAT'
            sign       'I'
            option     'EQ'
            low        '20091214'
*           high       =
            .
      CATCH cx_salv_not_found .                         "#EC NO_HANDLER
      CATCH cx_salv_data_error .                        "#EC NO_HANDLER
      CATCH cx_salv_existing .                          "#EC NO_HANDLER
    ENDTRY.
*
*
  ENDMETHOD.                    "set_filters
*
*
*
*$*$*.....CODE_ADD_3 - End....................................3..*$*$*
ENDCLASS.                    "lcl_report IMPLEMENTATION

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.