jpa容器或filter-vaadin应用程序

bis0qfac  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(310)

我有一个vaadin7应用程序和一个实体类的jpa容器。我有一个listselect控件,我用它来过滤那个容器

private ListSelect generateBrokerSelects() {
        ListSelect broker_ListSelect = new ListSelect("Broker:");
        JPAContainer<ReservationsBroker> containerBroker = JPAContainerFactory.makeReadOnly(ReservationsBroker.class, em);

        containerBroker.sort(new Object[]{"name"}, new boolean[]{true});

        broker_ListSelect.setContainerDataSource(containerBroker);
        broker_ListSelect.setMultiSelect(true);
        broker_ListSelect.setRows(4);
        broker_ListSelect.setItemCaptionPropertyId("name");

        return broker_ListSelect;
    }

我想根据这个listselect控件上选择的值过滤jpa容器。

public List<Container.Filter> getFilters() {
        List<Container.Filter> filters = new LinkedList<>();
        Container.Filter filter;

        Set brokerList = (Set) broker_ListSelect.getValue();
        if (brokerList.size() > 0) {
            for (Object brokerId : brokerList) {
                filter = new Compare.Equal("brokerId.id", brokerId);
                filters.add(filter);
            }

        return filters;
}
private void applyFilters() {
        container.removeAllContainerFilters();

        for (Container.Filter filter : getFilters()) {
            container.addContainerFilter(filter);
        }
}

上面代码的问题是,它有效地向容器添加了“and”过滤器,所以只剩下listselect控件的最后一个值。
我需要的是创建一个或筛选器,其中包含每个选定值的compare.equal

new Or(new Compare.Equal("brokerId.id", id1),new Compare.Equal("brokerId.id", id2),...)

当我迭代listselect控件的选定值时,是否有方法创建或筛选器并添加到该筛选器?

wn9m85ua

wn9m85ua1#

Or 筛选器构造函数可以将筛选器数组作为参数,即可以执行以下操作:

Filter[] filters = ...;
String[] ids = ...;
for (int i=0;i<10;i++) {
    filters[i] = new Compare.Equal("brokerId.id", ids[i]);
}
Or or  = new Or(filters);

相关问题