Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

How To Add Filter Functionality in ALV Tree

BY RUSHANA KHAN, SAP ABAP CONSULTANT

😊
Hope this blog will help my fellow ABAPers who are developing out-of-the-box solutions every single day
​. If you know other ways of adding this functionality, please do share!

CASE​:​ Few months back, our client had a requirement to add filter criteria for Item Category in ALV Tree
in the custom report.

Challenge​: Currently Standard SAP does not cater filter functionality in CL_ALV_TREE.
Solution​: I was able to complete this requirement by adding a custom button on the ALV Toolbar and
using standard Function Module ​'LVC_FILTER’​ sharing the solution in attachment.

For Demo purposes, I have created a custom program from standard report BCALV_TREE_DEMO and
done custom changes to add filter functionality.

The below program shows the places at which I have incorporated the changes.

Here are the steps followed:

1. In the below program , under PBO in perform init_tree , I have added custom Filter button in
toolbar under subroutine perform ​change_toolbar​.

BY RUSHANA KHAN (SAP ABAP CONSULTANT)


2. Since a new button in alv toolbar is added, defining a local class lcl_toolbar_event_receiver in
the program , since this is already present in the standard program, declared a new method
on_filter_selected

Under the implementation part of the method on_filter_selected, adding the below code

Our filter functionality logic is in the subroutine filter_function

*&---------------------------------------------------------------------*
*& Form FILTER_FUNCTION
*&---------------------------------------------------------------------*
* Filter Functionality for screen 100
*----------------------------------------------------------------------
FORM ​FILTER_FUNCTION ​.
DATA​: ​lr_filters ​TYPE REF TO ​cl_salv_filters​,
lr_filter ​TYPE REF TO ​cl_salv_filter​,
lt_filters ​TYPE ​salv_t_filter_ref​,
ls_filter ​TYPE ​salv_s_filter_ref​,

BY RUSHANA KHAN (SAP ABAP CONSULTANT)


lt_group1 ​LIKE ​ vc_s_sgrp ​OCCURS ​0​,
l
ls_group ​LIKE ​lvc_s_sgrp​,
filter_ranges ​TYPE ​lvc_t_filt​,
lt_sel ​TYPE ​lvc_t_fnam​,
lt_flt ​TYPE ​lvc_t_filt​,
ls_sel ​TYPE ​lvc_fname​,
lt_filter_index ​TYPE ​lvc_t_filt​,
lt_filter ​TYPE ​lvc_t_fidx​.

​DATA ​: ​ir_salv_table ​TYPE REF TO ​cl_salv_table​.

* RANGES : lr_pstyv FOR vbap-pstyv.

​DATA ​: l
​ t_col ​TYPE ​lvc_t_col​,
ls_col ​TYPE ​lvc_s_col​,
ls_layo ​TYPE ​lvc_s_layo​,
lv_idx ​TYPE ​sy​-​tabix​,
lx_hierarchy_header ​TYPE ​treev_hhdr​,
ls_vari ​TYPE ​disvariant​,
lt_filt_temp ​TYPE ​lvc_t_fidx​.

​CALL METHOD ​tree1​->​get_selected_columns


​IMPORTING
et_sel_columns ​= ​lt_sel​.

​LOOP AT ​lt_sel ​INTO ​ls_sel​.


ls_col​-​fieldname ​= ​ls_sel​.
​APPEND ​ls_col ​TO ​lt_col​.

​ENDLOOP​.

​CALL FUNCTION ​'LVC_FILTER'


​EXPORTING
i_callback_programm ​= ​sy​-​repid
it_fieldcat ​= ​gt_fieldcatalog
it_selected_cols ​= ​lt_col
* IT_VALUE_UNIT =
* IT_GROUPLEVELS =
is_layout ​= ​ls_layo
* IS_SELFIELD =
* IT_GROUPS =
* IS_FILT_LAYOUT =
* IT_EVENTS =
* I_NO_DIALOG =
* IT_EXCEPT_QINFO =
* I_IGNORING_CASE =
​IMPORTING
et_filter_index ​= ​lt_filter
* ET_GROUPLEVELS_FILTER =
* ET_FILTER_INDEX_INSIDE =

BY RUSHANA KHAN (SAP ABAP CONSULTANT)


* E_FILTER_FLAGNAME =
​TABLES
it_data ​= ​gt_sflight
​CHANGING
ct_filter ​= ​lt_filter_index
​EXCEPTIONS
no_change ​= ​1
​OTHERS ​= ​2​.
​IF ​sy​-​subrc <> ​0​.
* Implement suitable error handling here
​ENDIF​.

**Post this you can add your logic


​CLEAR ​: ​lv_idx​.
if ​lt_filter ​is NOT INITIAL​.
loop at ​gt_sflight ​ASSIGNING FIELD​-​SYMBOL​(​<ls_filter>​).
lv_idx ​= ​sy​-​tabix​.
​READ TABLE ​lt_filter ​INTO DATA​(​ls_filter_in​) ​WITH KEY ​table_line ​= ​lv
_idx​.
​IF ​sy​-​subrc ​= ​0​.
<ls_filter>​-​del ​= ​abap_true​.
​ENDIF​.
lv_idx ​= ​lv_idx + ​1​.
ENDLOOP​.

delete ​gt_sflight ​where ​del ​= ​abap_true​.


endif​.

**We have to again free our container and the tree and recreate
PERFORM ​tree_recreate​.
ENDFORM​.

Output:

BY RUSHANA KHAN (SAP ABAP CONSULTANT)

You might also like