Select to view content in your preferred language

Can I modify TOC.js to honor the setVisibleLayers I've defined?

3778
7
12-06-2013 11:30 AM
TracySchloss
Honored Contributor
If I limit the layers that are visible in my ArcGISDynamicMapService layer using setVisibleLayers, how can I modify the TOC.js dijit to honor these?  I have multiple layers in this service and I only need one of them for this particular project.  If I limit what is loaded, I get empty checkboxes for the other layers that aren't visible, but without any labels next to them.  No group layers or anything involved, just a service with more than one layer in it. 

I thought I read another thread about this, but I couldn't search it out.  [ATTACH=CONFIG]29657[/ATTACH]
   var countyLayer = new ArcGISDynamicMapServiceLayer(pathName+"/ArcGIS/rest/services/BaseMap/BasicBoundary/MapServer", {id:'countyLayer', visible:false})
  countyLayer.setVisibleLayers([3]);

//other layers I added, map gets created ....

   app.map.on("layers-add-result", function (event) {
        try {
            var toc = new agsjs.dijit.TOC({
              map: app.map,
            layerInfos: [{
             layer: controlLayer,
            title: "Control Points"
          },{
             layer: plssLayer,
            title: "Public Land Survey System"
          },{
             layer: twpRngLayer,
             title: "Township / Range"
          },{
              layer:countyLayer,
              title:"Missouri Counties"               
                }]
            }, 'tocDiv');
            toc.startup();
        }
        catch (e) {
            console.log(e.message);
        }

       });


I have poked around a bit in the TOC code, but I think this is the original behavior and not something I've introduced by accident.
0 Kudos
7 Replies
TracySchloss
Honored Contributor
I did realize there were some updates to this dijit since the last time I downloaded it.  But it still doesn't behave the way I want.  It doesn't honor my setVisibleLayers.  Or maybe my definition isn't the same.  I expect to only see the layer I set with setVisibleLayers and nothing of the others from that service.
0 Kudos
KenBuja
MVP Esteemed Contributor
Tracy,

You can hide layers in the TOC using the syntax

toc.findTOCNode(layer, layerId).hide();


Take a look at this sample.

In one of my applications, I have an array of layerIds I want to show in the TOC. Here's the code I use to hide all the other entries in the TOC. This does require that the map service is at 10.1. Note that it has to be in the load event of the TOC if you want to hide them at design time.

        function createTOC() {
            try {
                var toc = new TOC({
                    map: map,
                    layerInfos: layerInfo
                }, "divLayers");
                toc.startup();
            }
            catch (e) {
                console.log(e.message);
            }
            toc.on("load", function () {
                for (var k = 0; k < layerInfo.length; k++) {
                    var layer = layerInfo.layer;
                    var layersArray = layerInfo.layerList;
                    if (layersArray != null) {
                        var dynamicLayerInfos = layer.createDynamicLayerInfosFromLayerInfos();
                        for (var j = 0; j < dynamicLayerInfos.length; j++) {
                            toc.findTOCNode(layer, dynamicLayerInfos.id).hide(); //hide all the layers
                            for (var i = 0; i < layersArray.length; i++) {
                                if (dynamicLayerInfos.id == layersArray.id) {
                                    toc.findTOCNode(layer, dynamicLayerInfos.id).show(); //only show the layers in the dynamicLayerInfos array
                                }
                            }
                        }
                    }
                }
            });
        }


This is the layerInfo object
layerInfo = [{ layer: layerBenthic, title: "Benthic Data", slider: true, layerList: benthicLayers }];


The layerList array looks like this

            benthicLayers: [
                {
                    label: "Boundaries",
                    id: 72,
                    showLayer: true
                },
                {
                    label: "Structure",
                    id: 75,
                    showLayer: true
                },
                {
                    label: "Biological Cover",
                    id: 73,
                    showLayer: true
                },
                {
                    label: "Live Coral Cover",
                    id: 74,
                    showLayer: true
                },
                {
                    label: "Percent Hardbottom",
                    id: 76,
                    showLayer: true
                },
                {
                    label: "Zone",
                    id: 77,
                    showLayer: true
                }
RobertKirkwood
Frequent Contributor

If i have a service that is broken, it breaks the TOC. How can i avoid this?  I have some code that you gave me to check if the service is getting an error. How do i implement that in the TOC dynamically?

0 Kudos
KenBuja
MVP Esteemed Contributor

One way would be to build the layerInfo array dynamically, so that when a service loads properly, then the layers for that service gets added. Otherwise, they are not added and should not break the TOC

RobertKirkwood
Frequent Contributor

Ken,

Can i use this function to set the opacity of a single sub layer? 

I am using this to hide layers:

tocEORI.findTOCNode(eoriLayers, 12).hide();

can i use something like that to set the opacity of that one layer? 

0 Kudos
KenBuja
MVP Esteemed Contributor

You won't be able to do that with the findTOCNode method. It returns a TOC node, which can only be collapsed, expanded, shown, or hidden.

RobertKirkwood
Frequent Contributor

Thanks, that is what i suspected! 

0 Kudos