[GIS] Updating filter values and update map

ext-jsgeoextjavascriptopenlayers-2

I'm developing an ExtJS3.4-GeoExt1-OpenLayers2.12 GIS application.

For basic functionality I need to filter layers to get specific data from a Geoserver WFS layer.

Filter is done through the variable myVar as a filter value as it follows:

First Filter:

customFilter = new OpenLayers.Filter.Logical({
   filters: [
       new OpenLayers.Filter.Comparison({
           type: OpenLayers.Filter.Comparison.EQUAL_TO,
              property: 'myCode',
              value: myVar
       })
    ]
 });

Then the WFS Layer:

wfsLayer = new OpenLayers.Layer.Vector("LayerName", {
      protocol: new OpenLayers.Protocol.WFS({
           url: "http://localhost:8080/geoserver/wfs",
       featureType: "LayerName",
       featureNS: "http://localhost:8080/project",
       featurePrefix: "schema",
       geometryName: "the_geom",
       outputFormat: "json",
       readFormat: new OpenLayers.Format.GeoJSON(),
       srsName: 'EPSG:25831',
       defaultFilter: customFilter
    }),
    strategies: [new OpenLayers.Strategy.Fixed(),
        myStrategy
    ]
});

And add it to the map:

map.addLayer(wfsLayer);

This works with a pre-set myVar variable before running the app. My idea is that when myVar is changed dinamicaly in my app (this is done), wfs layer updates its filter value too so map data is updated.

I've tried refreshing layer as myLayer.refresh() when myVar has been changed on my app but it still takes the initial myVar value so data in the map does not change.

Anyone knows?

Best Answer

I'm not sure about defaultFilter on protocol level. But common use case is to define filter property on layer level and change it dynamically following way:

wfsLayer.filter = /* your new filter here */;
wfsLayer.refresh({force:true});