featurelayer - how to remove filterexpression

5053
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
KellyHutchins
Esri Frequent Contributor
Setting the definition expression to an empty string works for me. Here's a fiddle that shows this:

http://jsfiddle.net/RbzVj/
0 Kudos
TracySchloss
Frequent Contributor
The fiddle doesn't work for me.  It just stays on South Carolina, no matter which button I push. 

I changed my code to switch my filter expression to
 
buildingLayer.setDefinitionExpression(" ");


Instead of showing all features, it shows none at all.
0 Kudos
TracySchloss
Frequent Contributor
I just realized how old the version of the JS API was that I'm using (3.2).  I updated my references and it worked, just one time.  When I reloaded the page, now it's back to removing all the features.  Very wierd!
0 Kudos
KellyHutchins
Esri Frequent Contributor
Sorry the fiddle had a browser specific bit of code in it so it was failing in many browsers. This one should work:

http://jsfiddle.net/2HsMr/


In your app you said it works the first time and then fails. Can you check the requests in the net tab and see if the definition expression is set correctly?  For example if I run the code I provided in the fiddle with debugging tools open I can see the query request in the Net tab. If I look at the query params I can see that a where clause with the value of "STATE_NAME = 'North Carolina'" is specified.
0 Kudos
KenBuja
MVP Esteemed Contributor
The fiddle doesn't work for me.  It just stays on South Carolina, no matter which button I push. 

I changed my code to switch my filter expression to
 
buildingLayer.setDefinitionExpression(" ");


Instead of showing all features, it shows none at all.


Try using this instead

buildingLayer.setDefinitionExpression("");
0 Kudos
TracySchloss
Frequent Contributor
I've tried both "" and " ".  The space, or lack of one hasn't made any difference. 

If I look at the layer, it's defaultDefinitionExpression is  defined as "".  I originally had code that told it to set the definitionExpression back to this default.
buildingLayer.setDefinitionExpression(buildingLayer.defaultDefinitionExpression);

  That seems logical to me, but that didn't work either.

Putting breakpoints in my code, I can see that I am setting the definitionExpression to "".  But that doesn't take me back to all features, I'm still seeing none.
0 Kudos
KellyHutchins
Esri Frequent Contributor
Tracy,

Is your app public? If not can you send your code? I think the easiest way to debug this is to look at the query requests to see what is being used as the where clause value for the query request. I've attached a screen capture that shows an example of this.
0 Kudos
TracySchloss
Frequent Contributor
Unfortunately no, it's for an internal project with rather sensitive data.  I'm also trying to redo the whole thing to be AMD style instead, but that's not going very well either! 

I'll see if I can put something together, but I'm sure as soon as I lift out a portion of the code to show as an example, it will all work!
0 Kudos
TracySchloss
Frequent Contributor
I made a copy this AM and left only the relevant code to my situation.  One thing I should mention is that I am using IdentityManager, since my service is secure.  When I hit my Clear Results, in my Firebug console log, I'm getting messages, first a I see several that show that my Get request, I assume on my featurelayer, was aborted.  These are followed for a corresponding number of  CancelError: Request canceled. messages.

I get these every time I attempt to set the filter expression to "".  I can continue to use my dropdrown that sets the filter to a regular expression, it's only the filterexpression of "" that is generating these errors.

Could there be some bug related to feature layers, filterexpressions, identitymanager, security etc.?
0 Kudos