Legend title based on variable needs to refresh too

1122
4
Jump to solution
08-26-2014 09:05 AM
TracySchloss
Frequent Contributor

I have a project that let's the user change which attribute field is used in the display.  It's based on the sample https://developers.arcgis.com/javascript/jssamples/renderer_dynamic_layer_change_attribute.html  For the legend, I'm creating it if it doesn't exist, but if it does, then I just want it to refresh.  My problem is that the title is based on a variable, defined based on the change event of the FilteringSelect list of fields.  When I change the field, the legend itself updates, but it still has the title from its initial creation.

Can I change just the title?  It doesn't seem to be honoring my title definition when I specify legend.refresh().  In this example, fields is the array of attributes from my layer and fieldChoice is the selected item from the FilteringSelect. 

  domConstruct.empty("legendDiv");

        if (!legend){      

         legend = new Legend({

            map: map,

            layerInfos: [{ "layer": featureLayer, "title": fields[fieldChoice] }]

          },"legendDiv");

          legend.startup();

          }else{

            legend.refresh();

           

          }

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
KellyHutchins
Esri Frequent Contributor

If I understand the question correctly then calling legend.refresh and providing the updated layer info with the new title should update the title in the legend.

          legend.refresh([{

              layer: featureLayer,

              title: "Updated layer name"

            }]);

View solution in original post

0 Kudos
4 Replies
MirceaMaierean
New Contributor III

Ran in the same problem while creating a web map for my Capstone project but using the Generate Render Sample (Generate renderer | ArcGIS API for JavaScript )

I got to solve the problem by adding the "app." back into it and setting the "var app []" as a global variable.

function createLegend(map, fl) {

  // destroy previous legend, if present

  if ( app.hasOwnProperty("legend") ) {

app.legend.destroy();

  domConstruct.destroy(dojo.byId("legendDiv"));

}

  // create a new div for the legend

  var legendDiv = domConstruct.create("div", {

  id: "legendDiv"

  }, dom.byId("legendWrapper"));

  app.legend = new Legend({

  map : map,

  layerInfos : [{

  layer : fl,

  title : "Census Attribute: " + fields[currentAttribute]

  }]

  }, legendDiv);

  app.legend.startup();

  

  }

0 Kudos
TracySchloss
Frequent Contributor

That's not just changing the title, it's getting rid of the entire legend and recreating it every time. I'd like to avoid this.  What I did was add another div right above the legend, calling it legendTitle.  Then I styled the legend so that its default title wouldn't display at all.

.esriLegendServiceLabel {

   display:none;

}

Then I set the content of my new legendTitle to be the field name like I wanted.

0 Kudos
KellyHutchins
Esri Frequent Contributor

If I understand the question correctly then calling legend.refresh and providing the updated layer info with the new title should update the title in the legend.

          legend.refresh([{

              layer: featureLayer,

              title: "Updated layer name"

            }]);

0 Kudos
TracySchloss
Frequent Contributor

Your way worked and is more right then my workaround method, so I changed which one is marked as Correct Answer.

0 Kudos