How to persist filter values across different views in Jmix 2+?

Title: How to persist filter values across different views in Jmix 2+?

Description:
Hi,

I’m developing a web application using Jmix 2.2.5 and Spring Boot 3.2. I want to persist the values of filter fields in a ListView, so that when I return to the view (even after visiting other views), the previously entered filter values are still set.

I’ve tried using a @VaadinSessionScope bean to store the filter values, restore them in the BeforeShow/ReadyEvent and reload the data provider, but the solution is not working as expected. The filter values are not always restored.

Has anyone implemented a similar feature? Is there an official or recommended approach to achieve persistent filter fields across navigation in Jmix?

Thank you!

FilterStateHolder.java (600 Bytes)
FpspListView.java (1.6 KB)
fpsp-list-view.xml (3.7 KB)
FpspDetailView.java (569 Bytes)
fpsp-detail-view.xml (2.2 KB)
Fpsp.java (1.9 KB)

Hello!

I think the simplest way to store values from PropertyFilters is to use the settings facet. It saves values in the database for a specific user and view, including any provided settings.

For example, add the settings facet to the XML and inject it into the controller:

<settings id="settingsFacet" auto="true"/>

You can remove the auto attribute or set it to false if you do not want the facet to save the states of other components.

@ViewComponent
private PropertyFilter<String> usernamePropertyFilter;

@ViewComponent
private SettingsFacet settingsFacet;

@Subscribe
public void onBeforeShow(final BeforeShowEvent event) {
    String value = Objects.requireNonNull(settingsFacet.getSettings())
            .getString("usernamePropertyFilter", "value")
            .orElse(null);
    usernamePropertyFilter.setValue(Strings.nullToEmpty(value));
}

@Subscribe
public void onReady(final ReadyEvent event) {
    usernamePropertyFilter.apply();
}

@Subscribe
public void onBeforeClose(final BeforeCloseEvent event) {
    Objects.requireNonNull(settingsFacet.getSettings())
            .put("usernamePropertyFilter", "value", usernamePropertyFilter.getValue());
}

If you want to store settings in memory, it is better to use the @UIScope annotation instead of the session scope. This is because in the case of session scope, you also need to store information about the UI instance. Otherwise the stored values will be shared across multiple web browser tabs showing the same view.

1 Like

Thanks.

And how to persist value filtered over column in a datagrid?