Select to view content in your preferred language

How do I get my layer checkboxes to update layer visibility?

6942
22
Jump to solution
05-01-2015 11:37 AM
ChrisSergent
Deactivated User

I am trying to use this sample to toggle layers: Toggle layer visibility | ArcGIS API for JavaScript  but I receive this error message:

TypeError: Cannot read property 'on' of null {stack: (...), message: "Cannot read property 'on' of null"}

  1. message: "Cannot read property 'on' of null"
  2. stack: (...)
  3. get stack: function () { [native code] }
  4. set stack: function () { [native code] }
  5. __proto__: Error

"in domReady callback"

"TypeError: Cannot read property 'on' of null

and here is my code:

var visibleLayerIds = [];
    on(dom.byId("lyrSigns"), "change", updateLayerVisibility);
    on(dom.byId("lyrSupports"), "change", updateLayerVisibility);


    function updateLayerVisibility() {
        var inputs = query(".list_item");
        var inputCount = inputs.length;
               


        for (var i = 0; i < inputCount; i++) {
            if (inputs.checked) {
                visibleLayerIds.push(inputs.value);
            }
        }


        if (visibleLayerIds.length === 0) {
            visibleLayerIds.push(-1);
        }


        layer.setVisibleLayers(visibleLayerIds);
    }

And here is my project on github: csergent45/streetSigns · GitHub

Tags (2)
1 Solution

Accepted Solutions
KellyHutchins
Esri Frequent Contributor

In your updated sample you are setting the featureLayer.setVisiblity to an array of ids which is not valid for that method. The setVisiblity method accepts  true or false. So your code should be something like:

myLayer.setVisibility(false);

View solution in original post

22 Replies
KellyHutchins
Esri Frequent Contributor

Try checking to see what the value is for dom.byId("lyrSigns") and dom.byId("lyrSupports"). If you set a breakpoint in the code at that line and check the values are they correct?

ChrisSergent
Deactivated User

I am receiving this error immediately:

TypeError: Cannot read property 'call' of undefined {stack: (...), message: "Cannot read property 'call' of undefined"}

  1. message: "Cannot read property 'call' of undefined"
  2. stack: (...)
  3. get stack: function () { [native code] }
  4. set stack: function () { [native code] }
  5. __proto__: Error

"in domReady callback"

"TypeError: Cannot read property 'call' of undefined at Function.k.parse (http://js.arcgis.com/3.13compact/init.js:218:362) at k (http://js.arcgis.com/3.13compact/init.js:218:47) at http://localhost:50548/app/main.js:130:52 at ha (http://js.arcgis.com/3.13compact/init.js:32:473) at ha (http://js.arcgis.com/3.13compact/init.js:32:184) at http://js.arcgis.com/3.13compact/init.js:33:202 at ia (http://js.arcgis.com/3.13compact/init.js:33:89) at fa (http://js.arcgis.com/3.13compact/init.js:33:144) at b (http://js.arcgis.com/3.13compact/init.js:34:122) at e (http://js.arcgis.com/3.13compact/init.js:170:442)"

And I just added these two line to try to get the value of the checkboxes:

on(dom.byId("lyrSigns"), "change", console.log(on(dom.byId("lyrSigns").value)));

on(dom.byId("lyrSupports"), "change", console.log(on(dom.byId("lyrSupports").value)));

0 Kudos
KellyHutchins
Esri Frequent Contributor

Did you try Gregg Roemhildt​'s suggestion because I suspect that its the solution to this issue.

ChrisSergent
Deactivated User

I tried Gregg Roemhildt 's solution by entering the following:

var visibleLayerIds = [];

  

  on(dom.byId("lyrSigns"), "change", updateLayerVisibility);
    //on(dom.byId("lyrSupports"), "change", updateLayerVisibility);


    function updateLayerVisibility() {
        var inputs = query(".list_item");
        var inputCount = inputs.length;
             
        visibleLayerIds = [0];


        for (var i = 0; i < inputCount; i++) {
            if (inputs.checked) {
                visibleLayerIds.push(inputs.value);
            }
        }


        if (visibleLayerIds.length === 0) {
            visibleLayerIds.push(-1);
        }
        signLayerUrl.setVisibleLayers(visibleLayerIds);
    }

I have been referencing this sample: https://developers.arcgis.com/javascript/jssamples/map_explicitlayerlist.html

And I wondered, since I am working with two separate feature layers, do I need to work with them individually as the example just uses a single dynamic layer.

Either way, when I update the code from above I get the following error: ncaught ReferenceError: signLayerUrl is not defined

But it's defined in the config section of the page.

I have updated my code with this modification on github: https://github.com/csergent45/streetSigns

0 Kudos
KellyHutchins
Esri Frequent Contributor

If you are using feature layers you will want to use the feature layer's setVisibility method instead of setVisibleLayers.

The error saying the variable (signLayerUrl) is not defined generally means that the variable you are using isn't available.  Perhaps there's a typo in your app or maybe its a scope issue.  Try setting a breakpoint in your app at line 22 and inspect the signLayerUrl variable to see if its undefined.  

ChrisSergent
Deactivated User

Line 22? I have that as my require statements.

0 Kudos
KellyHutchins
Esri Frequent Contributor

line 22 in your code snippet above not in your source.

ChrisSergent
Deactivated User

I have updated it as follows and there are no errors, but nothing is happening:

var visibleLayerIds = [];
   


    on(dom.byId("lyrSigns"), "change", updateLayerVisibility);
    //on(dom.byId("lyrSupports"), "change", updateLayerVisibility);


    function updateLayerVisibility() {
        var inputs = query(".list_item");
        var inputCount = inputs.length;
             
        visibleLayerIds = [0];


        for (var i = 0; i < inputCount; i++) {
            if (inputs.checked) {
                visibleLayerIds.push(inputs.value);
            }
        }


        if (visibleLayerIds.length === 0) {
            visibleLayerIds.push(-1);
        }
        app.signLayer.setVisibility(visibleLayerIds);
    }

But app.signLayer does define as my sign layer.

0 Kudos
KellyHutchins
Esri Frequent Contributor

In your updated sample you are setting the featureLayer.setVisiblity to an array of ids which is not valid for that method. The setVisiblity method accepts  true or false. So your code should be something like:

myLayer.setVisibility(false);