Select to view content in your preferred language

filter out graphics on GraphicsLayer

752
1
Jump to solution
11-15-2022 11:13 PM
Guy_srb
Regular Contributor

Is there a similar property or workaround like definitionExspression of a featureLayer but in graphicsLayer?

my goal is to filter out graphics with certain attributes from a graphicsLayer but not remove them.

1 Solution

Accepted Solutions
UndralBatsukh
Esri Regular Contributor

Hi there, 

You should use client-side FeatureLayer instead of GraphicsLayer if you want to filter features based on attributes. We have couple of samples that showcase how to create client-side feature layer from graphics

https://next.sites.afd.arcgis.com/javascript/latest/sample-code/layers-featurelayer-collection/

https://next.sites.afd.arcgis.com/javascript/latest/sample-code/layers-featurelayer-collection-edits...

FeatureLayer.source SDK help talks about how to create client-side layers: https://next.sites.afd.arcgis.com/javascript/latest/api-reference/esri-layers-FeatureLayer.html#sour...

With all this said, you still must use GraphicsLayer then you could query all graphics in your GraphicsLayerView and set the visibility of graphics to false based on your needs. You can do something like this: 

view.whenLayerView(graphicsLayer).then(function(layerView){
  layerView.queryGraphics().then(function(results){
    results.forEach((item)=>{
      console.log(graphic?.attributes)
      if (graphic?.attributes?.Name){ // set your requirement here
        graphic.visible = false;
      }
    });
  });
});

 

View solution in original post

1 Reply
UndralBatsukh
Esri Regular Contributor

Hi there, 

You should use client-side FeatureLayer instead of GraphicsLayer if you want to filter features based on attributes. We have couple of samples that showcase how to create client-side feature layer from graphics

https://next.sites.afd.arcgis.com/javascript/latest/sample-code/layers-featurelayer-collection/

https://next.sites.afd.arcgis.com/javascript/latest/sample-code/layers-featurelayer-collection-edits...

FeatureLayer.source SDK help talks about how to create client-side layers: https://next.sites.afd.arcgis.com/javascript/latest/api-reference/esri-layers-FeatureLayer.html#sour...

With all this said, you still must use GraphicsLayer then you could query all graphics in your GraphicsLayerView and set the visibility of graphics to false based on your needs. You can do something like this: 

view.whenLayerView(graphicsLayer).then(function(layerView){
  layerView.queryGraphics().then(function(results){
    results.forEach((item)=>{
      console.log(graphic?.attributes)
      if (graphic?.attributes?.Name){ // set your requirement here
        graphic.visible = false;
      }
    });
  });
});