neo4jcomboboxvaadinspring-data-neo4jvaadin22

Vaadin 22 ComboBox with Lazy Loading and Filtering


I have a tagging system in my application. Now there is one specific entity, for which I want to allow only one tag. Actually I want to assign a parent to a tag. For this purpose I want to use a VaadinCombobox with lazy loading and filtering.

My data layer is Spring Boot Neo4J Data. There I have a repository like this:

Page<TagListDto> searchPaginated(String searchTerm, Pageable page);

This gives me a data transfer object used for list displays filtered by the searchTerm. The list is pageable. I use the same method for filtering Grids.

So I could do it like this, if I knew where to get the searchTerm from.

ComboBoxLazyDataView<TagListDto> dataView = parentTag.setItems(query ->
            tagRepository.searchPaginated(searchTerm,
                       PageRequest.of(query.getPage(), query.getLimit())).stream());
parentTag.setItemLabelGenerator(TagListDto::getName);

But probably, I'll have to use a DataProvider and a FilterBuilder for the ComboBox, right?


Solution

  • So the answer is to use setItemsWithFilterConverter-method. What you enter into the combo-box field will be sent to the filter converter (2nd method parameter) as a String and then passed as property of the query object to execute the search (1st method parameter).

    So if you need to convert the type of the search term from String to some other type, add wildcards or whatever, do it in the 2nd labda. With query.getFilter() in the 1st lambda you can retrieve the search term and then use that to make the query to your backend.

    Here's an example:

    ComboBox<Person> personComboBox = new ComboBox<>();
    personComboBox.setItemLabelGenerator(Person::getName);
    personComboBox.setItemsWithFilterConverter(
                    query -> personService.searchPaginated(query.getFilter().orElse(""),
                                                           PageRequest.of(query.getPage(),
                                                           query.getLimit())).stream(),
                    personSearchTerm -> personSearchTerm
            );