0

I want to filter an OpenLayers(2) vector layer based on tags. Therefore, the vector layer has a property column which can be filled by comma-seperated tags like e.g. "culture,food,park", but also only one tag e.g. "food". I want to show all features who has one tag "activated" by respective jquery controlled buttons.

with only one tag per feature it is working with the following code:

tags = ['school','food','park'];

activeTags = [];
function fillArrayOfFilters() {
  activeLayers = [];
  $.each(tags, function(id, tag) {
    if ($('#' + this).hasClass('active') == true) {
      activeTags.push(tags);
    };
  });
  filters = activeLayers.map(function(tag, id) {
    return new OpenLayers.Filter.Comparison({
        type: OpenLayers.Filter.Comparison.EQUAL_TO,
        property: "tags",
        value: tag
      });
  });
}

function filter() {
  var filter = new OpenLayers.Filter.Logical({
    type: OpenLayers.Filter.Logical.OR,
    filters: filters
  });
  filterStrategy.setFilter(filter);
};

I know that there is a LIKE comparison in OpenLayers. But how can i use it in this case? I want something like that:

  new OpenLayers.Filter.Comparison({
    type: OpenLayers.Filter.Comparison.LIKE,
    property: "tags",
    value: *tag*
  });

2 Answers 2

0

Okay, found a way - quite simple. One just need to use a regular expression object as filter value:

  new OpenLayers.Filter.Comparison({
    type: OpenLayers.Filter.Comparison.LIKE,
    property: "tags",
    value: new RegExp(tag)
  });
0

Please find below Jsfiddle which surely helpfull to you...

JsFiddle

function updateFilter(station_id) {
    //Put the logical filter there  

for(var id in station_id)
{
filters[id] =
    new OpenLayers.Filter.Comparison({
        type: OpenLayers.Filter.Comparison.EQUAL,
        property: "id_station",
                value: station_id[id]
    });
}


filter = new OpenLayers.Filter.Logical({
    type: OpenLayers.Filter.Logical.AND,
      filters:[filters]  });

filterStrategy.setFilter(filter);

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.