Ad
How Do You Setup A Grid In Vaadin 8 To Sort By A Custom Comparator?
So for example my grid is composed of People
so that I have:
public class People
{
int id;
String name;
}
My grid is such that I have:
grid.addColumn(People::getName).setId("Name");
Now I know I can do:
grid.sort("Name", SortDirection.ASCENDING);
The problem is what if I have two people with the same name, say John. In this case I want to first sort by name
and then by id
. The id is not a column in the grid, it's just a property of the People
class.
Although this example is oversimplified I do include additional information so it's important that I'm able to sort by the actual person and not just the name. In my case the grid is a report and the person's name is just one of many fields.
Ad
Answer
The way I ended up doing it, assuming you have a list of Cars with People being one of the columns, is:
Grid<Car> grid = new Grid<Car>();
grid.addColumn(Car::getPerson).setComparator((car1, car2) ->
{
// ignoring any null checks for getPerson() as they can be null.
return car1.getPerson().compareTo(car2.getPerson());
});
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad