featurelayer - how to remove filterexpression

5107
13
09-09-2013 10:41 AM
TracySchloss
Frequent Contributor
I have a dropdown list of choices that I'm using as a parameter for a filterExpression on a featureLayer.  This works just fine, until I tried to remove the filter.  Looking through the API,  I'm not seeing anything that seems suitable to get rid of this.  I thought I could just set it back to the original default filterExpression (which returns as  "" if I look at the layer before I set the filter).  I've also tried using clearSelection, which doesn't seem like what I want, since I didn't do a selection of features, I set a filter.

function filterFacilityByAgency() {
    var e = dijit.byId("facilitySelect");
    var code = e.value;
    map.graphics.clear();
    map.infoWindow.hide(); 
    var filterExpression = "Agency = '" + code + "'";
    buildingLayer.setDefinitionExpression(filterExpression);
    buildingLayer.setVisibility(true);
}
function clearBuildingLayer () {
  var orgExp = map.getLayer('buildingLayer').defaultDefinitionExpression;  //NOTE:  returns ""
buildingLayer.setDefinitionExpression(orgExp);
map.getLayer('buildingLayer').clearSelection;
    var select = dijit.byId("facilitySelect");
    select.reset();
}
0 Kudos
13 Replies
TracySchloss
Frequent Contributor
I'm noticing different behavior depending on the browser.  In Chrome and IE, if I hit the button a 2nd time, then all the features display.  In Firefox, hitting the clear button a 2nd time makes no difference.
0 Kudos
KellyHutchins
Esri Frequent Contributor
I'm not aware of any issues with the definition expression when working with secure layers. You mentioned that if you press the button a second time it works in some browser but not in others. Can you see the query request happening for each button click? Have you debugged through the click event code to ensure it is running successfully? Try setting your definition expression to this instead of the empty quotes and see if that makes a difference

states.setDefinitionExpression("1=1");
0 Kudos
VinayBansal
Occasional Contributor II
Try dirtying the query....
[ NIM086349 - Alternate Query (Get) requests fail in Version 10.1 SP1 ]

var dirty = (new Date()).getTime();

buildingLayer.setDefinitionExpression(dirty + "=" + dirty)
0 Kudos
TracySchloss
Frequent Contributor
I'm thinking that 'dirty = dirty' means the same thing as '1=1'  Neither worked.

I made a sample that isn't using any secure data, which has eliminated that as the culprit, it still doesn't work for me.  As far as seeing whether or not the query is OK on the click, I think the errors I'm seeing in the console are telling me that 'no' the query isn't executing properly, either with "", " ", or some other expression that resolves as true, like 1=1.


<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=7, IE=9, IE=10">
    <title>Feature Layer Filter Expression Example</title>
    <link rel="stylesheet" type="text/css" href="https://serverapi.arcgisonline.com/jsapi/arcgis/3.2/js/dojo/dijit/themes/claro/claro.css">
    <link rel="stylesheet" type="text/css" href="https://serverapi.arcgisonline.com/jsapi/arcgis/3.2/js/esri/css/esri.css" />
    <link rel="stylesheet" type="text/css" href="https://serverapi.arcgisonline.com/jsapi/arcgis/3.2/js/esri/dijit/css/Popup.css">

    <link rel="stylesheet" type="text/css" href="css/style.css">
        <script type="text/javascript">
            var dojoConfig = {
                parseOnLoad: true
            };
        </script>
    <script type="text/javascript" src="https://serverapi.arcgisonline.com/jsapi/arcgis/?v=3.2"></script>

    <script type="text/javascript">
      dojo.require("dijit.layout.BorderContainer");
      dojo.require("dijit.layout.ContentPane");
      dojo.require("esri.map");
      dojo.require("esri.dijit.Popup");
      dojo.require("esri.layers.FeatureLayer");
      dojo.require("dijit.form.Select");
      dojo.require("esri.IdentityManager"); 

      var map;
      var startExtent;
      var buildingLayer;
      var buildingInfoTemplate;
      var infoTemplateContent;
    var pathName = "https://ogitest.oa.mo.gov";
      var buildingAgencyList = [];

 function init(){
         esri.config.defaults.io.proxyUrl = pathName + "/proxy/proxy.ashx"; 
    var highlightFillSymbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255, 255, 0]), 3), new dojo.Color([255, 255, 0, 0.05]));
    var highlightMarkerSymbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE, 22, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255, 255, 0]), 2), new dojo.Color([255, 255, 0, 0.5]));
    var popup = new esri.dijit.Popup({
        fillSymbol: highlightFillSymbol,
        lineSymbol: false,
        markerSymbol: highlightMarkerSymbol
    }, dojo.create("div"));
    
    var spatialReference = new esri.SpatialReference({
        wkid: 102100
    });
    startExtent = new esri.geometry.Extent(-10583000, 4287025, -9979000, 4980462, spatialReference);
    
    map = new esri.Map("mapDiv", {
        extent: startExtent,
        fitExtent: true,
        infoWindow: popup,
        basemap: "gray"
    });
        
    map.infoWindow.resize(275, 600);

    var countyLayer = new esri.layers.ArcGISDynamicMapServiceLayer(pathName + "/ArcGIS/rest/services/BaseMap/county_simple/MapServer", {
        id: "countyLayer"
    });
 map.addLayer(countyLayer);
    
    buildingInfoTemplateContent = "<b>${Facility}</b><br>" +
    "Address: ${Address}<br>" +
    "City: ${City}<br>" +
    "Type: ${Type}<br>" +
    "Years: ${Years}" ;
    
    buildingInfoTemplate = new esri.InfoTemplate();
    buildingInfoTemplate.setTitle("${Facility}");
    buildingInfoTemplate.setContent(buildingInfoTemplateContent);
    
    buildingLayer = new esri.layers.FeatureLayer(pathName + "/ArcGIS/rest/services/BaseMap/Education/MapServer/2", {
        mode: esri.layers.FeatureLayer.MODE_ONDEMAND,
        id: "buildingLayer",
        outFields: ["*"], 
        infoTemplate: buildingInfoTemplate
    });

    
   var buildingSymbol = new esri.symbol.PictureMarkerSymbol('images/orangeDiamond.png', 24, 24);
   var buildingRenderer = new esri.renderer.SimpleRenderer(buildingSymbol);
   buildingLayer.setRenderer(buildingRenderer);
    
   map.addLayer(buildingLayer);
   populateBuildingDropDown(buildingLayer);
}
      
function pointToExtent(map, point, toleranceInPixel) { 
var pixelWidth = map.extent.getWidth() / map.width; 
var toleraceInMapCoords = toleranceInPixel * pixelWidth; 
return new esri.geometry.Extent( point.x - toleraceInMapCoords, 
    point.y - toleraceInMapCoords, 
    point.x + toleraceInMapCoords, 
    point.y + toleraceInMapCoords, 
    map.spatialReference );                            
} 

function populateBuildingDropDown(buildingLayer){  
    buildingAgencyList.length = 0;
    var queryTask = new esri.tasks.QueryTask(buildingLayer.url);
    var query = new esri.tasks.Query();
    query.outFields = ["*"];
    query.where = "1=1";
    query.returnGeometry = false;

  //populates the pick list searching state facilities by agency 
    dojo.connect(queryTask, "onComplete", function (featureSet){
        var select = dijit.byId("facilitySelect");
        dojo.forEach(featureSet.features, function (feature){
           var value= feature.attributes.Type;
           buildingAgencyList.push(value);
        });
        buildingAgencyList.sort();
        var sortedList = sortAndRemoveDuplicates(buildingAgencyList);
        for (var i = 0; i < sortedList.length; i++) {
            var bAgencyName = sortedList;
            select.addOption({
             label: bAgencyName,
             value: bAgencyName
           });
        }
    });
 queryTask.execute(query);
} 

function sortAndRemoveDuplicates(arr) {
    var copy = arr.slice(0);
    arr.length = 0;
    for (var i = 0, len = copy.length; i < len; ++i) {
        if (i === 0 || copy != copy[i - 1]) {
            arr.push(copy);
        }
    }
    return arr;
}

function filterFacilityByType() {
    var e = dijit.byId("facilitySelect");
    var code = e.value;
    map.graphics.clear();
    map.infoWindow.hide(); 
    var filterExpression = "Type = '" + code + "'";
    buildingLayer.setDefinitionExpression(filterExpression);
    buildingLayer.setVisibility(true);
}
  
function toggleLayer (layerId) {
var layer = map.getLayer(layerId);
    if (layer.visible){
        layer.setVisibility(false);
        map.infoWindow.hide();
    }else{
        layer.setVisibility(true);
    }
}

function clearBuildingLayer () {
  var exp = "";
  var exp = "1=1";
 var dirty = (new Date()).getTime();

buildingLayer.setDefinitionExpression(dirty + "=" + dirty);
//buildingLayer.setDefinitionExpression(exp);
buildingLayer.refresh();
    var select = dijit.byId("facilitySelect");
    select.reset();
    map.graphics.clear();
    map.infoWindow.hide();
}
  dojo.addOnLoad(init);
    </script>
  </head>

<body class="claro">
  <div dojotype="dijit.layout.BorderContainer" design="headline" gutters="false"
    style="width: 100%; height: 100%; margin: 0;">
   <div id="mainHeader" dojotype="dijit.layout.ContentPane" region="top" > 
Filter Expression Example              
</div>
      <div id="mapDiv" dojotype="dijit.layout.ContentPane" region="center" style="overflow:hidden;"splitter="true">
      </div>
      <div id="leftPane" data-dojo-type="dijit.layout.ContentPane" data-dojo-props="region:'left'"splitter="true" > 
        <button dojoType="dijit.form.Button" id="btnBuilding" class="togglebutton" iconClass="buildingIcon" onClick="toggleLayer('buildingLayer');" title="Turn State Facilities On/Off">
           Facility
         </button>

</br>                 
        Filter facilities by type: 
       <select data-dojo-type="dijit.form.Select" id="facilitySelect" onchange="filterFacilityByType()" title="Display homes by level of care">
         <option>Select type</option>
     </select>
     <button id="btnClear" dojotype="dijit.form.Button" onClick="clearBuildingLayer();" title="Clear Filter by Agency">Clear Results</button>         
    </div>
  </div>
  </body>

</html>
0 Kudos