toggle legend on automatically when layer visibility is true

188
7
Jump to solution
10-17-2019 02:18 PM
Highlighted
Occasional Contributor II

Im using the LayerList (4.13) with the legend integrated into it. I'd like for the legend to toggle open when the user clicks to make the layer visible. I'm watching the layers visible property but cannot figure out the code to toggle the legend open. Any ideas?

Tags (2)
Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
Esri Regular Contributor

It looks like if you uncomment line 1829 in the github code (it was line 1699 for me locally), and set the `open` property to `true`, the legend swatches show by default.

item.panel = {
  content: "legend",
  //open: true
}

View solution in original post

Reply
0 Kudos
7 Replies
Highlighted
Esri Regular Contributor

I'm pretty sure that's what we do in this sample:

ArcGIS API for JavaScript Sandbox 

Reply
0 Kudos
Highlighted
Occasional Contributor II

Noah

Not sure if it is because I have over 20 different layers, I'm not using a webmap, or the fact I have 4 grouplayers but, it does not function in the same way. I turn on a layer and I still have to click the legend button to see it.

Reply
0 Kudos
Highlighted
Esri Regular Contributor

Wanna share some code/repro case and I can take a look?

Reply
0 Kudos
Highlighted
Occasional Contributor II

Sure. See link below.

github

Reply
0 Kudos
Highlighted
Esri Regular Contributor

It looks like if you uncomment line 1829 in the github code (it was line 1699 for me locally), and set the `open` property to `true`, the legend swatches show by default.

item.panel = {
  content: "legend",
  //open: true
}

View solution in original post

Reply
0 Kudos
Highlighted
Occasional Contributor II

Awesome thanks Noah. I must've had the blinders on. One other question though. Is there an easy way to open the layer if the layer is inside a grouplayer? As it stands now if the layer in a grouplayer and is turned on by default the grouplayer doesnt open to reveal the legend.

Reply
0 Kudos
Highlighted
New Contributor III

Hi, sorry to reply to this old post but I'm trying to do the same thing.  I don't think the sample acutally toggles the visibility of the legend.  Adding the legend widget to the layerlist item panel adds the legend toggle button next to the layer item name.  If you set the item.panel open property to item.visible it will indeed open and display the legend of the layers that are turned on when the map first loads.  However, if you turn the layer off it doesn't actually close the panel.  It displays No Legend, which is confusing for the user.  If you click the legend button again it correctly closes the item.panel.  You can see this in the sample you linked to.  The layer visibility and legend visibility work independently of each other.  I would like to link the layer visibility (eyeball button) to the actual item.panel.open property.  So that the legend panel actually closes (and doesn't display No Legend) when a user turns off the layer.  How would I do that?  

Reply
0 Kudos