Search in sources :

Example 1 with PersonService

use of com.vaadin.flow.component.combobox.test.service.PersonService 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 PersonService

use of com.vaadin.flow.component.combobox.test.service.PersonService 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 PersonService

use of com.vaadin.flow.component.combobox.test.service.PersonService 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)

Example 4 with PersonService

use of com.vaadin.flow.component.combobox.test.service.PersonService in project flow-components by vaadin.

the class ComboBoxView method lazyLoadingWithCustomItemCountEstimate.

private void lazyLoadingWithCustomItemCountEstimate() {
    // The backend will have 12345 items
    PersonService service = new PersonService(12345);
    ComboBox<Person> comboBox = new ComboBox<>();
    ComboBoxLazyDataView<Person> lazyDataView = comboBox.setItems(query -> service.fetch(query.getFilter().orElse(null), query.getOffset(), query.getLimit()));
    /*
         * By default, the combo box will initially adjust the scrollbar to 200
         * items and as the user scrolls down it automatically increases the
         * size by 200 until the backend runs out of items.
         *
         * Depending on the desired UX and the backend performance, the
         * scrolling experience and the number of items in the drop down can be
         * customized accordingly by constraining the page size, estimated item
         * count and its increase.
         */
    comboBox.setPageSize(10);
    lazyDataView.setItemCountEstimate(50);
    lazyDataView.setItemCountEstimateIncrease(50);
    // Showing the item count for demo purposes
    Div countText = new Div();
    lazyDataView.addItemCountChangeListener(event -> {
        if (event.isItemCountEstimated()) {
            countText.setText("Person Count Estimate: " + event.getItemCount());
        } else {
            countText.setText("Exact Person Count: " + event.getItemCount());
        }
    });
    HorizontalLayout layout = new HorizontalLayout(comboBox, countText);
    comboBox.setId("custom-item-count-estimate");
    addCard("Lazy Loading", "Custom Item Count Estimate And Increase", layout);
}
Also used : Div(com.vaadin.flow.component.html.Div) 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) HorizontalLayout(com.vaadin.flow.component.orderedlayout.HorizontalLayout)

Example 5 with PersonService

use of com.vaadin.flow.component.combobox.test.service.PersonService in project flow-components by vaadin.

the class ComboBoxView method pagedRepository.

private void pagedRepository() {
    ComboBox<Person> comboBox = new ComboBox<>();
    PersonService service = new PersonService();
    /*
         * For those backend repositories which use paged data fetching, it is
         * possible to get the page number and page size from Query API.
         */
    comboBox.setItems(query -> service.fetchPage(query.getFilter().orElse(null), query.getPage(), query.getPageSize()));
    comboBox.setId("paged-box");
    addCard("Lazy Loading", "Lazy Loading from Paged Repository", 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)

Aggregations

ComboBox (com.vaadin.flow.component.combobox.ComboBox)6 Person (com.vaadin.flow.component.combobox.test.entity.Person)6 PersonService (com.vaadin.flow.component.combobox.test.service.PersonService)6 Div (com.vaadin.flow.component.html.Div)2 HorizontalLayout (com.vaadin.flow.component.orderedlayout.HorizontalLayout)2 Component (com.vaadin.flow.component.Component)1 Button (com.vaadin.flow.component.button.Button)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 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 Anchor (com.vaadin.flow.component.html.Anchor)1 H2 (com.vaadin.flow.component.html.H2)1