Checkboxes unticked and layers not visible when page starts up

646
4
09-04-2013 03:55 PM
RoryHall
New Contributor III
Good day all,
I have an application of 2 maps which requires 40 + layers per map to be toggled on and off for a time series comaparison. I have seperated the layers into groups of checkboxes (page design, trying to fit it all in) and have done this grouping the layers using .push to load layers into lists. To create the checkbox groups I use the following code:

dojo.connect(map1,'onLayersAddResult',function(results){       
        //add check boxes Map 1 Layers
        dojo.forEach(layersList1,function(layer){        
        var layerName = layer.title;
        var checkBox = new dijit.form.CheckBox({
        name: "checkBox" + layer.layer.id,
        value: layer.layer.id,
        checked: layer.layer.visible,
        onChange: function(evt) {
            var clayer = map1.getLayer(this.value);
           clayer.setVisibility(!clayer.visible);
           this.checked = clayer.visible;
                               }
         });
        //add the check box and label to the toc Map 1 Layers
        dojo.place(checkBox.domNode,dojo.byId("Map1_layers"),"after");
        var checkLabel = dojo.create('label',{'for':checkBox.name, innerHTML:layerName},checkBox.domNode,"after");
        dojo.place("<br />",checkLabel,"after");     
        });
        }); 


What I need to do is only show a few of the layers when the page loads. I have been able to accomplish this by setting the visibilty of the layers to true/false in the layer constructor but this interupts the way the checkbox functions. When I check a layer to make it visible "only" the first layer in the list toggles and not the associated layer with the checkbox.

Below is an example of my layer lists and layer constructor:

layersList1.push({layer:Sept09_fl_1m1,title:"Sept 2009"}); 
 layersList1.push({layer:Dec09_fl_2m1,title:'Dec 2009'});
 layersList1.push({layer:Mar10_fl_3m1,title:'Mar 2010'});
 layersList1.push({layer:June10_fl_4m1,title:'June 2010'});


var Landsat_2010_m1 = new esri.layers.ArcGISDynamicMapServiceLayer("http://sirgis2/ArcGIS/rest/services/CentralWest/Landsat_Callide/MapServer");
      Landsat_2010_m1.setVisibleLayers([2]);


thanks for any assistance
cheers ~ Rory
0 Kudos
4 Replies
RoryHall
New Contributor III
A much nicer approach, Thanks Jose
I got stuck in a rut a sticking to an old style of a previous project and building on to it. I had come across TOC a few times and was stubborn to implement it. Thanks for the nudge. It would still be nice to know if it is possible to set the checkboxes and initial visibility of layers in the method I posted, for future referenece

Cheers ~ Rory
0 Kudos
LefterisKoumis
Occasional Contributor III
A much nicer approach, Thanks Jose
I got stuck in a rut a sticking to an old style of a previous project and building on to it. I had come across TOC a few times and was stubborn to implement it. Thanks for the nudge. It would still be nice to know if it is possible to set the checkboxes and initial visibility of layers in the method I posted, for future referenece

Cheers ~ Rory


I use 10.1 and 3.6 api and this TOC gives me a blank map. Let me know if you have a similar setting and it works for you.
Thank you.
0 Kudos
RoryHall
New Contributor III
I have just cut and pasted some of my code into the template on the link and it works fine. The web services are on a 10.0 server and the js api is 3.5 in the template.

With that said I am back to my same original dilemma. The majority of the layers that I need to toggle are feature layers as they need to have info boxes assigned to them. The TOC with really great with DynamicFeatureService, but I can not see how I can implement it with FeatureService layers, with out individually adding them to the TOC.

Cheers ~ Rory
0 Kudos