vaadin grid sort bigDecimal column

Viewed 76

I've add a column for a BigDecimal property into the grid and expect that sorting for such column works out-of-the-box:

grid.addColumn(new NumberRenderer<>(dto -> dto.getTotalPrice(), NumberFormat.getCurrencyInstance()))
    .setAutoWidth(true)
    .setFlexGrow(0)
    .setHeader("Summe")
    .setSortProperty("totalPrice") 
    .setSortable(true);

But this doesn't work and I've to provide the comparator

.setComparator((val1, val2) -> val1.getTotalPrice().compareTo(val2.getTotalPrice()))

Maybe similar issue like this one

Addition: If I go without the NumberRenderer and add the column the normal way, then setSortedProperty works as expected:

grid.addColumn(OrderListDto::getTotalPrice, "totalPrice")
    .setSortProperty("totalPrice")
1 Answers

If you don't define a renderer, then the value provider that you define for the column will also be used for comparing in-memory values if sorting is enabled for the column. When you use a renderer, then the column doesn't have direct access to the value provider that you give to the renderer, so you instead need to define it manually.

The value that you pass to setSortProperty is only used by backend data providers, but not for in-memory data. It may still give the impression of working since setSortProperty also triggers setSortable(true) which will work when no renderer is used based on the value provider. The value that you pass to setSortProperty does not have any effect with in-memory data, so you could do setSortProperty("foo") and the result would be the same.

Related