Search in sources :

Example 1 with ComboBoxListDataView

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);
}
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)

Example 2 with ComboBoxListDataView

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);
}
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) Button(com.vaadin.flow.component.button.Button) ComboBox(com.vaadin.flow.component.combobox.ComboBox) PersonService(com.vaadin.flow.component.combobox.test.service.PersonService) IntegerField(com.vaadin.flow.component.textfield.IntegerField) Person(com.vaadin.flow.component.combobox.test.entity.Person)

Aggregations

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