Refresh labels in layer list widget programmatically

215
1
Jump to solution
09-27-2021 07:18 AM
Danik-B
New Contributor III

Web AppBuilder 2.21 Developer Edition

I'm trying to refresh the labels for a specific layer in the layer list widget.  The new labels are dependent of the map view and are recreated at every update-end event for the respective layer.

I can refresh the labels in the layer list widget using the _refresh() method but it collapses the legend for the layer.  How can I expand the legend after the _refresh()?

Or, if anyone has a better solution, I'm all ears.  Thanks.

0 Kudos
1 Solution

Accepted Solutions
Danik-B
New Contributor III

Through some trialing I was able to come up with a solution.  Not sure if this is best practice but it does seem to work.

function updateLayerListLegend(featureLayer){
    //Using the Widget Manager get an instance of the Layer List widget
    let wm = WidgetManager.getInstance(),
        layerListWidget = wm.getWidgetsByName('LayerList')[0];
    if(layerListWidget){
        //Get the layer info for the layer who's legend is to be updated
        let layerInfo = layerListWidget.operLayerInfos.getLayerInfoById(featureLayer.id)
        //Use the redrawLegends method to update the legend.  Parameter must be a layer info, not the feature layer.
        layerListWidget.layerListView.redrawLegends(layerInfo)
    }
}

I couldn't find any documentation on this, so be forewarned. It may not work in other versions.

View solution in original post

0 Kudos
1 Reply
Danik-B
New Contributor III

Through some trialing I was able to come up with a solution.  Not sure if this is best practice but it does seem to work.

function updateLayerListLegend(featureLayer){
    //Using the Widget Manager get an instance of the Layer List widget
    let wm = WidgetManager.getInstance(),
        layerListWidget = wm.getWidgetsByName('LayerList')[0];
    if(layerListWidget){
        //Get the layer info for the layer who's legend is to be updated
        let layerInfo = layerListWidget.operLayerInfos.getLayerInfoById(featureLayer.id)
        //Use the redrawLegends method to update the legend.  Parameter must be a layer info, not the feature layer.
        layerListWidget.layerListView.redrawLegends(layerInfo)
    }
}

I couldn't find any documentation on this, so be forewarned. It may not work in other versions.

0 Kudos