Problem with updating legend in Legend widget

2397
3
Jump to solution
02-09-2017 12:13 PM
RolandoFlorez
New Contributor III

This is the problem:

I'm developing a widget that allows classify the visualization of thematic layers depending of selected field.

So, when I apply the render the first time, the legend works fine. Now, if I change de layer, it applies the render but legend take the default value, not the current value. This happens sometimes but not always, I don't understand what is hapenning.

This is my code when I create the Legend:

createLegend: function () {
   htmlF = "<div id='legendDiv' ></div>";
   var node = domConstruct.toDom(htmlF);
   domConstruct.place(node, 'legendField');
   this.legend = new Legend({
      map: this.map,
      layerInfos: [{
         hideLayers: this.parameters.idHideLayers,
         layer: this.parameters.layer,
         title: this.parameters.layer.title
      }
   ]
}, 'legendDiv');
   this.legend.startup();
   this.legend.refresh();
}

The Legend also takes an array of sublayer Ids that will be hide.

thematic classification

0 Kudos
1 Solution

Accepted Solutions
RolandoFlorez
New Contributor III

 

(copy this code and apply format)

createLegend: function () {

var hideLayers = this.parameters.idHideLayers;
var layer = this.parameters.layer;
var title = this.parameters.layer.title;
if (!this.legend) {
htmlF = "<div id='legendDiv' ></div>";
var node = domConstruct.toDom(htmlF);
domConstruct.place(node, 'legendField');
this.legend = new Legend({
map: this.map,
layerInfos: [{
hideLayers: hideLayers,
layer: layer,
title: title
}
]
}, 'legendDiv');
this.legend.startup();
}else{
this.legend.refresh([{
hideLayers:hideLayers,
layer:layer,
title:title
}]);
}
}

Once created the legend, I tried refresh instead destroy it. So that was the solution, refreshing the legend by passing the same parameters in 'layerinfos' (being hideLayers optional)

View solution in original post

3 Replies
RobertScheitlin__GISP
MVP Emeritus

Rolando,

  From the api docs:

You should call legend.refresh() after you change your layer renderer because you have specified the layerInfos.

Refresh the legend. Takes an optional list of layerInfos to replace the layers setup by the legend constructor. Calling refresh is only necessary when layerInfos is used in the Legend constructor, otherwise the legend will refresh the layers automatically.
RolandoFlorez
New Contributor III

Thanks Robert for your response.
But there are something that I cannot understand. When I call the refresh method, can you explain me which parameters I must pass to this method?
Show me an example please according with my code, the documentation is not clear. In my legend constructor I'm passing hideLayers,title and layer.

0 Kudos
RolandoFlorez
New Contributor III

 

(copy this code and apply format)

createLegend: function () {

var hideLayers = this.parameters.idHideLayers;
var layer = this.parameters.layer;
var title = this.parameters.layer.title;
if (!this.legend) {
htmlF = "<div id='legendDiv' ></div>";
var node = domConstruct.toDom(htmlF);
domConstruct.place(node, 'legendField');
this.legend = new Legend({
map: this.map,
layerInfos: [{
hideLayers: hideLayers,
layer: layer,
title: title
}
]
}, 'legendDiv');
this.legend.startup();
}else{
this.legend.refresh([{
hideLayers:hideLayers,
layer:layer,
title:title
}]);
}
}

Once created the legend, I tried refresh instead destroy it. So that was the solution, refreshing the legend by passing the same parameters in 'layerinfos' (being hideLayers optional)