0

I'm making a map, using ArcGIS javascript API, that has a filter tool that filters some polygons by habitat type. When the user selects a habitat type, only features of that habitat type should be left on the map, but for some reason, my function is hiding all features when I select different types. I'm making this map based off of a tutorial on the ArcGIS for javascript API website. When I test the value of "value" and "newValue" they have the correct value, but it's not translating into selective hiding.

Why is my filter function not changing features properly?

Here are the functions I'm using:

  require([
    "esri/map",
    "esri/layers/FeatureLayer",
    "esri/dijit/PopupTemplate",
    "esri/dijit/Legend",
    "dojo/domReady!"
  ], function(
    Map, 
    FeatureLayer, 
    PopupTemplate, 
    Legend
  ) {

Here's is the code that creates the filter upon a change event and the function (updateDefinitionExpression) that uses a conditional statement to set the definition expression:

  var filter = document.getElementById("filter");
  // filters the layer using a definitionExpression
  // based on option selected by the user
  filter.addEventListener("change", function(event){
    var newValue = event.target.value;
    updateDefinitionExpression(newValue);
    alert(newValue);
  });
});

function updateDefinitionExpression(value){

  var definitionExpression = value ? "habitat type = '" + value + "'" : null;
  layer.setDefinitionExpression(definitionExpression);
  alert(definitionExpression);
  alert(value);
  map.infoWindow.hide();


}

And the HTML:

<body>
  <div id="viewDiv"></div>
  <div id="infoDiv">
    Filter by habitat type:
    <select id="filter">
      <option value="">All</option>
      <option value="Mangrove">Mangrove</option>
      <option value="Pelagic">Pelagic</option>
      <option value="Reefs">Reefs</option>
      <option value="Salt Marsh">Salt Marsh</option>
      <option value="Sediment">Sediment</option>
    </select>
    <div id="legendDiv"></div>
  </div>
</body>
  • can you show how do you init your feature layer ? – LMokrane Feb 12 '18 at 16:01
0

You must replace "habitat type" in your query with the name of the field from your feature layer :

Bad query:

var definitionExpression = value ? "habitat type = '"+value+"'" : null;

Better query:

var definitionExpression = value ? "habitat_type_field_name_from_feature_layer = '"+value+"'" : null;

Also, make sure the values in your select, are the same values in your feature layer

Your Answer

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

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