use of com.vaadin.flow.internal.JsonSerializer 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;
}
Aggregations