Setting Default Visibility

2913
5
07-07-2011 06:54 AM
DuncanRager
New Contributor
Greetings,

I'm attempting to create a list of checkboxes for the user to toggle the visibility of feature layers in a dynamic map service layer. I want to start the application with one feature layer visible, while the others remain off.

The sample shown here: http://help.arcgis.com/en/webapi/javascript/arcgis/help/jssamples_start.htm

...uses the 'layer.layerInfos.defaultVisibility' property to determine which checkboxes are turned on from the start.

Even though I use:
layers = new esri.layers.ArcGISDynamicMapServiceLayer("http://server/ArcGIS/rest/services/features/MapServer"
  );
  layers.setVisibleLayers([0])
 }


...to set the initial visibility, that doesn't alter the default visibility property for the other feature layers, so the sample script doesn't work.

Anybody know how to adjust this property?

Thanks,

DR
0 Kudos
5 Replies
derekswingley1
Frequent Contributor
You need to wait for your layer to load before you can call setVisibleLayers. Try this:
layer = new esri.layers.ArcGISDynamicMapServiceLayer("http://server/ArcGIS/rest/services/features/MapServer");
dojo.connect(layer, 'onLoad', function(l) {
    l.setVisibleLayers([1]);
});
0 Kudos
SiqiLi
by Esri Contributor
Esri Contributor
0 Kudos
DuncanRager
New Contributor
As you say, setting the visible layers as an event handler of the layer load works... however for the script below:

function buildLayerList(layer) {
        var items = dojo.map(layer.layerInfos,function(info,index){
          if (info.defaultVisibility) {
            visible.push(info.id);
          }
          return "<input type='checkbox' class='list_item' checked='" + (info.defaultVisibility ? "checked" : "") + "' id='" + info.id + "' onclick='updateLayerVisibility();' /><label for='" + info.id + "'>" + info.name + "</label>";
        });

        dojo.byId("layer_list").innerHTML = items.join();

        layer.setVisibleLayers(visible);
        map.addLayer(layer);


...setting the .setVisibleLayers([]) method doesn't change anything about whether the checkbox is checked. It reads the "default visibility" property of each feature layer in the map and applies a "check" in its box from the start if it is set to "true". And even when I use .setVisibleLayers([0]) to only show the first featureclass in the map, all of the layers have a "default visibility" of "true"... this is confirmed with variable watcher in the Firefox extension Firebug.

So, I'm curious as to how I can set the "default visibility" property of a layer in a map to "false", so that the checkbox I create for it is unchecked from the beginning.

The reason I need this is because I present the user with 10-15 feature layer checkboxes in a single map service, and If they were all checked from the start, it would be majorly slow, and also show a very cluttered map.

For reference, I'm looking at this sample:

http://help.arcgis.com/en/webapi/javascript/arcgis/help/jssamples_start.htm

Thanks!
0 Kudos
StephenLead
Regular Contributor III
So, I'm curious as to how I can set the "default visibility" property of a layer in a map to "false", so that the checkbox I create for it is unchecked from the beginning.


Just a guess, but may need to do this in ArcMap, so that the MXD/MSD uses this setting to create the map service with that visibility hard-coded?


For reference, I'm looking at this sample:
http://help.arcgis.com/en/webapi/javascript/arcgis/help/jssamples_start.htm


To provide the full link to the sample, you need to right-click on the sample's name and choose Copy Link Address, or Open In New Tab. Otherwise the homepage is shown.

Cheers,
Steve
0 Kudos
DuncanRager
New Contributor
Steve,

You were right about setting it in the MXD, thank you.

I've run into one more issue... this piece of code...:

function addChecklist(fLayers){
  var items = dojo.map(fLayers.layerInfos, function(info,index){
   if (info.defaultVisibility) {
    visible.push(info.id);
   }
   
   return "<input type='checkbox' class='list_item' checked='" + (info.defaultVisibility ? "checked" : "") + "' id='" +
    info.id + "' onclick='updateLayerVisibility();' /><label for='" + info.id + "'>" + info.name + "</label>" + "<br />"; 
  });


...doesn't exactly work because no matter what value I've set for the <checked= > attribute of the <input> object, it sees it as true. From my testing, simply have the <checked> attribute in the <input> tag at all sets the default value to 'checked'... so for instance these two lines are the same, resulting in a checked box from the start:

[HTML]<input type='checkbox' class='list_item' id="a" checked="checked"/>

<input type='checkbox' class='list_item' id="b" checked=""/>[/HTML]


Does anyone know if there is a value that can be set in the <checked> attribute so that the default is set to not being checked?

Thanks,

DR

EDIT: I skirted the issue with the code below, but I'm still interested to know if there's an value that would set the checked to false.

function addChecklist(fLayers){
  var items = dojo.map(fLayers.layerInfos, function(info,index){
   if (info.defaultVisibility) {
     visible.push(info.id);
    
     return "<input type='checkbox' class='list_item' checked='" + (info.defaultVisibility ? "checked" : "") + "' id='" +
     info.id + "' onclick='updateLayerVisibility();' /><label for='" + info.id + "'>" +  info.name + "</label>" + "<br />";
     }
   
    else {
     return "<input type='checkbox' class='list_item' id='" +
     info.id + "' onclick='updateLayerVisibility();' /><label for='" + info.id + "'>" + info.name + "</label>" + "<br />";
    }
  });
0 Kudos