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
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.
*
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.
DATA: t_selopt TYPE salv_t_selopt_ref.
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 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
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.
* Set default PF status
me->set_pf_status( ).
* Set Filters
me->set_filters( ).
* set events
DATA: lo_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.
DATA: lo_functions TYPE REF TO cl_salv_functions_list.
lo_functions = o_alv->get_functions( ).
lo_functions->set_default( abap_true ).
ENDMETHOD. "set_pf_status
*
METHOD set_filters.
DATA: lo_filters TYPE REF TO cl_salv_filters.
DATA: lo_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'.
DATA: lo_filters TYPE REF TO cl_salv_filters.
DATA: lo_filter TYPE REF TO cl_salv_filter.
DATA: lt_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
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?
Good one, Naimesh π