use of com.vaadin.flow.data.renderer.Renderer in project flow-components by vaadin.
the class Grid method addColumn.
/**
* Adds a new text column to this {@link Grid} with a template renderer,
* sorting properties and column factory provided. The values inside the
* renderer are converted to JSON values by using
* {@link JsonSerializer#toJson(Object)}.
* <p>
* <em>NOTE:</em> You can add component columns easily using the
* {@link #addComponentColumn(ValueProvider)}, but using
* {@link ComponentRenderer} is not as efficient as the built in renderers
* or using {@link TemplateRenderer}.
* <p>
* This constructor attempts to automatically configure both in-memory and
* backend sorting using the given sorting properties and matching those
* with the property names used in the given renderer.
* <p>
* <strong>Note:</strong> if a property of the renderer that is used as a
* sorting property does not extend Comparable, no in-memory sorting is
* configured for it.
*
* <p>
* Every added column sends data to the client side regardless of its
* visibility state. Don't add a new column at all or use
* {@link Grid#removeColumn(Column)} to avoid sending extra data.
* </p>
*
* @see #addColumn(Renderer, String...)
* @see #removeColumn(Column)
*
* @param renderer
* the renderer used to create the grid cell structure
* @param columnFactory
* the method that creates a new column instance for this
* {@link Grid} instance.
* @param sortingProperties
* the sorting properties to use for this column
* @return the created column
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
protected <C extends Column<T>> C addColumn(Renderer<T> renderer, BiFunction<Renderer<T>, String, C> columnFactory, String... sortingProperties) {
C column = addColumn(renderer, columnFactory);
Map<String, ValueProvider<T, ?>> valueProviders = renderer.getValueProviders();
Set<String> valueProvidersKeySet = valueProviders.keySet();
List<String> matchingSortingProperties = Arrays.stream(sortingProperties).filter(valueProvidersKeySet::contains).collect(Collectors.toList());
column.setSortProperty(matchingSortingProperties.toArray(new String[matchingSortingProperties.size()]));
Comparator<T> combinedComparator = (a, b) -> 0;
Comparator nullsLastComparator = Comparator.nullsLast(Comparator.naturalOrder());
for (String sortProperty : matchingSortingProperties) {
ValueProvider<T, ?> provider = valueProviders.get(sortProperty);
combinedComparator = combinedComparator.thenComparing((a, b) -> {
Object aa = provider.apply(a);
if (!(aa instanceof Comparable)) {
return 0;
}
Object bb = provider.apply(b);
return nullsLastComparator.compare(aa, bb);
});
}
return column;
}
use of com.vaadin.flow.data.renderer.Renderer in project flow-components by vaadin.
the class IronListTestPage method createListWithComponentRenderer.
private void createListWithComponentRenderer() {
IronList<String> list = new IronList<>();
list.setHeight("100px");
List<String> items = IntStream.range(1, 101).mapToObj(i -> "Item " + i).collect(Collectors.toList());
list.setRenderer(new ComponentRenderer<>(item -> {
Label label = new Label(item);
label.addClassName("component-rendered");
return label;
}));
list.setItems(items);
list.setId("component-renderer");
add(list);
}
use of com.vaadin.flow.data.renderer.Renderer in project flow-components by vaadin.
the class VirtualListPage method createTemplateFromRendererWithPeople.
private void createTemplateFromRendererWithPeople() {
VirtualList<Person> list = new VirtualList<>();
list.setHeight("100px");
List<Person> people = createPeople(3);
DataProvider<Person, ?> dataProvider = DataProvider.ofCollection(people);
list.setDataProvider(dataProvider);
list.setRenderer(TemplateRenderer.<Person>of("[[item.name]] - [[item.age]] - [[item.user]]").withProperty("name", Person::getName).withProperty("age", Person::getAge).withProperty("user", person -> person.getName().toLowerCase().replace(" ", "_")));
NativeButton update = new NativeButton("Update item 1", evt -> {
Person item = people.get(0);
item.setName(item.getName() + " Updated");
list.getDataProvider().refreshItem(item);
});
list.setId("template-renderer-with-people");
update.setId("template-renderer-with-people-update-item");
add(list, update);
}
use of com.vaadin.flow.data.renderer.Renderer in project flow-components by vaadin.
the class VirtualListPage method createListWithComponentRenderer.
private void createListWithComponentRenderer() {
VirtualList<String> list = new VirtualList<>();
list.setHeight("100px");
List<String> items = IntStream.range(1, 101).mapToObj(i -> "Item " + i).collect(Collectors.toList());
list.setRenderer(new ComponentRenderer<>(item -> {
Div text = new Div(new Text(item));
text.addClassName("component-rendered");
text.setHeight("18px");
return text;
}));
list.setItems(items);
list.setId("component-renderer");
add(list);
}
use of com.vaadin.flow.data.renderer.Renderer in project flow-components by vaadin.
the class IronListTestPage method createListWithComponentRendererWithBeansAndPlaceholder.
private void createListWithComponentRendererWithBeansAndPlaceholder() {
IronList<Person> list = new IronList<>();
list.setHeight("100px");
List<Person> people = createPeople(100);
list.setRenderer(new ComponentRenderer<Label, Person>(person -> {
Label label = new Label(person.getName());
label.addClassName("component-rendered");
return label;
}));
list.setItems(people);
list.setId("component-renderer-with-beans");
Person placeholder = new Person();
placeholder.setName("the-placeholder");
list.setPlaceholderItem(placeholder);
add(list);
}
Aggregations