I am wondering why I don’t find a comparable question but anyway.
I want to create an form with multiple fields to search through an entity. It should be possible not to fill all the fields and also search over child attributes. The result should be displayed in a grid.
The main problem for me is the searching part. How can this be implemented in Vaadin 8, especially if several fields are filled but not all?
Currently, your Filter type in the DataProvider is String: AbstractBackEndDataProvider<Person, **String**>. You can also use a more complex object there, so you can put multiple fields in it. Then use the fields in the DataProvider implementation to do the actual filtering.
It’s maybe a bit dense, but the main point is that in this case Query.getFilter() doesn’t return a String (or an Optional), it returns another type of object. You can thus use that that object to do your own filtering.
This is just with two attributes, but I want to add a lot more. There must be an better solution?!
Otherwise I have to create tons of methods in the repository and check all the possible cases in the service like above.
@Query("SELECT p FROM Person p WHERE "
+"(:name is null or :name='' or p.name = :name) and "
+"(:gender is null or :gender='' or p.gender = :gender)")
List<Person> findAnyMatchingPerson(@Param("name") String name,
@Param("gender") String gender);
I suppose that this is not the best way to do it, but it works.