SALV Table 16 – Static Filters

By | October 4, 2011 | SALV Table, SALV Tutorial, Tutorials | 2,393 | 1

While writing the last post, SALV Table 15 – Show Count of Displayed Rows, I remembered that I wrote an interesting piece of code to solve the unique requirement – Static Filters on the ALV.

The trick here is to save the Filter after applying it for the first time and check for the filter changes or deletion in the event handler of the AFTER_SALV_FUNCTION. If the filter is not the same as what we have applied initially, will put back the filter again. Thus we are setting the Static filter.

Here is the code snippet which shows how to apply this trick:

*&---------------------------------------------------------------------*
*& Purpose: To show Static Filter
*& Author : Naimesh Patel
*&---------------------------------------------------------------------*

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.
*
  PRIVATE SECTION.
    METHODS:
      set_pf_status,
      set_filters,
      on_after_salv_function FOR EVENT after_salv_function OF cl_salv_events
        IMPORTING e_salv_function.

    DATAt_selopt TYPE salv_t_selopt_ref.

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 100 ROWS
           WHERE erdat GE '20091213'.

*
  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.

*   Set default PF status
    me->set_pf_status).

*   Set Filters
    me->set_filters).

*   set events
    DATAlo_events TYPE REF TO cl_salv_events_table.
    lo_events o_alv->get_event).
    SET HANDLER me->on_after_salv_function FOR lo_events.

* Displaying the ALV
*   Here we will call the DISPLAY method to get the output on the screen
    o_alv->display).
*
  ENDMETHOD.                    "generate_output

  METHOD set_pf_status.

    DATAlo_functions TYPE REF TO cl_salv_functions_list.
    lo_functions o_alv->get_functions).
    lo_functions->set_defaultabap_true ).

  ENDMETHOD.                    "set_pf_status
*
  METHOD set_filters.

    DATAlo_filters TYPE REF TO cl_salv_filters.
    DATAlo_filter  TYPE REF TO cl_salv_filter.

    lo_filters o_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'.
* save the values.
        TRY.
            lo_filter lo_filters->get_filter'ERDAT' ).
            t_selopt lo_filter->get).
          CATCH cx_salv_not_found.
        ENDTRY.

      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
*
  METHOD on_after_salv_function.

    CHECK e_salv_function '&ILT'
       OR e_salv_function '&ILD'.


    DATAlo_filters TYPE REF TO cl_salv_filters.
    DATAlo_filter  TYPE REF TO cl_salv_filter.
    DATAlt_selopt TYPE salv_t_selopt_ref.
*
    lo_filters o_alv->get_filters).
    TRY.
        lo_filter lo_filters->get_filter'ERDAT' ).

*      filter still there, check for the values
        lt_selopt lo_filter->get).
        IF lt_selopt NE t_selopt.

          TRY.
              CALL METHOD lo_filters->add_filter
                EXPORTING
                  columnname 'ERDAT'
                  sign       'I'
                  option     'EQ'
                  low        '20091214'.
            CATCH cx_salv_not_found .                   "#EC NO_HANDLER
            CATCH cx_salv_data_error .                  "#EC NO_HANDLER
            CATCH cx_salv_existing .                    "#EC NO_HANDLER
          ENDTRY.

        ENDIF.

*     when Filter is removed, this exception would be raised.
*       set it back
      CATCH cx_salv_not_found.

        TRY.
            CALL METHOD lo_filters->add_filter
              EXPORTING
                columnname 'ERDAT'
                sign       'I'
                option     'EQ'
                low        '20091214'.
          CATCH cx_salv_not_found .                     "#EC NO_HANDLER
          CATCH cx_salv_data_error .                    "#EC NO_HANDLER
          CATCH cx_salv_existing .                      "#EC NO_HANDLER
        ENDTRY.

    ENDTRY.

  ENDMETHOD.                    "on_After_salv_function

ENDCLASS.                    "lcl_report IMPLEMENTATION

Output of the program:

Here the output doesn’t show much of the program’s action. You may want to test it out and see how it is going ..!

Originally discussed at: Any way to define a non-modifiable (static) filter for an ALV grid?

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{272 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 272 articles.

Load comments

1 Comment

  • Suhas Saha

    Good one, Naimesh :-)

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.