AnsweredAssumed Answered

LayerList reload not working

Question asked by ccgage on May 11, 2016
Latest reply on May 12, 2016 by kenbuja

I have an application where I need to switch out layers when a user makes a selection from a dropdownlist.  I have the switching of the layers working fine, but I cannot get the LayerList to refresh with the newly selected layer.  The LayerList works fine when the application loads, but when a layer is switched it does not work.  I have tried to use the refresh method but that does not seem to work.  Perhaps I am doing something wrong.  I have a simplified version of what I am trying to do in the Javascript code below.   I am using JS API version 3.16.






  "dojo/dom", "dojo/parser", "dojo/on", "dojo/query", "dojo/dom-class",

  "dojo/dom-style", "dojo/dom-construct",














], function(

  dom, parser, on, query, domClass,

  domStyle, domConstruct,

   Map, ArcGISDynamicMapServiceLayer,

   registry, LayerList )




  // Define layers and URL variables

  var dynamicLayer;

  var staticLayer;

  var dynLayerURL;

  var staticURL = "";

  var layer1URL = "";

  var layer2URL = "";


  // Get the layer drop down list

  var selectedLayer = registry.byId('layerDropDown');

  selectedLayer.on('change', setLayer);


  // Start out with layer selected in dropdownList

  var layerValue = selectedLayer.get('value');

  if (layerValue == '1') {

  dynLayerURL = layer1URL;


  else if (layerValue == '2') {

  dynLayerURL = layer2URL;


  map = new Map("mapPanel", {

        center: [-98.2, 40.05],

        zoom: 5,

        basemap: "national-geographic"


  // Define layers as ArcGISDynamicMapServiceLayers

  dynamicLayer = new ArcGISDynamicMapServiceLayer(dynLayerURL, { visible: true });

  staticLayer = new ArcGISDynamicMapServiceLayer(staticURL, { visible: true });


  // Add the LayerList

  var layerList = new LayerList({

       map: map,

       showLegend: true,

       layers: [ {layer: staticLayer, id: "Shelters", sublayers: true}, {layer: dynamicLayer, id: "Assessment", sublayers: true}]

  }, "legendDiv");



  layerList.on('load', function(){




  // ADD the LAYERS

  map.addLayers([staticLayer, dynamicLayer]);


  function loadDynamicLayer() {

  // Remove the current dyamicLayer



  // Point the new Dynamic Layer URL to the dynamicLayer

  dynamicLayer = new ArcGISDynamicMapServiceLayer(dynLayerURL, { visible: true });



// REFRESH the layerList  (DOES NOT WORK)

  //  If I click on the layerList checkboxs, it gives me the error:

  //   TypeError: t.byId(...) is null    ???




  function setLayer() {

  var lyrValue = selectedLayer.get('value');

  if (lyrValue == '1') {

  dynLayerURL = layer1URL;


  else if (lyrValue == '2') {

  dynLayerURL = layer2URL;



  // Load the newly selected dynamic layer




  function expandLayerList() { 


  domClass.add(node, "esriListExpand"); 



  domClass.replace(node, "esri-icon-down", "esri-icon-right");