Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use setSortOrderProvider in Grid Vaadin 8?

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.

like image 268
Luis Cordn Tortulec Avatar asked Jan 08 '18 20:01

Luis Cordn Tortulec


Video Answer


2 Answers

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.

like image 92
Steffen Harbich Avatar answered Oct 07 '22 16:10

Steffen Harbich


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));
like image 40
Luis Cordn Tortulec Avatar answered Oct 07 '22 18:10

Luis Cordn Tortulec