Select to view content in your preferred language

Excluding children from a layerlist

810
3
04-17-2011 10:48 PM
AW1
by
Emerging Contributor
Hi,

I've used the ESRI sample for dynamically building checkbox toggles for layer visibility. However, as the layers in my map service occur several times albeit in different scales, the list layers function shows both the name of the parent group layer as well as the names of the children. Like so:

-Checkbox- Roads
-Checkbox- Roads 500
-Checkbox- Roads 1000
-Checkbox- Roads 1500

Thusly, I need a way to exclude the children from the list (Roads 500, 1000, 1500) while still having the Roads group checkbox hiding or showing all of its children. Is there some variable that I can check in the buildlayerlist function that filters out the layers I want to exclude? How would the code look?

Any help is very appreciated!

Thank you
0 Kudos
3 Replies
HemingZhu
Frequent Contributor
Hi,

I've used the ESRI sample for dynamically building checkbox toggles for layer visibility. However, as the layers in my map service occur several times albeit in different scales, the list layers function shows both the name of the parent group layer as well as the names of the children. Like so:

-Checkbox- Roads
-Checkbox- Roads 500
-Checkbox- Roads 1000
-Checkbox- Roads 1500

Thusly, I need a way to exclude the children from the list (Roads 500, 1000, 1500) while still having the Roads group checkbox hiding or showing all of its children. Is there some variable that I can check in the buildlayerlist function that filters out the layers I want to exclude? How would the code look?

Any help is very appreciated!

Thank you


you can identify a sublayer by using layer property: parentLayerId.
here is the code sample show how:
  
var infos = myMapLayer.layerInfos;
//set the selected layer visible
for (var i = 0; i < infos.length; i++) {
if (infos.parentLayerId !=-1){ // a sublayer
if (infos.name ==your sublayer name)
viewService.setVisibleLayers();
{
}
0 Kudos
AW1
by
Emerging Contributor
Thank you for the help!

However, I'm still having problems integrating it into the appropriate function.
I tried doing this, but it doesn't seem to have any effect:

      function buildLayerList(layer) {

        var items = dojo.map(layer.layerInfos,function(info,index){
          if (layer.parentLayerId == -1) {
            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);

      }


The idea was to insert a condition where it only creates checkboxes for the group parents, but I'm obviously missing something.

Thanks again for help!

EDIT:

To clarify: I want to toggle the visibility of a group of layers with the help of a checkbox that refers to the parent in the group. I want to prevent the script from generating checkboxes for the children, but I still want to show the children in the map when their parent is checked.
0 Kudos
HemingZhu
Frequent Contributor
Thank you for the help!

However, I'm still having problems integrating it into the appropriate function.
I tried doing this, but it doesn't seem to have any effect:

      function buildLayerList(layer) {

        var items = dojo.map(layer.layerInfos,function(info,index){
          if (layer.parentLayerId == -1) {
            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);

      }


The idea was to insert a condition where it only creates checkboxes for the group parents, but I'm obviously missing something.

Thanks again for help!

EDIT:

To clarify: I want to toggle the visibility of a group of layers with the help of a checkbox that refers to the parent in the group. I want to prevent the script from generating checkboxes for the children, but I still want to show the children in the map when their parent is checked.


You can't fetch layerInfos until after layer properties for the layer are successfully populated. You can fire layer's onLoad event to get and set the layer's visibility.. try this:

  function buildLayerList(layer) {
        dojo.connect(layer, "onLoad", function(){
             var items = dojo.map(layer.layerInfos,function(info,index){
               if (layer.parentLayerId == -1) {
                     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>";
        });
     });
0 Kudos