Select to view content in your preferred language

How to update properties of graphic in GraphicsLayer?

497
5
11-18-2024 02:02 PM
Sparkles
Emerging Contributor

Hi, 
I'm trying to update the "attributes" property under an item 
and the "visible" property under an item, in graphicsLayer.graphics.items (graphicsLayer is the GraphicsLayer object)

I tried using setAttribute, but it didn't work.

   graphicsLayer.graphics._items.forEach((item) => {
      item.setAttribute('uid', item.uid)
    })

I didn't find a method to use for updating "visible" property.

Thanks!
0 Kudos
5 Replies
JoelBennett
MVP Regular Contributor

The uid property of a Graphic is set when it is instantiated, and cannot be changed afterwards.  If for some reason you need a graphic with a different uid, but the same info, you should use clone.

There is no method for setting the visible property, but no method is necessary.  Just set the property value instead:

 

graphic.visible = false;

 

 

 

0 Kudos
Sparkles
Emerging Contributor

Hi @JoelBennett , thanks for the reply!. I tried that it didn't persist (it was just changed in the lines of code right after, but then when the component reloaded - using React- the value went back to true)

Also, I'm not trying to change the uid, I'm trying to add an attribute, it's just that in this case I'm trying to add a uid attribute to "attributes" that takes the value from the uid, but this isn't about the uid, because adding any attribute didn't work either. I'm thinking maybe the reason nothing is persisting is because I'm using React? 

0 Kudos
JoelBennett
MVP Regular Contributor

Ok...yeah, I was mistaken about what you meant by uid.  In that case, your use of "setAttribute" is correct. 

However, it's not recommended to use the "_items" array since it's undocumented, and the layer probably wouldn't notice that anything had changed if you manipulate it.  If you change a graphic's properties and/or attributes, you may want to call remove on the collection, and then add it back.  This would properly inform the layer instance that something has changed.

If you use this alternate workflow, and it still doesn't work, then it's possible it could be a React issue.  I don't use that though, so I couldn't say for certain.

0 Kudos
UndralBatsukh
Esri Regular Contributor

Hi there, 

You can loop through the graphics and update its attribute as shown below. No need to get _items, graphics is a collection.

graphicsLayer.graphics.forEach((graphic) =>{
  graphic.attributes.Owner = "Here here";
});


This codepen shows the concept - https://codepen.io/U_B_U/pen/aberQpp?editors=100

 

Sparkles
Emerging Contributor

Hi @UndralBatsukh , thanks for the help! When trying to update visible to false, it works in the code snippet you provided ,but not in my React component. Do you think React could be interfering with this? as the component reloads and it seems the value is coming back from the default arcgis value "true" at that time. It seems that changing an attribute manually in the JS code just changes the value locally but doesn't actually persist the change to the arcgis data. Do you know if there's a built in method to use that would persist the change?

Thanks!

0 Kudos