Skip to content

Instantly share code, notes, and snippets.

@mstahv
Created January 26, 2023 16:52
Show Gist options
  • Save mstahv/05d3ae2142185de86bfac1ed84b03628 to your computer and use it in GitHub Desktop.
Save mstahv/05d3ae2142185de86bfac1ed84b03628 to your computer and use it in GitHub Desktop.
Abusing Vaadin ComboBox lazy data binding for filtering other components using ComboBox
ArrayList<String> values = new ArrayList<>();
values.add("foo");
values.add("bar");
ComboBox<String> stringComboBox = new ComboBox<>();
stringComboBox.setItemsWithFilterConverter((CallbackDataProvider.FetchCallback<String, String>) query -> {
if(query.getFilter().isPresent()) {
String filter = query.getFilter().get();
System.out.println("Do something else with filter: " + filter);
return values.stream()
.filter(s -> s.startsWith(filter.toString()))
.skip(query.getOffset()).limit(query.getPageSize());
}
return values.stream().skip(query.getOffset()).limit(query.getPageSize());
}, s -> s);
stringComboBox.addValueChangeListener(v -> {
// Selecting existing values
});
stringComboBox.addCustomValueSetListener(v -> {
// adding new values (probably all intercepted already in lazy data binding)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment