具有延迟加载和过滤功能的 Vaadin 22 ComboBox

发布于 2025-01-09 02:07:28 字数 738 浏览 9 评论 0原文

我的应用程序中有一个标记系统。现在有一个特定的实体,我只想允许为其添加一个标签。实际上我想将父级分配给标签。 为此,我想使用具有延迟加载和过滤功能的 VaadinCombobox。

我的数据层是 Spring Boot Neo4J Data。我有一个像这样的存储库:

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

这给了我一个数据传输对象,用于由 searchTerm 过滤的列表显示。该列表是可分页的。我使用相同的方法来过滤网格。

所以如果我知道从哪里获取 searchTerm,我就可以这样做。

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

但可能我必须为 ComboBox 使用 DataProviderFilterBuilder,对吧?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独难免 2025-01-16 02:07:28

所以答案是使用 setItemsWithFilterConverter 方法。您在组合框字段中输入的内容将作为 String 发送到过滤器转换器(第二个方法参数),然后作为 query 对象的属性传递以执行搜索(第一个方法参数)。

因此,如果您需要将搜索项的类型从 String 转换为其他类型,添加通配符或其他类型,请在第二个 labda 中执行此操作。
使用第一个 lambda 中的 query.getFilter(),您可以检索搜索词,然后使用它向后端进行查询。

这是一个例子:

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
        );

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
        );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文