Programatically Clear Filter

I have a browse screen with a Default filter being applied. There is an instance when this browse screen is opened as a lookup from a specific action and in this case I do not want this filter to be applied. Is there a way to programmatically remove the default filter?

I have programmatically set filters before in the controller for the browse screen based on passed parameters but I cannot figure out how to remove a filter with this approach.

Thank you,
Corey Amoruso

Hi,
When you see no filter that actually means that a filter with no conditions is applied. In the browser screen you may do the same. Your code will be something like this:

    @Inject
    private Filter filter;

    @Inject
    private Metadata metadata;

    @Inject
    private FilterParser filterParser;

    @Inject
    private UserSessionSource userSessionSource;

    @Subscribe("resetFilterBtn")
    public void onResetFilterBtnClick(Button.ClickEvent event) {
        FilterEntity adHocFilter = metadata.create(FilterEntity.class);
        String emptyXml = filterParser.getXml(new ConditionsTree(), Param.ValueProperty.VALUE);
        adHocFilter.setXml(emptyXml);
        adHocFilter.setComponentId(ComponentsHelper.getFilterComponentPath(filter));
        adHocFilter.setUser(userSessionSource.getUserSession().getCurrentOrSubstitutedUser());
        adHocFilter.setName("");
        filter.setFilterEntity(adHocFilter);
    }
1 Like

Thank you for this detailed explanation, this is just what I was looking for.

how can you set some conditions?

If you want to create a new filter with parameters from scratch, then you need to set proper XML in the filter.setXml(...) method. Take a look at what XML is stored in the database for some existing filter - that will give you an idea how to build such an XML.
If you need to set a value to a condition of existing filter, then maybe this API can help you.

1 Like