openlayers-3 - 如何在js中动态添加ol.format.filter?
问题描述
我正在开发一个使用 Openlayers 和 geoserver 的项目,我的目标是放大到我分配给用户的国家。目前我通过添加像ol.format.filter.equalTo('country','India')这样的过滤器来实现它,但问题是如果我分配超过1个国家,我必须更改代码以便会有多个ol.format.filter.equalTo,我想动态添加它们。
我搜索了以前的答案,但没有一个能解决这个问题。
解决方案
这是我的示例代码。请看一下。
var conditions=new Array();
for(var i=0;i<this.selectedAttribute.length;i++) {
if(this.selectedAttribute[i]=="") {
alert("Please selected all attributes");
return;
}
if(this.selectedValue[i]=="") {
alert("Please input value");
return;
}
if(this.selectedOperator[i]=="") {
alert("Please select operators");
return;
}
var condition;
var operator=this.selectedOperator[i];
if(operator=="=") {
condition=ol.format.filter.equalTo(this.selectedAttribute[i],this.selectedValue[i]);
}
else if(operator=="!=") {
condition=ol.format.filter.equalTo(this.selectedAttribute[i],this.selectedValue[i]);
}
else if(operator=="<"){
condition=ol.format.filter.greaterThan(this.selectedAttribute[i],this.selectedValue[i]);
}
else if(operator=="<="){
condition=ol.format.filter.greaterThanOrEqualTo(this.selectedAttribute[i],this.selectedValue[i]);
}
else if(operator==">"){
condition=ol.format.filter.lessThan(this.selectedAttribute[i],this.selectedValue[i]);
}
else if(operator==">="){
condition=ol.format.filter.lessThanOrEqualTo(this.selectedAttribute[i],this.selectedValue[i]);
}
else if(operator=="LIKE"){
condition=ol.format.filter.like(this.selectedAttribute[i],this.selectedValue[i]);
}
else if(operator=="ISNULL"){
condition=ol.format.filter.isNull(this.selectedAttribute[i]);
}
conditions.push(condition);
}
if(this.selectedAttribute.length==1) {
filter=conditions[0];
}
else {
if(this.selectedCondition=="AND") {
filter=ol.format.filter.and.apply(null,conditions);
}
else if(this.selectedCondition=="OR"){
filter=ol.format.filter.or.apply(null,conditions);
}
}
推荐阅读
- javascript - 嵌入的 Twitter 链接显示在汉堡包下拉列表上方
- javascript - 如何制作中心移动堆叠图?
- pandas - 如何根据某个字符串组合是否在同一行的其他列中来更改列中的值?(熊猫)
- powershell - 将 Add-Content 与 ForEach 一起使用
- python - json 转储不包括属性名称
- regex - perl 正则表达式 - 将一行上的多个十进制数字转换为最接近的整数
- javascript - 在 GraphQL 中将参数从根传递到子查询
- javascript - document.evaluate 但对于任何节点
- c# - 如何在 C# 中截断为整数除法?
- java - 出于某种原因,getArrayList 被视为 int