Use Enter Key on filter conditions

Hello

Is there any way to apply filter conditions when Enter key is pressed?

Thank you in advance

By default, the filter is applied when the user changes conditions (see autoApply), and for a String condition it means when the user presses Enter in the text field.

Isn’t it what you need?

Hi @mihai.maranduca ,

I used to do a workaround for this since CUBA times, since the Filter element doesn’t surpport this requirement…

I added in the Descriptor a vbox, give it an ID (e.g. filterBox) and put the filter component into that.

        <vbox spacing="false" id="filterBox">
            <filter id="filter"
                        dataLoader="usersDl">
                <properties include=".*"/>
            </filter>
        </vbox>

Then in the Controller inject the filterBox and the filter component and inside one of your starting events (e.g. onBeforeShow) add a ‘ENTER’ ShortcutAction to your filterBox which trigger then the filter apply.

    @Autowired
    private VBoxLayout filterBox;
    @Autowired
    private Filter filter;

    @Subscribe
    public void onBeforeShow(BeforeShowEvent event) {
        filterBox.addShortcutAction(new ShortcutAction("ENTER",
                shortcutTriggeredEvent -> filter.apply()));
    }

This works for classic UI - not sure what to do for Flow UI.

BR
Stefan

1 Like

Good morning, Thank you for your time
This logic/idea seems correct to me, but I don´t know why it doesn´t work in my app. I guess I´ll use the default shortcut “shift+enter” to apply the filter from keyboard and waiting for an update that, if I´m lucky, it will incorporate this requirement.

Good moring.
I specially put autoApply on FALSE because in the filter I have 2 dates conditions (Users could add any other conditions like string one). If I set autoApply on true, on init it will load all my database records. this will slow down the app enormously (even with pagination).
Initially I wanted to set filter conditions required to avoid that massive data load, like in CUBA, but I see Jmix doesn´t have this option.
Also in CUBA you can change default shortcut combination in application.propperties, But Jmix doesn´t have this possibility neither, I´m I wrong?

Hi @mihai.maranduca
You are right, it’s not working regarding the autoApply=FALSE.
I expected you had enabled the autoApply.

It has nothing to do with your apply shortcut at all - we call the apply function just in the program.

Since in Jmix (different to CUBA) the filter.apply() method check the autoApply and if it is disabled, it doesn’t do a dataLoader.load() by themself.
So you have todo the dataLoader.load() by your own.

Please change the controller and inject your dataLoader (here it’s usersDl) then change the lambda to call the load by your own:

    @Autowired
    private VBoxLayout filterBox;
    @Autowired
    private Filter filter;
    @Autowired
    private CollectionLoader<User> usersDl;

    @Subscribe
    public void onBeforeShow(BeforeShowEvent event) {
        filterBox.addShortcutAction(new ShortcutAction("ENTER",
                shortcutTriggeredEvent -> {
                    filter.apply();
                    usersDl.load();
                }));
    }

ATTENTION: don’t call the load method when you have autoApply=TRUE then the filter.apply() mothod will do this for you!

BR
Stefan

Now it´s working the way I like. It was not a life-death “problem”… more like a ‘touch’ to my app for making it more comfortable to use. So thank you very much for your time.

You are right. We’ll do it: [Filter] Provide the ability to change apply-shortcut · Issue #1573 · jmix-framework/jmix · GitHub

Hello
Good to know, I´ll be aware

Thank you very much