Graphics from featureLayer - do I need to do a selection to get them?

2246
6
Jump to solution
09-24-2015 07:35 AM
TracySchloss
Frequent Contributor

I have a featureLayer loaded as

 app.bufferLayer = new FeatureLayer(config.pathName + "/arcgis/rest/services/DPS/VetComm_bufferAnalysis/FeatureServer/0" ,{
        id:"bufferLayer",
        outFields:['*']
        }
      )

I also have an onDemandGrid that shows the attributes for this layer.  When I first load the layer, I'd like to be able to populate the grid.

        on(app.bufferLayer, 'load', function(){
          myGrid.populateGrid();
        });

//function in myGrid

     populateGrid: function() {
     var graphics = app.bufferLayer.graphics;
       var gAttributes = arrayUtils.map(graphics, function(g){     
          return g.attributes;
       });
    var currentMemory = new Memory ({data:gAttributes, idProperty:'id'}); 
        app.totalsGrid.set('store', currentMemory);
     }

I'm confused about featureLayer.graphics, which is documented as as an array of graphics from the featureLayer.  When the load event executes for the bufferLayer, app.bufferLayer.graphics is an array with the length of 0.

If I add  graphics to this layer, I can execute this same function again, and this time, instead of the array length being 0, it now has all the previous graphics that were in bufferLayer, along with the new feature I added.  The grid gets populated with everything.

          on (app.bufferLayer, 'edits-complete', function (adds,updates,deletes){
            myGrid.populateGrid();
          });

I can run a query against the bufferLayer once it's loaded and use the results to populate my grid initially, but it seems like extras steps, since from the documentation is sounds like I can get all the graphics from the layer without doing all that. 

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

Instead of using the load event, use the update-end event. I'm guessing you'll want to make that a one time only event.

Take a look at this sample that shows the difference between the graphic count for the two events.

View solution in original post

6 Replies
KenBuja
MVP Esteemed Contributor

Instead of using the load event, use the update-end event. I'm guessing you'll want to make that a one time only event.

Take a look at this sample that shows the difference between the graphic count for the two events.

TracySchloss
Frequent Contributor

That did it for populating my grid, but it is now executes for every extent change.

0 Kudos
KenBuja
MVP Esteemed Contributor

The sample was updated to take that into account.

on.once(featureLayer, "update-end", function(){
    console.log("update-end: " + featureLayer.graphics.length);
});
TracySchloss
Frequent Contributor

I forgot about on.once.  I only needed it one other time.  That fixed it, thanks.

0 Kudos
TracySchloss
Frequent Contributor

On studying this some more, there's still something not quite right about this.  My grid populates, but as I add additional features to my featureLayer using applyEdits, sometimes not all the graphics are discovered.  It seems to happen when I zoomed in and I've added a new graphic to the featureLayer.  When I run the populateGrid function once I've edited to add a new graphic, instead of all the graphics, it only appears to be picking up the graphics that are at least partially in the current extent.

      on (app.bufferLayer, 'edits-complete', function (adds,updates,deletes){
        myGrid.populateGrid();
      });

the function in myGrid:

 populateGrid: function() {
    var graphics = app.bufferLayer.graphics;// graphics are not always found if zoomed in
    if (app.demographicLayer){
      app.demographicLayer.clearSelection();
    }
   
    app.cityNameList.length = 0;
    var gAttributes = arrayUtils.map(graphics, function(g){   
        return g.attributes;
    });
    arrayUtils.forEach(graphics, function (g){
      app.cityNameList.push(g.attributes.CITY);
    });
      var currentMemory = new Memory ({data:gAttributes, idProperty:'OBJECTID'});
      app.totalsGrid.set('store', currentMemory);
    }

I don't have anything programmatically limiting my graphics to only those in the map extent.

If I reload the page, the grid populates correctly.

0 Kudos
TracySchloss
Frequent Contributor

Update:  I have changed the mode of my FeatureLayer to MODE_SNAPSHOT from the default AUTO, and that seems to make a difference.  In the discussion  Feature Layer Best Practices | Guide | ArcGIS API for JavaScript   it does mention that one benefit of Vector Tiling is that

  • Only features that intersect the map's current extent are retrieved

I wonder if that's what is going on in this situation?

0 Kudos