FeatureLayer SELECTION_ADD and SELECTION_SUBTRACT Example

1435
3
12-22-2011 04:58 AM
JasonLin
New Contributor
Hi,

Can someone provide/explain a snippet that performs featurelayer adding and subtracting functionality based on user's selection? For some reason, previously-selected features will not be retained.

      
function addToMap(geometry) {
        selectionToolbar.deactivate();
        map.showZoomSlider();
  
 var selectQuery = new esri.tasks.Query();
 selectQuery.geometry = geometry;
  
 // selectMethod is a variable that holds selectionMethod constant
 featureLayer.selectFeatures(selectQuery, selectMethod);
 map.addLayer(featureLayer);
}
  

In this posting, http://forums.arcgis.com/threads/44845-How-to-add-features-using-SELECTION_ADD, Kelly did mention about using getSelectedFeatures() but I'm still not sure how everything works together.

Any help greatly appreciated!!
0 Kudos
3 Replies
KellyHutchins
Esri Frequent Contributor
Jason,

Here's a link to a sample that shows how to add and remove features from the current selection. In this example click on the map to select features then shift-click to remove features from the selection.

http://jsfiddle.net/J4sfK/



      dojo.connect(neighborhoods,'onClick',function(e){
        var query = new esri.tasks.Query();
        query.objectIds = [e.graphic.attributes.FID];
        
        //if shift key is pressed subtract from selection - otherwise add 
        if(e.shiftKey){
          neighborhoods.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_SUBTRACT);           
        }else{
          neighborhoods.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_ADD);   
        }
     
      });
0 Kudos
JasonLin
New Contributor
It does make sense to me but I'm still not sure where things went wrong in my code. Any chance you can take a look at the following?

http://jsfiddle.net/LBNXa/

Thanks so much again, Kelly.
0 Kudos
AllenHuang
New Contributor III
Hi

FeatureLayer.selectFeatures(...) is an asynchronous operation. You need to wait for the result triggering the onSelectionComplete event.
0 Kudos