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