Select to view content in your preferred language

How to get reference to instance of Legend in LayerList?

366
2
09-24-2023 06:04 PM
miro_aus
New Contributor II

Given example:

 

 

const layerList = new LayerList({
  view: view,
  listItemCreatedFunction: function(event){
    const item = event.item;
    // displays the legend for each layer list item
    item.panel = {
      content: "legend"
    };
  }
});

 

 

Is there a way to get reference to the legend instance created this way? E.g., if I would like to set legend.respectLayerVisibility to false?

Update - current workaround I use is by creating legend instance from Legend widget myself:

 

 

const legend = new arcgis.Legend({
    view: view,
    respectLayerVisibility: false,
    layerInfos: [
        {
            layer: item.layer,
        },
    ],
});

 

 

And add legend instance created this way to the panel instead of using string 'legend':

 

 

    item.panel = {
      content: legend
    };

 

 

0 Kudos
2 Replies
JoelBennett
MVP Regular Contributor

There doesn't appear to be a way of doing so according to the documentation, but this presently works:

 

const layerList = new LayerList({
  view: view,
  listItemCreatedFunction: function(event){
    const item = event.item;
    // displays the legend for each layer list item
    item.panel = {
      content: "legend"
    };
    var handle = item.panel.watch("className", function(newValue, oldValue, propertyName, target) {
      if (target._legend) {
        handle.remove();
        handle = null;

        var legend = target._legend;
        alert("got legend: " + legend.id);
      }
    });
  }
});

 

 

miro_aus
New Contributor II

Thank you, appreciate it.

I have ended up creating legend object for each item in layer list using Legend widget and layerInfos as:

const legend = new Legend({
  view: view,
  layerInfos: [{
    title: '',
    layer: item.layer,
  },],
});

 

Creating legend by just passing legend string is really convenient way if one doesn't need reference, but until there will be standard  way to get a reference I stick with creating legend with these couple of lines.

0 Kudos