Get layer name in layerList widget

793
5
10-18-2021 07:46 AM
MichaelBoschert
New Contributor III

Hey,

i just tried to add some actions to my layerlist widget. (increase/decrease opacity, zoom to extent)

In this example they called a special layer by name but i want to use this actions on each layer in my map.

Isn't there an easy way to define the selected Layer in the layerList widget?

I didn't find anything easy like "var layer = layerlist.selectedLayer"

 

Thank you

 

 

0 Kudos
5 Replies
ReneRubalcava
Frequent Contributor

The LayerList has selectedItems Collection you can watch for change events. The Item has a layer property you can reference.

MichaelBoschert
New Contributor III

Thanks for your reply @ReneRubalcava 

I tried with the following snippet;

layerList.on("trigger-action", (event) => {
layerList.selectedItems.watch("ListItem",layer =>{
console.log("do something");
});
});

 

But the console.log doesn't return anything.

0 Kudos
ReneRubalcava
Frequent Contributor

selectedItems is a Collection, so you can use ".on()" to listen for events, like this sample.

https://codepen.io/odoe/pen/zYzXrEy?editors=0010

MichaelBoschert
New Contributor III

Thanks again for your fast reply @ReneRubalcava 

The Solution that works for me is;

layerList.on("trigger-action", (event) => {
   var activeLayer = event.item.layer.name;
   console.log(activeLayer);
});

Defining the 'name" in the layer definition...

0 Kudos
MichaelKarikari1
New Contributor III

I was looking to do the same thing, but your excerpt didnt work for me. Would you be able to provide the exact syntax you used?

 

Thanks

0 Kudos