Im trying to use Grid Component. I need to define the order of a column, I'm using this proyect: https://github.com/vaadin/tutorial/tree/v8-step4
And I add this code:
Column name = grid.addColumn(customer -> customer.getFirstName() + " " + customer.getLastName())
.setCaption("Name")
.setSortOrderProvider(direction -> Stream.of(
new QuerySortOrder("lastName", direction)
));
grid.setSortOrder(GridSortOrder.asc(name));
But I'm not getting the expected results, I'm getting ordered by firstName and then by lastName but i need the results ordered by lastName.
Have you had the same problem? How have you solved it?
Thank you.
I digged into the code and found out that you need you need to call setComparator
instead of the setSortOrderProvider
. The former is intended for in-memory data providers. Unfortunately, it's a little bit confusing and not really well documented.
I use this implementation of setComparator and it's working. : )
Column name = grid.addColumn(customer -> customer.getFirstName() + " " + customer.getLastName())
.setCaption("Name")
.setComparator(new SerializableComparator<Customer>() {
@Override
public int compare(Customer arg0, Customer arg1) {
return arg0.getLastName().compareTo(arg1.getLastName());
}
});
With Lambda:
.setComparator((customer0, customer1) -> {
return customer0.getLastName().compareTo(customer1.getLastName());
});
and this other option:
Column name = grid.addColumn(customer -> customer.getFirstName() + " " + customer.getLastName())
.setCaption("Name")
.setComparator(grid.getColumn("lastName").getComparator(SortDirection.ASCENDING));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With