Hakan Akkurt
Hakan Akkurt

Reputation: 31

Vaadin Grid sortable date column

i want to add a sortable date column to my vaadin grid component. Unfortunately it doesn't work with formatted dates, I think after formatting the date object it is just a String and therefore sorting with this column doesn't work correctly, but I need anyway a solution for that. I already have tried this solution, which also doesn't work:

    grid.addColumn(new LocalDateTimeRenderer<>(MyObject::getCreated,
                DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.MEDIUM)
                        .withLocale(Locale.GERMANY).withZone(ZoneId.of("Europe/Paris"))))
                            .setHeader("Created").setSortProperty("created");

Do you have an another idea?

Thanks

Upvotes: 3

Views: 964

Answers (1)

Simon Martinelli
Simon Martinelli

Reputation: 36103

You can set a comparator

grid.addColumn(new LocalDateTimeRenderer<>(MyObject::getCreated,
                DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.MEDIUM)
                           .withLocale(Locale.GERMANY).withZone(ZoneId.of("Europe/Paris"))))
     .setHeader("Created")
     .setSortProperty("created")
     .setComparator(MyObject::getCreated);

Please also check the documentation: https://vaadin.com/docs/latest/ds/components/grid#sorting

Upvotes: 6

Related Questions