【问题标题】:How to add ol.format.filter dynamically in js?如何在js中动态添加ol.format.filter?
【发布时间】:2019-07-22 04:19:32
【问题描述】:

我正在开发一个使用 Openlayers 和 geoserver 的项目,我的目标是放大到我分配给用户的国家/地区。目前我通过添加像ol.format.filter.equalTo('country','India')这样的过滤器来实现它,但问题是如果我分配超过1个国家,我必须更改代码以便会有多个ol.format.filter.equalTo,我想动态添加。

我搜索了以前的答案,但没有一个解决这个问题。

【问题讨论】:

  • 你有代码示例吗?

标签: openlayers-3 geoserver openlayers-5


【解决方案1】:

这是我的示例代码。请看一下。

             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);
                }
            }

【讨论】:

    猜你喜欢
    • 2018-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    • 2016-02-18
    相关资源
    最近更新 更多