SearchSelect
SearchSelect<T> selects one item from a list, with type-to-search.
Semantically it is a Select (no custom values), but the user can type to
filter. It looks and behaves exactly like Vaadin's ComboBox, because it is
the same vaadin-combo-box web component, but the server side is written from
scratch for one goal: no unnecessary server round trips.
- Small and medium lists (up to 1000 items by default) are sent to the browser once and filtered there. Typing causes no server visits at all; only the selected item is sent back.
- Large lists and backend queries are filtered on the server, but only when the user pauses typing (300 ms by default). A fluent typist causes one request, not one per keystroke. While waiting, results that are already in the browser are narrowed down at once.
- The default filter matches all typed words, in any order, ignoring case
and accents:
pk juofinds “Juoksu – (PK1) kevyt”,paivafinds “Päivä”. - No custom frontend bundle is needed. The web component comes with the platform, driven by a small connector script.
Since Viritin 3.10.0. The API may still change, so the class is marked
@Deprecated(forRemoval = false) for now as an “experimental” flag.
Basic usage
SearchSelect<TrainingType> type = new SearchSelect<>("Training type", TrainingType.all());
type.setItemLabelGenerator(TrainingType::label);
type.setPlaceholder("Type e.g. pk juo");
type.addValueChangeListener(e -> Notification.show("Selected " + e.getValue()));
It is a regular HasValue field, so it works with Binder, FormBinder and
the rest. It also supports label, helper, placeholder, clear button, tooltip,
required indicator, validation state and the accessible name.
Finding items by more than their label
Use setSearchTextGenerator to let users find items by synonyms, codes or
any other text that isn't shown:
country.setItemLabelGenerator(Country::name);
country.setSearchTextGenerator(c -> c.name() + " " + c.isoCode() + " " + c.nativeName());
Custom client-side filter
The browser filter can be replaced with the body of a JavaScript function
(item, filter, normalize) => boolean. item.label and item.search are the
item's label and search text, and normalize(text) lower-cases the text and
removes accents. For example, a prefix match:
select.setClientFilter("return normalize(item.label).startsWith(normalize(filter));");
Large lists and backend data
When there are more in-memory items than setClientSideItemLimit(int)
(default 1000), filtering moves to the server automatically. It uses the same
rule as the browser, implemented in Java in SearchMatching.matchesAllWords.
For data that shouldn't be loaded into memory at all, give a fetch callback. The query's filter is the typed text, and its limit is the server result limit (default 100):
select.setItems(query -> repository
.findByNameContainingIgnoreCase(query.getFilter().orElse(""),
PageRequest.of(0, query.getLimit()))
.stream());
You can tune the behaviour:
setServerFilterDelay(Duration)– how long a typing pause triggers a server search (default 300 ms).setServerResultLimit(int)– how many matches at most are shown (default 100). Typing more narrows them down.filter(String)– the items shown for a given text, applying the same logic as the UI. Handy in tests and server-side logic.
Differences from ComboBox
SearchSelect isn't a drop-in replacement for ComboBox:
- There are no custom values (
allowCustomValue) and no way to add new items from the field. Pick aComboBoxif the user must be able to type a value that isn't in the list. - There are no item renderers. Items are shown as plain text labels.
- There is no
DataProvideror paging-by-scrolling. Large result sets are cut at the result limit, because a long filtered list is something you type more into, not scroll.
