Luc*_*aes 3 spring spring-data spring-data-jpa spring-boot vaadin8
你好!我正在尝试在运行时为数据网格构建自定义过滤器。我正在使用 spring boot 和 vaadin 8 进行数据展示。Vaadin 知识与此问题无关。
我在做什么:我为过滤器构建了一个哈希图。
private HashMap<String, Specification<ARInteraction>> interactionSpecifications =
new HashMap<>();
Run Code Online (Sandbox Code Playgroud)
每个过滤器文本字段都会向地图添加或删除规范:
TextField filterOwner = new TextField("Filter");
filterOwner.addValueChangeListener(nv -> {
if (StringUtils.isNotEmpty(nv.getValue())) {
interactionSpecifications.put("owner", ARInteractionSpecifications
.withOwnerEmail(nv.getValue()));
} else {
interactionSpecifications.remove("owner");
}
refreshContent();
});
Run Code Online (Sandbox Code Playgroud)
当字段数据更改时,自定义规范会从规范映射中添加或替换(或删除)。
然后我调用刷新数据视图内容,这会导致查询获取要运行的数据。
为了构建查询数据的规范,我只需通过在它们之间应用“与”操作来添加所有规范。
private Specification<ARInteraction> buildSpecification() {
// No specs
if (interactionSpecifications.isEmpty())
return null;
// Assembles all specs together
Specification<ARInteraction> ret = null;
for (Specification<ARInteraction> spec : interactionSpecifications.values()) {
if (ret == null) {
ret = Specification.where(spec);
} else {
ret.and(spec);
}
}
return ret;
}
Run Code Online (Sandbox Code Playgroud)
我期望发生的是,应用了两个过滤器后,只会获取符合这两个规范的实体。
实际发生的情况是,如果我单独设置状态规范(此处未显示),则它可以工作,如果我设置所有者电子邮件过滤器,它也可以工作,但是如果我同时设置了两者,则显示来自该所有者的所有交互都会忽略状态过滤器。
你好吗?
规范行为是不可变的,因此您应该使用 and 操作分配 ret 规范。初学者错误。
for (Specification<ARInteraction> spec : interactionSpecifications.values()) {
if (ret == null) {
ret = Specification.where(spec);
} else {
ret = ret.and(spec); //Assign the ret so that specs are actually added.
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1097 次 |
最近记录: |