Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Set Filter in an existing ALV- Layout

Former Member
0 Likes
9,057

Dear All,

I need to change an existing, stored ALV- Layout of an ABAP- Report. So the Problem is not to select the regarding ALV- Grid Layout at selection Screen with "cl_salv_layout_service=>f4_layouts" but to get the settings of the selected Layout in the Sourcecode and to change some Filtersettings with Methods like "lo_filters->add_filter" bevore it will be shown with "gr_alv->display( )".

maybe someone knows some code examples...

Thanks & Regards

Roland

14 REPLIES 14
Read only

naimesh_patel
Active Contributor
0 Likes
4,674

You need to only apply the filters in your code. If user selects the layout on the selection screen or it has any default layout, ALV framework will apply that layout on the ALV once your filters are applied.

To apply the filters, try like this:


*
    DATA: lo_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.
*

You can also check the filters example in this post - http://help-abap.blogspot.com/2009/12/salv-table-12-apply-filters.html

Regards,

Naimesh Patel

Read only

0 Likes
4,674

Thanks for your Help, but its not the problem to set filters in the standard ALV- Layout of Table- object co_alv. I need to choose a still defined Layout and in this Layout i have to set in addition to the existing settings (like filters, aggregations and so on... ) a "codet filter like you show it in the code- example below...

Now the problem isn't to set the Filter, but i need to know the steps to choose an existing Layout and in the next step i want to add some filter- criterias to it...

Thanx!

Best ragards,

Roland

Read only

0 Likes
4,674

Hi Roland,

after determining the layout in question, you can use method CL_SALV_LAYOUT->SET_INITIAL_LAYOUT to apply this layout. Then you can check the respective columns, sort and aggregation objects for the contents, add something, change something, remove something.

You can find the layouts in table LTDX, RELID = 'LT' and the reports name.

Regards,

Clemens

Read only

0 Likes
4,674

Hi Clemens,

DATA: gr_alv TYPE REF TO cl_salv_table.

  • Instanz für ALV-Objekt holen

TRY.

cl_salv_table=>factory(

EXPORTING

list_display = list_display

IMPORTING

r_salv_table = gr_alv

CHANGING

t_table = alv_table

).

ENDTRY.

.

.

.

*Layout Objekt erstellen

DATA: lr_layout TYPE REF TO cl_salv_layout,

ls_key TYPE salv_s_layout_key.

lr_layout = gr_alv->get_layout( ).

ls_key-report = sy-cprog.

*Layoutschlüssel setzen

lr_layout->set_key( value = ls_key ).

*Einschränkung zum Sichern des Layouts setzen

lr_layout->set_save_restriction(

value = if_salv_c_layout=>restrict_none).

*Layout Voreinstellung erlauben

lr_layout->set_default( value = 'X' ).

*Initiales Layout setzen

lr_layout->set_initial_layout( value = p_layout ).

After choosing the Layout, there is my Problem to add the filter- settings below... So the correct Layout is getting displayed but the additional filter criteria is missing...

*Filter im ALV Setzen

DATA: lo_filters TYPE REF TO cl_salv_filters.

DATA: lo_filter TYPE REF TO cl_salv_filter.

lo_filters = gr_alv->get_filters( ).

lo_filter = lo_filters->add_filter(

columnname = 'ARBPL'

  • sign =

  • option =

  • low =

  • high =

).

LOOP AT it_arbpl INTO wa_arbpl.

lo_filter->add_selopt(

sign = 'E'

option = wa_arbpl-option

low = wa_arbpl-low

high = wa_arbpl-high

).

ENDLOOP.

lo_filter = lo_filters->add_filter(

columnname = 'VVSTAT'

sign = 'I'

option = 'EQ'

low = ' '

high = ' '

).

gr_alv->display( ).

Read only

0 Likes
4,674

Hi Roland,

because your code is not formatted as code it is hard to read.

Did you try a (CL_SALV_TABLE) REFRESH after setting the layout?

Regards,

Clemens

Read only

0 Likes
4,674

Sorry for the format of the last posting...

to your Question: "Did you try a (CL_SALV_TABLE) REFRESH after setting the layout?"

No i don't... should I?

Read only

0 Likes
4,674

Sorry for the format of the last posting...

to your Question: "Did you try a (CL_SALV_TABLE) REFRESH after setting the layout?"

No i don't... should I?

Read only

0 Likes
4,674

Sorry for the suggestive character of my question. Yes, at least you should try. It is my assumption that

lr_layout->set_initial_layout( value = p_layout ).

will apply the layouts settings with filters and the like and further changes to the layout may need a refresh.

I never applied filters before display, only at runtime triggered by some toolbar buttons and then always followed by refresh. Excerpt from that code:

