Search in sources :

Example 1 with ComboBox

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);
}
Also used : ComboBox(com.vaadin.flow.component.combobox.ComboBox) PersonService(com.vaadin.flow.component.combobox.test.service.PersonService) Person(com.vaadin.flow.component.combobox.test.entity.Person)

Example 2 with 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);
}
Also used : ComboBox(com.vaadin.flow.component.combobox.ComboBox) PersonService(com.vaadin.flow.component.combobox.test.service.PersonService) Person(com.vaadin.flow.component.combobox.test.entity.Person)

Example 3 with 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);
}
Also used : Div(com.vaadin.flow.component.html.Div) 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) ComboBox(com.vaadin.flow.component.combobox.ComboBox) Element(com.vaadin.flow.component.combobox.test.entity.Element)

Example 4 with ComboBox

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);
}
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 5 with ComboBox

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);
}
Also used : Div(com.vaadin.flow.component.html.Div) ComboBox(com.vaadin.flow.component.combobox.ComboBox) VerticalLayout(com.vaadin.flow.component.orderedlayout.VerticalLayout)

Aggregations

ComboBox (com.vaadin.flow.component.combobox.ComboBox)69 Div (com.vaadin.flow.component.html.Div)35 Span (com.vaadin.flow.component.html.Span)21 NativeButton (com.vaadin.flow.component.html.NativeButton)20 Route (com.vaadin.flow.router.Route)18 List (java.util.List)16 H2 (com.vaadin.flow.component.html.H2)14 Collectors (java.util.stream.Collectors)14 Label (com.vaadin.flow.component.html.Label)12 ComponentRenderer (com.vaadin.flow.data.renderer.ComponentRenderer)12 Stream (java.util.stream.Stream)12 Component (com.vaadin.flow.component.Component)10 VerticalLayout (com.vaadin.flow.component.orderedlayout.VerticalLayout)10 ArrayList (java.util.ArrayList)10 Button (com.vaadin.flow.component.button.Button)9 Person (com.vaadin.flow.component.combobox.test.entity.Person)9 PersonService (com.vaadin.flow.component.combobox.test.service.PersonService)9 IntStream (java.util.stream.IntStream)9 ItemFilter (com.vaadin.flow.component.combobox.ComboBox.ItemFilter)8 Paragraph (com.vaadin.flow.component.html.Paragraph)8