Vaadin:过滤器的菊花链?

问题描述 投票:1回答:1

我现在有这个特殊的问题-我有一个网格,试图通过多个过滤器过滤数据。为此,我正在使用文本框作为我的过滤条件的输入字段。

我的网格有三列(名字,姓氏,地址),我希望能够一个接一个地链接过滤操作。所有值均取自MySQL数据库。

基本上,过滤过程应如下所示:FirstName ^ LastName ^ Address

例如,具有三列的网格:

enter image description here

table

并且在“名字过滤器”中,输入变量Aa,这将导致表如下所示:

enter image description here

table1

但是,如果我确定将D输入到“姓氏”过滤器中,它将返回这样的结果(忽略第一个过滤器的修改):

enter image description heretable3

而不是看起来像这样的预期结果:

enter image description hereenter image description here

我通过网格过滤的方式是这样的:

firstNameFilter.addValueChangeListener( e->
    {
        Notification.show(e.getValue());

        ldp.setFilter(desc -> 
        {
            return StringUtils.containsIgnoreCase(desc.getFName(), firstNameFilter.getValue());
        });
    });     
    firstNameFilter.setValueChangeMode(ValueChangeMode.EAGER);

在考虑先前的过滤操作的同时,通过多个列进行过滤的最佳方法是什么?

java vaadin vaadin-grid vaadin10
1个回答
1
投票

listDataProvider.setFilter(...)将覆盖任何现有过滤器。您应该定义一个valueChangeListener,将用于all过滤器字段。在该valueChangeListener中,您可以设置一个新的过滤器该帐户占所有过滤器值

[我写了an answer about this very topic,其中有完整的示例代码可供复制粘贴,并且屏幕截图显示多个过滤器可以按预期工作。

这是您的代码的外观:

firstNameFilter.addValueChangeListener( e-> this.onFilterChange());
lastNameFilter.addValueChangeListener( e-> this.onFilterChange());
addressFilter.addValueChangeListener( e-> this.onFilterChange());


private void onFilterChange(){
    ldp.setFilter(desc -> {
        boolean fNameMatch = true;
        boolean lNameMatch = true;
        boolean addressMatch = true;

        if(!firstNameFilter.isEmpty()){
            fNameMatch = StringUtils.containsIgnoreCase(desc.getFName(), firstNameFilter.getValue());
        }
        if(!lastNameFilter.isEmpty()){
            lNameMatch = StringUtils.containsIgnoreCase(desc.getLName(), lastNameFilter.getValue());
        }
        if(!addressFilter.isEmpty()){
            addressMatch = StringUtils.containsIgnoreCase(desc.getAddress(), addressFilter.getValue());
        }

        return fNameMatch && lNameMatch && addressMatch;
    });
});     
© www.soinside.com 2019 - 2024. All rights reserved.