Change the attribute visible of layer when click on checkbok?

911
4
Jump to solution
10-18-2019 12:04 PM
UlisesTabares
New Contributor II

i have this code to detect change on my checkbox.

if is true turn on layer if is not true turn off layer

in my map service the layer have id 14

$(document).ready(function() {

$('#checkboxOrtofoto10m').change(function() {
if(this.checked) {
Layers.findSublayerById(14).labelsVisible = true;
}
});

in console mark this 

any suggestions?
0 Kudos
1 Solution

Accepted Solutions
BenElan
Esri Contributor
4 Replies
BenElan
Esri Contributor

Here is a sample that toggles sublayer visibility: MapImageLayer - Toggle sublayer visibility | ArcGIS API for JavaScript 4.13 

Noah-Sager
Esri Regular Contributor

Hmm, I tested this functionality as a sanity check, and it is possible to do.

Test app (toggle state labels on and off with the button at the bottom)

https://codepen.io/noash/pen/MWWbWZy 

Perhaps the MapServer you're using doesn't support dynamic layers? You need it to in order to make changes dynamically.

When you inspect the MapServer from REST, do you see:

Supports Dynamic Layers: true

?

UlisesTabares
New Contributor II

Thank you for answering if it supports Dynamic Layers, my quick stop solution that works for me is to load the map again, for now I will stay with this solution later I will see the performance. 

UlisesTabares
New Contributor II

I have the solution i only need findSublayerById function .

$('#checkbox').change(function(event) {
var idcheck = event.target.getAttribute("data-id");
if(idcheck){
var sublayer = Layers.findSublayerById(parseInt(idcheck));
sublayer.visible = !sublayer.visible;

}

});