use of com.vaadin.flow.component.combobox.test.entity.Ticket 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);
}
Aggregations