use of com.vaadin.flow.component.combobox.dataview.ComboBoxListDataView 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.dataview.ComboBoxListDataView in project flow-components by vaadin.
the class ComboBoxView method filteringAndSortingWithDataView.
private void filteringAndSortingWithDataView() {
// 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
ComboBox<Person> comboBox = new ComboBox<>("Persons");
PersonService personService = new PersonService();
// We fetch the items to the memory and bind the obtained collection
// to the combo box
Collection<Person> persons = personService.fetchAll();
ComboBoxListDataView<Person> dataView = comboBox.setItems(persons);
/*
* Providing a predicate item filter allows filtering by any field of
* the business entity and apply a combo box's text filter independently
*/
IntegerField personAgeFilter = new IntegerField(event -> dataView.setFilter(person -> event.getValue() == null || person.getAge() > event.getValue()));
/*
* Providing a value provider or comparator allows sorting combo box's
* items by custom field, or combination of fields
*/
Button sortPersons = new Button("Sort Persons by Name", event -> dataView.setSortOrder(Person::toString, SortDirection.ASCENDING));
personAgeFilter.setLabel("Filter Persons with age more than:");
personAgeFilter.setWidth(WIDTH_STRING);
addCard("Filtering", "Filtering and Sorting with Data View", comboBox, personAgeFilter, sortPersons);
}
Aggregations