0

I'm working on a project which consumes Openlayers and geoserver, My aim is to zoom to the countries I assign to user. Currently I'm achieving it by adding a filter like ol.format.filter.equalTo('country', 'India'), but the problem is if I assign more than 1 countires, I have to change code so that there will be multiple ol.format.filter.equalTo, I want to add those dynamically.

I searched for previous answers but none of them address this.

krishna lodha
  • 381
  • 3
  • 9

1 Answers1

1

Here is my sample code. please take a look.

             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);
                }
            }
jin yingmin
  • 11
  • 1
  • 2