你好!我正在尝试在运行时为数据网格构建自定义过滤器。我正在使用 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);
} …Run Code Online (Sandbox Code Playgroud)