I recently published some great helpers for connecting large amount of data to UI components. They provide you simple tools to make a well performing databinding with Vaadin UI components and a service layer implemented with e.g. Spring Data or a stateless EJB.
Since the first “release” I have played a bit more with them, not least due to the XRebel introduction we held with Simon Maple from ZeroTurnaround a week ago. I still like the concept a lot and wanted to make some improvements to it. Now that I’m bit sick and at the home office, it was a nice opportunity for some additional improvements and a new release. Solutions introduced below are available since the Viritin version 1.26.
Filtering is most often needed when you want to have a select component at the UI that users can filter efficiently with some text input. ComboBox is the Vaadin component of choice for this, but as you might know, making a really well performing data binding for it may be a bit tricky.
The solution in Viritin is now called LazyComboBox. It uses customized version of the LazyList and, in addition to “paging”, you can use it to delegate sorting to your backend as well. You can find a usage example from my jpa-invoicer example. It is bit hairy as it also contains support for adding new items and it filters values from another select visible in the UI as well, but you should get a basic idea from these lines.
Sorting is another thing that is in Vaadin delegated to its Container interface. I’m not quite sure if I’m a big fan of that decision, but now you can overcome this using MTable. The initial version actually allowed sorting for LazyList backed UI components, even though that is something very stupid thing to do and was most probably broken in many multiple ways. If you are using the simple LazyList, sorting is now correctly disabled by default for all properties.
But that don’t excactly help if your requirements says you need sorting :-) You can now implement a totally customized sorting logic by using SortListener, that you can use to bypass the default “container sorting” done by Vaadin. You can also explicitly define the properties the UI should allow sorting on using setSortableProperties method.
However, instead of the above customization, I’d suggest to look at SortableLazyList, or the related constructors in MTable. This LazyList extension, supported natively by the MTable, also passes information about sort direction and property to your “backend binding”. With Java 8 lambdas, the well performing sortable and pageable data binding now looks like this:
// entity fetching strategy
(firstRow, asc, sortProperty) -> repo.findAll(
firstRow / PAGESIZE,
asc ? Sort.Direction.ASC : Sort.Direction.DESC,
// fall back to id as "natural order"
sortProperty == null ? "id" : sortProperty
// count fetching strategy
() -> (int) repo.count(),
The above example is taken from a Spring Data Vaadin CRUD example and only uses the default methods generously provided by the Spring Data JpaRepository. BTW. If you don’t know about Spring Data, and even if not needing a well performing sortable lazy binding to your data today, check it out and play with the example. In case you didn’t know it, Spring Data is a super cool beast, sincerely. I miss it every time I work on a Java EE project. And naturally, it works perfectly with Vaadin ;-)
Improvement ideas, WELCOME
Check out the latest version of Viritin from the Directory and let me know what you think! If you have improvement ideas, feel free to post the to the github project, issues and pull requests are both more than welcome.