AnsweredAssumed Answered

Executing selections based on another query's results - managing scope

Question asked by schlot on Sep 2, 2015
Latest reply on Sep 2, 2015 by schlot

I have two layers.  I want to query staffLayer based on the a buffer of a selected feature in alertLayer.  I am looking at Select with Feature Layer | ArcGIS API for JavaScript  for an example.  The difference is I don't want a map click, I want to select the feature in alertLayer based on a row in a grid.

 

I'm getting a feature from alertLayer with my grid click.   I then need to buffer it, then use that buffer as the input geometry for a Query on staffLayer.  I am getting as far as generating a circle based on the selected row, which draws in the correct location.  However, I'm never getting any features found in that circle, even though I see there are several in the vicinity that should be returned.

 

I have changed this around several times, and I know I have a scope problem with the sequence of what I'm trying to do.  I started out with something more straight forward, but changed to more a lang.hitch type of syntax, which I'm still getting the hang of.

 

I'm getting the error Uncaught TypeError: app.staffLayer.on(...).then is not a function 

 

      updateAlertGrid: function(){
        var queryParams = new Query();
        queryParams.geometry = app.currentExtent;
        queryParams.spatialRelationship = Query.SPATIAL_REL_CONTAINS;
        queryParams.outFields = ["*"];
        queryParams.outSpatialReference = app.spatialReference;
        var queryTask = new QueryTask(app.alertLayer.url);
        queryTask.on('error', queryErrorHandler);
        queryTask.execute(queryParams, lang.hitch(this, updateGridHandler));
        
        function updateGridHandler(results){
          var data = [];
          if (app.alertGrid) {
            app.alertGrid.refresh();
          }
          data = arrayUtils.map(results.features, function(feature){
            return feature.attributes;
          });
          var currentMemory = new Memory({
            data: data,
            idProperty: 'ESRI_OID'
          });
          
          app.alertGrid.set("store", currentMemory);
          app.alertGrid.sort('FlagStatusCode');
          app.alertGrid.on('.dgrid-row:click', function(event){
            var row = app.alertGrid.row(event);
            var gridQuery = new Query();
            gridQuery.objectIds = [row.data.ESRI_OID];
            app.alertLayer.selectFeatures(gridQuery, FeatureLayer.SELECTION_NEW, function(results){
              if (results.length > 0) {
                app.alertFeature = results[0];
                app.alertFeature.setInfoTemplate(app.alertTemplate);
                //    map.centerAndZoom(results[0].geometry,14);  
                var resultGeometry = results[0].geometry;
                app.map.infoWindow.setFeatures(results);
                app.map.infoWindow.show(resultGeometry);
              }
              else {
                console.log("error in grid.on click function");
              }
            });
          });
          on(app.alertGrid, ".dgrid-cell:click", function(evt){
            var cell = app.alertGrid.cell(evt);
            var col = cell.column;
            //var row = cell.row;
            if (col.field === 'staff') {
              //   console.log("you clicked the staff cell");
              findStaffbyLocation(app.alertFeature.geometry);
            }
          });
        }
        function queryErrorHandler(err){
          console.log("error in queryTask is " + err.error);
        }
        //functions for finding staff near a server alert
        function findStaffbyLocation(geom){
          var circle = new Circle({
            center: geom,
            geodesic: true,
            radius: 50,
            radiusUnit: "esriMiles"
          });
          
          var circleSymb = new SimpleFillSymbol(SimpleFillSymbol.STYLE_NULL, new           SimpleLineSymbol(SimpleLineSymbol.STYLE_SHORTDASH, new Color([105, 105, 105]), 2), new Color([255, 255, 0, 0.25]));
          var graphic = new Graphic(circle, circleSymb);
          app.map.graphics.add(graphic);  //circle is added correctly around the selected row
          app.map.setExtent(circle.getExtent());
          var query = new Query();
          query.geometry = circle.getExtent();
          app.staffLayer.queryFeatures(query);
       
         app.staffLayer.on( 'query-features-complete', lang.hitch(this, function(response){   //there are never any features found here  
            var feature;
            var features = response.features;
            var inBuffer = [];
            
            //started with extent,now filter out features that are not actually within buffer
            for (var i = 0; i < features.length; i++) {
              feature = features[i];
              if (circle.contains(feature.geometry)) {
                inBuffer.push(feature.attributes[ESRI_OID]);
              }
            }
          })).then(function(){
            var query = new Query();
            query.objectIds = inBuffer;
            var staffList = [];
            app.staffLayer.selectFeatures(query, FeatureLayer.SELECTION_NEW, function(results){
              arrayUtils.forEach(results, function(feature){
                staffList.push(feature.attributes.Contact_FirstName + " " + feature.attributes.Contact_LastName + "</br>");
              });
              alert("Staff near selected server: " + staffList);
            });
          });
        }
      }

Outcomes