Filter abort apply

Hi,
Wich way to prevent filter apply if some condition properties value on filter was wrong?

In cuba we can do it by event beforeFilterAppliedHandler. But now?

regards

gabriel

Hi,

You can use the PreLoadEvent of the DataLoader.

Something like this:

import com.company.extendreportscreen.entity.User;
import io.jmix.core.querycondition.Condition;
import io.jmix.core.querycondition.LogicalCondition;
import io.jmix.core.querycondition.PropertyCondition;
import io.jmix.ui.Notifications;
import io.jmix.ui.component.Filter;
import io.jmix.ui.model.CollectionLoader;
import io.jmix.ui.navigation.Route;
import io.jmix.ui.screen.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;


@UiController("User.browse")
@UiDescriptor("user-browse.xml")
@LookupComponent("usersTable")
@Route("users")
public class UserBrowse extends StandardLookup<User> {

    @Autowired
    private Filter filter;

    @Autowired
    private Notifications notifications;

    @Subscribe(id = "usersDl", target = Target.DATA_LOADER)
    public void onUsersDlPreLoad(CollectionLoader.PreLoadEvent<User> event) {
        Filter.Configuration currentConfiguration = filter.getCurrentConfiguration();
        LogicalCondition queryCondition = currentConfiguration.getQueryCondition();
        for (Condition condition : queryCondition.getConditions()) {
            if (condition instanceof PropertyCondition propertyCondition) {
                String property = propertyCondition.getProperty();
                if ("username".equals(property)) {
                    String value = (String) propertyCondition.getParameterValue();
                    if (value != null && value.startsWith("a")) {
                        notifications.create(Notifications.NotificationType.WARNING)
                                .withCaption("I will not filter for usernames starting with 'a' letter")
                                .show();
                        event.preventLoad();
                    }
                }
            }
        }
    }
}