How to get selected FeatureLayer in LayerList?

2117
6
Jump to solution
02-09-2017 02:50 PM
RaymondIrwin
New Contributor II

We are using JS 4.2 API.  There must be some way to determine which FeatureLayer was selected to be visible or not without having to write an Action for each item in the LayerList.  Basically, how can I get the FeatureLayer from the LayerList based on action item I chose?  I can get the ListItems, but what attribute tells me it is the one to operate on?

0 Kudos
1 Solution

Accepted Solutions
KristianEkenes
Esri Regular Contributor

Basically, how can I get the FeatureLayer from the LayerList based on action item I chose?  I can get the ListItems, but what attribute tells me it is the one to operate on?

In 4.3 the ListItem will have a `layer` property that indicates the layer associated with the fired action. Not available in 4.2 though.

View solution in original post

6 Replies
YueWu1
by Esri Regular Contributor
Esri Regular Contributor

All you need to do is using layer.watch to check the visible status

featureLayer.watch("visible", function(val){
if(val == true){
alert("Recreation Layer is visible");
} else {
alert("Recreation Layer is invisible")
}
})‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍


Here I provide a sample code to show:

https://jsfiddle.net/xeub4Lww/1/

RaymondIrwin
New Contributor II

Thank you.  You are keying off the visibility and the title, but also specifying title as well.  What happens if have several layers and several are visible and you don't necessarily know all the names in advance?  Suppose we have 50 possible layers and wanted to adjust transparency (a defined action) on any one of those so could see the ones below.  That's a whole lot of code and hard values if have to branch for each item.title.   Isn't there some property or event that says, "I am the ListItem that you chose?"

0 Kudos
KristianEkenes
Esri Regular Contributor

Basically, how can I get the FeatureLayer from the LayerList based on action item I chose?  I can get the ListItems, but what attribute tells me it is the one to operate on?

In 4.3 the ListItem will have a `layer` property that indicates the layer associated with the fired action. Not available in 4.2 though.

IsaacLañado
New Contributor II

Hi Kristian Ekenes

I've been trying to use the ListItem to get the id of the layer that the user just made visible but I haven't been able to get it to work. I'm using API 4.3.

Do you have a little example on how to do this?

I actually made another post: LayerList API 4.3 obtain Id of the sublayer on toggle 

Hope you could help me.

Thank you

Isaac

0 Kudos
KristianEkenes
Esri Regular Contributor

Hi Isaac,

I think I know what you're looking for, but it takes a little extra work than what I described above. In the workflow of my previous comment, I was referring to getting the layer ID when an action is invoked. When toggling layer visibility, the trigger-action event doesn't fire since actions aren't being used. Instead, you'll have to set up a watcher on each list item to watch for the visibility of the item in the layerlist. You can then grab the layerID within the callback of the watcher. I'll take a look at this some more and see if I can post more details on the other thread you mentioned. Hope that helps.

0 Kudos
IsaacLañado
New Contributor II

Ki Kristian, 

Thank you very much for looking into this. It would help a lot if we could get this working or a little example on how to create the watchers.

Thank you for your time and help

0 Kudos