use of com.vaadin.flow.component.combobox.ComboBox in project flow-components by vaadin.
the class ComboBoxView method filteringWithTypesOtherThanString.
private void filteringWithTypesOtherThanString() {
// PersonService can be found:
// https://github.com/vaadin/vaadin-combo-box-flow/tree/master/vaadin-combo-box-flow-demo/src/main/java/com/vaadin/flow/component/combobox/demo/service/PersonService.java
PersonService personService = new PersonService(500);
ComboBox<Person> comboBox = new ComboBox<>("Person");
comboBox.setPlaceholder("Enter minimum age to filter");
comboBox.setPattern("^\\d+$");
comboBox.setPreventInvalidInput(true);
// Configuring fetch callback with a filter converter, so entered filter
// strings can refer also to other typed properties like age (integer):
comboBox.setItemsWithFilterConverter(query -> personService.fetchOlderThan(query.getFilter().orElse(null), query.getOffset(), query.getLimit()), ageStr -> ageStr.trim().isEmpty() ? null : Integer.parseInt(ageStr));
comboBox.setItemLabelGenerator(person -> person.getFirstName() + " " + person.getLastName() + " - " + person.getAge());
comboBox.setClearButtonVisible(true);
comboBox.setWidth(WIDTH_STRING);
addCard("Filtering", "Filtering with types other than String", comboBox);
}
use of com.vaadin.flow.component.combobox.ComboBox in project flow-components by vaadin.
the class ComboBoxView method lazyLoadingWithExactItemCount.
private void lazyLoadingWithExactItemCount() {
ComboBox<Person> comboBox = new ComboBox<>();
PersonService service = new PersonService();
/*
* By using these callbacks the ComboBox doesn't load all the items to
* the server memory right away. The ComboBox calls the first provided
* callback to fetch items from the given range with the given filter.
* The second callback is optional and can be used to determine an exact
* count of items that match the query, if the exact count is desired.
*/
comboBox.setItems(query -> service.fetch(query.getFilter().orElse(null), query.getOffset(), query.getLimit()), query -> service.count(query.getFilter().orElse(null)));
comboBox.setId("with-exact-items-count");
addCard("Lazy Loading", "Lazy Loading with Exact Items Count", comboBox);
}
use of com.vaadin.flow.component.combobox.ComboBox in project flow-components by vaadin.
the class ComboBoxView method customFiltering.
private void customFiltering() {
Div div = new Div();
div.setText("Example uses case-sensitive starts-with filtering");
ComboBox<Element> filteringComboBox = new ComboBox<>();
List<Element> elementsList = getElements();
/*
* Providing a custom item filter allows filtering based on all of the
* rendered properties:
*/
ItemFilter<Element> filter = (element, filterString) -> element.getName().startsWith(filterString);
filteringComboBox.setItems(filter, elementsList);
filteringComboBox.setItemLabelGenerator(Element::getName);
filteringComboBox.setClearButtonVisible(true);
addCard("Filtering", "Custom filtering", div, filteringComboBox);
}
use of com.vaadin.flow.component.combobox.ComboBox in project flow-components by vaadin.
the class ComboBoxView method itemCountChangeNotification.
private void itemCountChangeNotification() {
ComboBox<Ticket> comboBox = new ComboBox<>("Available tickets");
comboBox.setPlaceholder("Select a ticket");
Collection<Ticket> tickets = generateTickets();
ComboBoxListDataView<Ticket> dataView = comboBox.setItems(tickets);
Button buyTicketButton = new Button("Buy a ticket", click -> comboBox.getOptionalValue().ifPresent(dataView::removeItem));
/*
* If you want to get notified when the ComboBox's items count has
* changed on the server-side, i.e. due to adding or removing an
* item(s), or by changing the server-side filtering, you can add a
* listener using a data view API.
*
* Please note that the ComboBox's client-side filter change won't fire
* the event, since it doesn't change the item count on the server-side,
* but only reduces the item list in UI and makes it easier to search
* through the items.
*/
dataView.addItemCountChangeListener(event -> comboBox.getOptionalValue().ifPresent(ticket -> {
if (event.getItemCount() > 0) {
Notification.show(String.format("Ticket with %s is sold. %d ticket(s) left", ticket, event.getItemCount()), 3000, Notification.Position.MIDDLE);
} else {
Notification.show("All tickets were sold out", 3000, Notification.Position.MIDDLE);
buyTicketButton.setEnabled(false);
}
comboBox.clear();
}));
HorizontalLayout layout = new HorizontalLayout(comboBox, buyTicketButton);
layout.setAlignItems(FlexComponent.Alignment.BASELINE);
addCard("Item Count Change Notification", layout);
}
use of com.vaadin.flow.component.combobox.ComboBox in project flow-components by vaadin.
the class ComboBoxView method valueChangeEvent.
private void valueChangeEvent() {
ComboBox<String> comboBox = new ComboBox<>();
comboBox.setLabel("Label");
comboBox.setItems("Option one", "Option two");
comboBox.setClearButtonVisible(true);
Div value = new Div();
value.setText("Select a value");
comboBox.addValueChangeListener(event -> {
if (event.getValue() == null) {
value.setText("No option selected");
} else {
value.setText("Selected: " + event.getValue());
}
});
VerticalLayout verticalLayout = new VerticalLayout(comboBox, value);
verticalLayout.setAlignItems(FlexComponent.Alignment.START);
addCard("Value change event", verticalLayout);
}
Aggregations