Group collapsed

Hello,

I recently upgraded from Lizmap Web Client 3.6 to 3.7, and I noticed that some of my JavaScript customizations are no longer working properly.

In particular:

  • When I click on a layer in the layer panel, the legend does not automatically expand anymore.
  • Also, I can no longer keep a group collapsed by default when the map loads.

Has anyone experienced similar issues with version 3.7?
Any ideas, workarounds, or updated code examples would be very welcome.

Thanks in advance!

The JS API for layer tree has changed in LWC 3.7.

For example to collapse all layer groups:

lizMap.events.on({
    uicreated: function(e) {
        const collapseChildren = (layerTreeItem) => {
            for (const child of layerTreeItem.getChildren()) {
                child.expanded = false;
                if (child.type == 'group') {
                    collapseChildren(child);
                }
            }
        };
        collapseChildren(lizMap.mainLizmap.state.layerTree);
    }
});

To find a layer group:

/* @return array */
lizMap.mainLizmap.state.layerTree.findTreeLayersAndGroups().filter(tl => tl.type == 'group' && tl.name == 'your group name')

You can take a look at the JS API JSDoc: Module: LayerTreeState

Merci ! Ca fonctionne !

And is it possible that when we check the layer it automatically unfolds the legend?

Et est ce que c’est possible que lorsqu’on coche la couche ça déplie la légende automatiquement ?

To do so, you have to listen to new event for example:

lizMap.mainLizmap.state.layerTree.addListener((evt) => {
    // it is a group do nothing
    if (evt.target.type == 'group') return;
    // the layer is visible, expand the legend
    if (evt.target.visibility) evt.target.expanded = true;
},'layer.visibility.changed')

Another elegant solution is to use QGIS map themes. This allows you to control the display of the legend/layer group individually for each theme and then for each layer/layer group.

Hello, unfortunately this js does not work for me :confused:

with the most recent version 3.9.4 QGIS map themes respect now checked state (even if the layer is in an unchecked parent group) and expanded state. small bugfixes are on the way, so with 3.9.5 by latest, it should be fully and flawlessly supported. maybe this is an approach whcih works for you, @pierreflorin

for autoexpaneding the legend of a layer when activating it, you still would need to refrain to custom JS - this behaviour is not QGIS default, so it is also not really “desired” by design in lizmap imho