Solved! Go to Solution.
protected function InvertSelBtn_clickHandler(event:MouseEvent):void{
//reset all three arrays
features = [];
selected = [];
notSelected = [];
this.cursorManager.setBusyCursor();
//add features that are currently selected to array selected, then clear selection
var i:Number = new Number;
for(i=0;i<targetLayer.selectedFeatures.length;i++){
var selFeature:Graphic = targetLayer.selectedFeatures;
selected.push(selFeature);
}
targetLayer.clearSelection();
//this selects all features
var query:Query = new Query;
query.where = "1=1";
//this.cursorManager.setBusyCursor();
targetLayer.selectFeatures(query,"new",new AsyncResponder(onInvertSelectAllResult, onInvertSelectAllFault));
}
//if select all during invert fails
private function onInvertSelectAllFault(info:Array, token:FeatureLayer = null):void{
Alert.show("Error During Invert Selection.");
}
//if select all during invert is successful - continue with invert process
private function onInvertSelectAllResult(info:Array, token:FeatureLayer = null):void{
var marker:Boolean = false;
//upon result of selecting all features, add all features from targetLayer to array features, then clear selection
var i:Number = new Number;
for(i=0;i<targetLayer.selectedFeatures.length;i++){
var feature:Graphic = targetLayer.selectedFeatures;
features.push(feature);
}
targetLayer.clearSelection();
//nested for loop to iterate through features (run once for each feature in targetLayer)
//then iterate through selected (run once for each feature in selFeatures)
//if their objects match, marker set to true - if at the end of the inside loop marker is still false
//no match was found and the feature is loaded into notSelected
var x1:Number = new Number;
var y1:Number = new Number;
for(x1=0;x1<features.length;x1++){
marker=false;
for(y1=0;y1<selected.length;y1++){
//if there is ever a match, mark boolean as true, and features wont be added to notSelected
//i will have to change this to work with line, poly -for now points is fine
if(features[x1].geometry.toString() == selected[y1].geometry.toString()){
//this lets us know that there was a match and nothing should be added to notSelected
marker=true;
}
}
//if match wasnt found in inside for loop, then the feature wasnt selected and will be added to notSelected
if(marker===false){
notSelected.push(features[x1]);
}
}
//pass the array of unselected features into featSet, then pass to unionGeom to get a geometry back
var featSet:FeatureSet = new FeatureSet(notSelected);
var unionGeom:Geometry = SelectionWidgetUtil.unionGeoms(featSet);
//now run query with unionGeom
var query1:Query = new Query;
query1.geometry=unionGeom;
targetLayer.selectFeatures(query1,"new",new AsyncResponder(onInvertResult, onInvertFault, targetLayer));
}
//on invert selection result
private function onInvertResult(info:Array, token:FeatureLayer = null):void
{
this.cursorManager.removeBusyCursor();
//this makes the selection results page be displayed upon selection
/* var e:Event = new SelectionFeatureLayerEvent(SelectionFeatureLayerEvent.SHOW_RESULTS, null);
dispatchEvent(e); */
}
//on invert selection fault
private function onInvertFault(info:Object, token:Object = null):void
{
this.cursorManager.removeBusyCursor();
Alert.show("Could not complete Selection. Please try again.\n" + info.toString(),
"Selection Error", Alert.OK);
}
var query:Query = new Query;
query.where = "NOT " targetLayer.layerDetails.objectIdField + " IN (" + OIDArr.join(",") + ")";
targetLayer.selectFeatures(query,"new",new AsyncResponder(onInvertSelectAllResult, onInvertSelectAllFault)); Where OIDArr is the array containing the objectids of the current selection.var OIDArr:Array = new Array;
for(anX=0;anX<targetLayer.selectedFeatures.length;anX++){
Alert.show(targetLayer.selectedFeatures[anX].id)
} var OIDArr:Array = new Array; for(anX=0;anX<targetLayer.selectedFeatures.length;anX++){ Alert.show(targetLayer.selectedFeatures[anX].attributes[targetLayer.layerDetails.objectIdField]) }