Get and set checkbox visibility by JavaScript code

1018
1
06-11-2014 06:37 AM
BillChappell
Occasional Contributor II
We have a large number of layers in our map and every user complains about the lack of ability to save what's turned on/off.
So my task was to create some code that would store variables in local storage to remember what was checked, and then on-load setup the map for them. So I wrote some code that works from a button that reads all the layers and sublayers and gets the default visibility. However it doesn't read the current map settings. i.e. you can uncheck a layer and it still read true in this code. So how do I get the current check box setting, and how would I set it's value?

What I have so far.

 
function getTheMapLayers() {
   var theInfo2 = []
   var theList2 =""
  for (var j=0, jl=map.layerIds.length; j<jl; j++) {

  theList2 = theList2 +"\n"+map.getLayer(map.layerIds).id+" - Map Service, Visible = "+ map.getLayer(map.layerIds).visible+"\n"

    var items2 = dojo.map(map.getLayer(map.layerIds).layerInfos,function(info,index){
     theInfo2[info.id] = ( "id: "+ info.id +" Name: "+ info.name+" Visibility: "+info.defaultVisibility);
     theList2 = theList2 +"\n"+theInfo2[info.id]
   });
     };
     alert(theList2);
  }; 
0 Kudos
1 Reply
BillChappell
Occasional Contributor II
Got it working. This attached to a button, reads the checkbox values, gets names, layer id, sublayer Id, and if it's checked visible. Now on to the read/write to local storage portion.

        function getTheMapLayers() {
            var theList2 = ""
            var layerID = ""
   var theCheck = ""

            for (var j = 0, jl = map.layerIds.length; j < jl; j++) {

             theList2 = theList2 + "\n" + map.getLayer(map.layerIds).id + " - Map Service, Visible = "+ map.getLayer(map.layerIds).visible + "\n"
                layerID = map.getLayer(map.layerIds).id;
                var clayer = map.getLayer(layerID);
                var visArray = clayer.visibleLayers;

    var items2 = dojo.map(map.getLayer(map.layerIds).layerInfos, function (info, index) {
  
                    var infoID = parseInt(info.id);
                    var ind = visArray.indexOf(infoID);
                    if (ind > -1) {
                        ind = 1;  
      theCheck = "On"
                    }
                    else {
                         ind = -1;
       theCheck = "Off"
                    }
 
     theList2  = theList2   +layerID +" " + info.id + "  "+ info.name +" is "+theCheck + "\n"
 
                });
            };
            alert(theList2);
        };
0 Kudos