WHEN mc_colname_ap_date.
                <column_ref>-r_column->set_visible( abap_false ).
                lo_sorts->remove_sort( <column_ref>-columnname ).
              WHEN mc_colname_prof_date.
                <column_ref>-r_column->set_visible( abap_false ).
                lo_sorts->remove_sort( <column_ref>-columnname ).
              WHEN mc_colname_prof_time.
                <column_ref>-r_column->set_visible( abap_false ).
            ENDCASE.
        ENDCASE.
      ENDLOOP.
    CATCH cx_salv_data_error cx_salv_not_found cx_salv_existing
      INTO mo_exception.
      ASSERT 1 = 2.
  ENDTRY.

  enforce_column_sequence( ).
  mo_salv->refresh( refresh_mode  = if_salv_c_refresh=>full ).

Regards,

Clemens

Read only

0 Likes
4,674

It looks like, Initial Layout settings have the higher preference than the explicit supplied Filters.

Here is what's going on inside - In the method SET_TABLE_FOR_FIRST_DISPLAY, Filters are being set from the attribute IT_FILTERS in the attribute MT_FILTERS of Variant object M_CL_VARIANT. Later it loads the settings like Sort, filters from the initial Layout in the object M_CL_VARIANT using the method LOAD_VARIANT and in this step, it looses the content of the MT_FIILTERS. It gives the Filters of the Layout a higher preference than the additional supplied filters...


...
    m_cl_variant->mt_sort               = it_sort.
    m_cl_variant->mt_filter             = it_filter.
    m_cl_variant->m_variant_save        = i_save.
...
...
    call method m_cl_variant->load_variant
      exporting
        i_tabname          = ls_fieldcatalog-tabname
        i_bypassing_buffer = m_bypassing_buffer
        i_buffer_active    = m_buffer_active
        i_dialog           = space
        i_no_frontend      = abap_true
      importing
        e_exit             = l_no_variant_loaded.
...

Regards,

Naimesh Patel

Read only

0 Likes
4,674

OK, Thank you for your answers!

So if the Layout settings have the higher preference than the explicit supplied Filters, maybe changing the Layout itself is the only way to solve the problem?!?

Has anyone an idea to change an existing Layout with Coding, because i was allways trying to change the Table Settings in the past...

But the Methods of the class CL_SALV_LAYOUT did not seem promising enough...

F4_LAYOUTS

GET_CURRENT_LAYOUT

GET_DEFAULT_LAYOUT

GET_INITIAL_LAYOUT

GET_KEY

GET_LAYOUTS

GET_SAVE_RESTRICTION

HAS_DEFAULT

SET_DEFAULT

SET_INITIAL_LAYOUT

SET_KEY

SET_SAVE_RESTRICTION

Best Regards,

Roland

Read only

0 Likes
4,674

Does this mean you did not and will not try to use the

refresh( refresh_mode  = if_salv_c_refresh=>full )

after setting the filters?

Regards,

Clemens

Read only

0 Likes
4,674

You can try to save the Variant using the FM LVC_VARIANT_SAVE_LOAD. I haven't tried it but it is being called when we manually try to save the Layout. You can get the Filters and change them and than save them using this FM.

Regards,

Naimesh Patel

Read only

0 Likes
4,674

if I try it with your solution...

 
gr_alv->refresh( refresh_mode = if_salv_c_refresh=>full ).
  gr_alv->display( ).

...i get the following error...

 
     "OBJECTS_OBJREF_NOT_ASSIGNED_NO" "CX_SY_REF_IS_INITIAL"
     "CL_SALV_TABLE=================CP" bzw.
  "CL_SALV_TABLE=================CM014"
     "REFRESH"

 
    1 method refresh.
    2
    3   check me->r_controller is bound.
    4
    5   me->r_controller->set_changed(
    6     name         = 'CL_SALV_TABLE'
    7     method       = 'REFRESH'
    8     refresh_mode = refresh_mode
    9     flavour      = if_salv_c_changelist_flavour=>refresh ).
   10
   11   if refresh_mode = if_salv_c_refresh=>full.
   12     data: ir_adapter type ref to if_salv_adapter.
   13     ir_adapter = me->r_controller->IF_SALV_CONTROLLER_MODEL~R_ADAPTER.
>>>>>     IR_ADAPTER->APPLY_SERVICES( me->r_table ).
   15   endif.
   16
   17   me->r_controller->refresh( s_stable ).
   18
   19 endmethod.                    "refresh

Read only

0 Likes
4,674

This is really a shame. Shame on me! Shame on SAlv!

What could you possibly do?

Create the SALV instance

load layout variant

get filters, aggregations, sorts.

Ceate a new instance of SALV

merge new filters into existing filters

apply filters, aggregations, sorts to new salv instance.

display the new instance.

This is just an idea, but i think it could work.

Regards,

Clemens