Search in sources :

Example 1 with Ticket

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);
}
Also used : ItemFilter(com.vaadin.flow.component.combobox.ComboBox.ItemFilter) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) Person(com.vaadin.flow.component.combobox.test.entity.Person) Image(com.vaadin.flow.component.html.Image) SortDirection(com.vaadin.flow.data.provider.SortDirection) Component(com.vaadin.flow.component.Component) ProjectData(com.vaadin.flow.component.combobox.test.data.ProjectData) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout) ElementData(com.vaadin.flow.component.combobox.test.data.ElementData) Div(com.vaadin.flow.component.html.Div) ComboBox(com.vaadin.flow.component.combobox.ComboBox) Song(com.vaadin.flow.component.combobox.test.entity.Song) ArrayList(java.util.ArrayList) Route(com.vaadin.flow.router.Route) FlexComponent(com.vaadin.flow.component.orderedlayout.FlexComponent) FlexLayout(com.vaadin.flow.component.orderedlayout.FlexLayout) TemplateRenderer(com.vaadin.flow.data.renderer.TemplateRenderer) IntegerField(com.vaadin.flow.component.textfield.IntegerField) ElementConstants(com.vaadin.flow.dom.ElementConstants) Department(com.vaadin.flow.component.combobox.test.entity.Department) Paragraph(com.vaadin.flow.component.html.Paragraph) Notification(com.vaadin.flow.component.notification.Notification) Ticket(com.vaadin.flow.component.combobox.test.entity.Ticket) Project(com.vaadin.flow.component.combobox.test.entity.Project) Anchor(com.vaadin.flow.component.html.Anchor) Query(com.vaadin.flow.data.provider.Query) ComboBoxLazyDataView(com.vaadin.flow.component.combobox.dataview.ComboBoxLazyDataView) Collection(java.util.Collection) VerticalLayout(com.vaadin.flow.component.orderedlayout.VerticalLayout) PersonService(com.vaadin.flow.component.combobox.test.service.PersonService) H2(com.vaadin.flow.component.html.H2) Element(com.vaadin.flow.component.combobox.test.entity.Element) DepartmentData(com.vaadin.flow.component.combobox.test.data.DepartmentData) List(java.util.List) Button(com.vaadin.flow.component.button.Button) Stream(java.util.stream.Stream) ComboBoxListDataView(com.vaadin.flow.component.combobox.dataview.ComboBoxListDataView) Span(com.vaadin.flow.component.html.Span) Ticket(com.vaadin.flow.component.combobox.test.entity.Ticket) Button(com.vaadin.flow.component.button.Button) ComboBox(com.vaadin.flow.component.combobox.ComboBox) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout)

Aggregations

Component (com.vaadin.flow.component.Component)1 Button (com.vaadin.flow.component.button.Button)1 ComboBox (com.vaadin.flow.component.combobox.ComboBox)1 ItemFilter (com.vaadin.flow.component.combobox.ComboBox.ItemFilter)1 ComboBoxLazyDataView (com.vaadin.flow.component.combobox.dataview.ComboBoxLazyDataView)1 ComboBoxListDataView (com.vaadin.flow.component.combobox.dataview.ComboBoxListDataView)1 DepartmentData (com.vaadin.flow.component.combobox.test.data.DepartmentData)1 ElementData (com.vaadin.flow.component.combobox.test.data.ElementData)1 ProjectData (com.vaadin.flow.component.combobox.test.data.ProjectData)1 Department (com.vaadin.flow.component.combobox.test.entity.Department)1 Element (com.vaadin.flow.component.combobox.test.entity.Element)1 Person (com.vaadin.flow.component.combobox.test.entity.Person)1 Project (com.vaadin.flow.component.combobox.test.entity.Project)1 Song (com.vaadin.flow.component.combobox.test.entity.Song)1 Ticket (com.vaadin.flow.component.combobox.test.entity.Ticket)1 PersonService (com.vaadin.flow.component.combobox.test.service.PersonService)1 Anchor (com.vaadin.flow.component.html.Anchor)1 Div (com.vaadin.flow.component.html.Div)1 H2 (com.vaadin.flow.component.html.H2)1 Image (com.vaadin.flow.component.html.Image)1