Grid Date Column

Hi All,

Is it possible to set a default renderer for Date/LocalDate/LocalDateTime etc for a grid?

Or will I have to set ValueProviders or ComponentRenderers for all my Date columns??

Thanks,

Stuart.

Hi!

Flow does have LocalDateRenderer and LocalDateTimeRenderer. Here’s an example of how to use them with Grid: https://vaadin.com/components/vaadin-grid/java-examples/formatting-contents

Hi Pekka,

I was hoping for a global setting for the Grid :slight_smile: I’m adding columns based on a list of visible columns…

PropertySet<T> propertySet = BeanPropertySet.get(clazz);
for (String column : settings.getVisiblecolumns().split(" ")) {
		Class<?> type = propertySet.getProperty(column).get().getType();
		if (type == LocalDate.class) {
			grid.addColumn(column, new LocalDateTimeRenderer<>(item::getDate, DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.MEDIUM))));  <-- Obviously not working.
		} else {
			grid.addColumn(column);
		}
}

So I don’t actually know which columns are Dates. So I don’t know how (if possible) to set the renderer without passing it the source method reference.

Stuart.

Ok, now I get it. Unfortunately Grid doesn’t have a feature that would allow to set a default renderer for a property type. So you have to implement it on your own like you’ve done already.

Sadly though the above code doesn’t work. I’m not sure how to create the Renderer and tell it to get the field from the grid. (Instead of using item::getDate as the field could be called anything :slight_smile: )

I started down this route…

	Column<?> c = this.addColumn(column, new  BiFunction<Renderer<T>,String, Column<T>>() {

								@Override
								public Column<T> apply(Renderer<T> t, String u) {
									// TODO Auto-generated method stub
									return new Column<T>(g, u, t);
								}
								
							});

But to be honest, I don’t know what I’m doing with this…

:slight_smile:

Stuart

You can get the PropertyDefinition like you do in your example to get the property type:

PropertyDefinition<YOUR_BEAN_TYPE, ?> propertyDefinition = propertySet.getProperty(column).get();

You can get the property value corresponding to this definition from an object like this:

propertyDefinition.getGetter().apply(item)

So to bring it all together, something like this should work:

PropertyDefinition<YOUR_BEAN_TYPE, ?> propertyDefinition = propertySet.getProperty(column).get();
Class<?> type = propertyDefinition.getType();
if (type == LocalDate.class) {
    grid.addColumn(new LocalDateTimeRenderer<>(item -> (LocalDateTime) propertyDefinition.getGetter().apply(item),
        DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.MEDIUM)));
}

Let me know if this does the trick or not. :slight_smile:

Hi Pekka,

I finally got around to implementing your suggestion :slight_smile: And yes, it does the trick!!

Many thanks,

Stuart