Filtering

Filtering can be implemented by adding UI components such as a TextField and a ComoboBox with value listeners on them. When the user changes the filtering components, the value listeners update the data by passing their values to the backend and updating the view accordingly. For example, in order to filter by last name, the UserRepository.findAll method should accept a string with the value to match:

public class UserRepository {

public static List<User> findAll(String lastName) {
return JPAService.runInTransaction(em ->
em.createQuery("select u from User u where u.lastName like :lastName")
.setParameter("lastName", lastName)
.getResultList()
);
}
...
}
Always keep in mind that findAll methods are useful and safe to use when they return a small number of results. When this is not the case, you should add lazy loading capabilities like the ones discussed in Chapter 9, Lazy Loading.

Assuming there is a lastNameFilter input component (of type TextField, for example), the Grid should be populated using the new method, and passing the value in the filter:

grid.setItems(UserRepository.findAll(lastNameFilter.getValue()));
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset