Search in sources :

Example 1 with ItemFilter

use of com.vaadin.flow.component.combobox.ComboBox.ItemFilter 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 2 with ItemFilter

use of com.vaadin.flow.component.combobox.ComboBox.ItemFilter in project flow-components by vaadin.

the class ComboBoxDemoPage method createComboBoxUsingTemplateRenderer.

private void createComboBoxUsingTemplateRenderer() {
    Div message = createMessageDiv("template-selection-message");
    ComboBox<Song> comboBox = new ComboBox<>();
    List<Song> listOfSongs = createListOfSongs();
    /*
         * Providing a custom item filter allows filtering based on all of the
         * rendered properties:
         */
    ItemFilter<Song> filter = (song, filterString) -> song.getName().toLowerCase().contains(filterString.toLowerCase()) || song.getArtist().toLowerCase().contains(filterString.toLowerCase());
    comboBox.setItems(filter, listOfSongs);
    comboBox.setItemLabelGenerator(Song::getName);
    comboBox.setRenderer(TemplateRenderer.<Song>of("<div>[[item.song]]<br><small>[[item.artist]]</small></div>").withProperty("song", Song::getName).withProperty("artist", Song::getArtist));
    comboBox.addValueChangeListener(event -> {
        if (event.getSource().isEmpty()) {
            message.setText("No artist selected");
        } else if (event.getOldValue() == null) {
            message.setText("Selected artist: " + event.getValue().getArtist());
        } else {
            message.setText("Selected artist: " + event.getValue().getArtist() + "\nThe old selection was: " + event.getOldValue().getArtist());
        }
    });
    comboBox.getStyle().set(ElementConstants.STYLE_WIDTH, WIDTH_STRING);
    comboBox.setId("template-selection-box");
    add(new Div(new H2("Using templates"), new H2("Rendering items using TemplateRenderer"), comboBox, message));
}
Also used : Div(com.vaadin.flow.component.html.Div) IntStream(java.util.stream.IntStream) ItemFilter(com.vaadin.flow.component.combobox.ComboBox.ItemFilter) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) VerticalLayout(com.vaadin.flow.component.orderedlayout.VerticalLayout) Div(com.vaadin.flow.component.html.Div) Label(com.vaadin.flow.component.html.Label) H2(com.vaadin.flow.component.html.H2) ComboBox(com.vaadin.flow.component.combobox.ComboBox) Collectors(java.util.stream.Collectors) Route(com.vaadin.flow.router.Route) ArrayList(java.util.ArrayList) List(java.util.List) Faker(com.github.javafaker.Faker) TemplateRenderer(com.vaadin.flow.data.renderer.TemplateRenderer) ElementConstants(com.vaadin.flow.dom.ElementConstants) ComboBox(com.vaadin.flow.component.combobox.ComboBox) H2(com.vaadin.flow.component.html.H2)

Example 3 with ItemFilter

use of com.vaadin.flow.component.combobox.ComboBox.ItemFilter in project flow-components by vaadin.

the class ComboBoxDemoPage method createComboBoxUsingComponentRenderer.

private void createComboBoxUsingComponentRenderer() {
    Div message = createMessageDiv("component-selection-message");
    ComboBox<Song> comboBox = new ComboBox<>();
    List<Song> listOfSongs = createListOfSongs();
    /*
         * Providing a custom item filter allows filtering based on all of the
         * rendered properties:
         */
    ItemFilter<Song> filter = (song, filterString) -> song.getName().toLowerCase().contains(filterString.toLowerCase()) || song.getArtist().toLowerCase().contains(filterString.toLowerCase());
    comboBox.setItems(filter, listOfSongs);
    comboBox.setItemLabelGenerator(Song::getName);
    comboBox.setRenderer(new ComponentRenderer<>(item -> {
        VerticalLayout container = new VerticalLayout();
        Label song = new Label(item.getName());
        container.add(song);
        Label artist = new Label(item.getArtist());
        artist.getStyle().set("fontSize", "smaller");
        container.add(artist);
        return container;
    }));
    comboBox.addValueChangeListener(event -> {
        if (event.getSource().isEmpty()) {
            message.setText("No artist selected");
        } else if (event.getOldValue() == null) {
            message.setText("Selected artist: " + event.getValue().getArtist());
        } else {
            message.setText("Selected artist: " + event.getValue().getArtist() + "\nThe old selection was: " + event.getOldValue().getArtist());
        }
    });
    comboBox.getStyle().set(ElementConstants.STYLE_WIDTH, WIDTH_STRING);
    comboBox.setId("component-selection-box");
    add(new Div(new H2("Using components"), new H2("Rendering items using ComponentTemplateRenderer"), comboBox, message));
}
Also used : Div(com.vaadin.flow.component.html.Div) IntStream(java.util.stream.IntStream) ItemFilter(com.vaadin.flow.component.combobox.ComboBox.ItemFilter) ComponentRenderer(com.vaadin.flow.data.renderer.ComponentRenderer) VerticalLayout(com.vaadin.flow.component.orderedlayout.VerticalLayout) Div(com.vaadin.flow.component.html.Div) Label(com.vaadin.flow.component.html.Label) H2(com.vaadin.flow.component.html.H2) ComboBox(com.vaadin.flow.component.combobox.ComboBox) Collectors(java.util.stream.Collectors) Route(com.vaadin.flow.router.Route) ArrayList(java.util.ArrayList) List(java.util.List) Faker(com.github.javafaker.Faker) TemplateRenderer(com.vaadin.flow.data.renderer.TemplateRenderer) ElementConstants(com.vaadin.flow.dom.ElementConstants) ComboBox(com.vaadin.flow.component.combobox.ComboBox) Label(com.vaadin.flow.component.html.Label) VerticalLayout(com.vaadin.flow.component.orderedlayout.VerticalLayout) H2(com.vaadin.flow.component.html.H2)

Aggregations

ComboBox (com.vaadin.flow.component.combobox.ComboBox)3 ItemFilter (com.vaadin.flow.component.combobox.ComboBox.ItemFilter)3 Div (com.vaadin.flow.component.html.Div)3 H2 (com.vaadin.flow.component.html.H2)3 VerticalLayout (com.vaadin.flow.component.orderedlayout.VerticalLayout)3 ComponentRenderer (com.vaadin.flow.data.renderer.ComponentRenderer)3 TemplateRenderer (com.vaadin.flow.data.renderer.TemplateRenderer)3 ElementConstants (com.vaadin.flow.dom.ElementConstants)3 Route (com.vaadin.flow.router.Route)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Faker (com.github.javafaker.Faker)2 Label (com.vaadin.flow.component.html.Label)2 Collectors (java.util.stream.Collectors)2 IntStream (java.util.stream.IntStream)2 Component (com.vaadin.flow.component.Component)1 Button (com.vaadin.flow.component.button.Button)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