Multiple Filter for Grid

Hello everyone,
I have two filters for a Grid. One is for name, one is for assignee field. When I type something in the textfields, it returns the correct result but when i clear the textfield or remove some letter from it , it doesn’t refresh itselfs. It contunies the filter the result.

For exapmle

Name Assignee
ABCDEF John
ABCDEF Jane

When I enter abc to name filter it returns both of the rows, then I enter john to assignee filter and it just returns the first raw. But when I clear the both filters, it doesnt return the second raw.

Here is my code;
TextField tfName = new TextField();
tfName.addValueChangeListener(event → {
dataProvider.refreshAll();
((InMemoryDataProvider) dataProvider).addFilter(task → {
if (event.getValue().equals(“”) || event.getValue() == null) {
return true;
}
String name = task.getName();
String nameLower = name == null ? “” : name.toLowerCase(Locale.ENGLISH);
String filterLower = event.getValue().toLowerCase(Locale.ENGLISH);
return nameLower.contains(filterLower);
});

    });
    filterRow.getCell("taskName").setComponent(tfName);

    TextField tfAssignee = new TextField();
    tfAssignee.addValueChangeListener(event -> {
        dataProvider.refreshAll();
        ((InMemoryDataProvider<Task>) dataProvider).addFilter(task -> {
            if (event.getValue().equals("") || event.getValue() == null) {
                return true;
            }
            String assignee = "";
            if (task.getAssignedUser() != null) {
                assignee = task.getAssignedUser().getFirstName() + " " + task.getAssignedUser().getLastName();
            }
            String nameLower = assignee == null ? "" : assignee.toLowerCase(Locale.ENGLISH);
            String filterLower = event.getValue().toLowerCase(Locale.ENGLISH);
            return nameLower.contains(filterLower);
        });

    });
    filterRow.getCell("taskAssignee").setComponent(tfAssignee);

Any idea how can solve this problem? Thanks in advance.

Instead of using addFilter, you should use setFilter, because addFilter will keep the old filters, that’s why the grid wasn’t refreshed.

yes, check all the fields from a single method is the way to go

But then it would not be multiple filter anymore. I actually solved this problem by using a filter() function that I implemented, I am checking all the fields from there.

     if (tfName.getValue() != null && !tfName.equals("")) {

        String name = task.getName();
        String nameLower = name == null ? "" : name.toLowerCase(Locale.ENGLISH);
        String filterLower = tfName.getValue().toLowerCase(Locale.ENGLISH);
        result = nameLower.contains(filterLower);
        if (result == false) {
            return result;
        }
    }
    if (tfAssignee.getValue() != null && !tfAssignee.equals("")) {

        String assignee = "";
        if (task.getAssignedUser() != null) {
            assignee = task.getAssignedUser().getFirstName() + " " + task.getAssignedUser().getLastName();
        }
        String assigneeLower = assignee == null ? "" : assignee.toLowerCase(Locale.ENGLISH);
        String filterLower = tfAssignee.getValue().toLowerCase(Locale.ENGLISH);
        result = assigneeLower.contains(filterLower);
        if (result == false) {
            return result;
        }
    }