toggle legend on automatically when layer visibility is true

1607
7
Jump to solution
10-17-2019 02:18 PM
JayHill
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)
0 Kudos
1 Solution

Accepted Solutions
Noah-Sager
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

0 Kudos
7 Replies
Noah-Sager
Esri Regular Contributor

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

ArcGIS API for JavaScript Sandbox 

0 Kudos
JayHill
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.

0 Kudos
Noah-Sager
Esri Regular Contributor

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

0 Kudos
JayHill
Occasional Contributor II

Sure. See link below.

github

0 Kudos
Noah-Sager
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
}

0 Kudos
JayHill
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.

0 Kudos
NilsBabel1
Occasional Contributor

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?  

0 Kudos