3

I'm using Leaflet filter on geoJson data. I'm able to apply it with a simple criteria :

filter: function(feature, layer) {
     return feature.properties.field1 == "yes";
}

I can also apply two criteria with logical operators like && or || :

filter: function(feature, layer) {
     return (feature.properties.field1 == "yes" && feature.properties.field2 == "yes");
}

I want now to insert a condition (depending of a variable):

filter: function(feature, layer) {  
   if (varState == "01") {  
     return (feature.properties.field1 == "yes" && feature.properties.field2 == "yes");
   }
   else if (varState == "02") {
     return (feature.properties.field1 == "yes" && feature.properties.field2 == "no");
    }
    else {
      return feature.properties.field1 == "yes";            
    }
}

Does someone has already succeed this ?

  • what about defining two different layers for each filter. For example 'var field1 = L.Geojson... filter: function() { field1 }' 'var field2 = L.Geojson... filter: function() { field2 }' – Shahzad Bacha Apr 29 '16 at 10:35
  • Ok, and how can I use this two filters in a single layer ? (markers with feature.properties.field1 == "yes" && feature.properties.field2 == "yes"; – GeoGyro Apr 29 '16 at 12:19
  • 1
    I can see no reason why this wouldn't work. Here's a quick test filtering some random data with multiple properties: fiddle.jshell.net/nathansnider/am3nwqn8 Have you made sure that there are actually features that have "yes" for both field and field2? – nathansnider Apr 29 '16 at 17:53
  • Yes, features have "yes" for both field1 and field2 – GeoGyro May 2 '16 at 7:29
  • It works. I can't see it because I make a if else if else not recognize by filters. – GeoGyro May 4 '16 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.