Vaadin Grid 可排序日期列

Hak*_*urt 3 date vaadin columnsorting vaadin-grid vaadin-flow

我想向我的 vaadin 网格组件添加一个可排序的日期列。不幸的是,它不适用于格式化日期,我认为格式化日期对象后它只是一个字符串,因此使用此列进行排序无法正常工作,但无论如何我需要一个解决方案。我已经尝试过这个解决方案,但它也不起作用:

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

你还有别的想法吗?

谢谢

Sim*_*lli 6

您可以设置一个比较器

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);
Run Code Online (Sandbox Code Playgroud)

另请检查文档:https://vaadin.com/docs/latest/ds/components/grid#sorting