Leaflet.HtmlLegend icon indicating copy to clipboard operation
Leaflet.HtmlLegend copied to clipboard

allow no label and still have a collapsible item

Open brentfraser opened this issue 6 years ago • 6 comments

Currently if the legend entry does not have a label value , the entry is not collapsible. And a missing label key causes "undefined" to be shown on the legend.

brentfraser avatar Dec 05 '19 18:12 brentfraser

Thanks for reporting this. I'll take a look later. Also, feel free to submit a PR if you have a fix for it.

ka7eh avatar Dec 06 '19 02:12 ka7eh

The first part of the issue is intentional. If you want to legend with a single entry and no label to be collapsible, you can set collapseSimple to true in its options.

As for the second part, I couldn't reproduce the issue. Can you provide an example for it? Thanks.

ka7eh avatar Jan 12 '20 03:01 ka7eh

Simply remove/rename the "name" key from the legends list and the item is no longer collapsible. Below is an edit of the Example to show this. Note the name is not shown (as expected), but there is no ability to collapse the elements (a problem):

`

var htmlLegend1and2 = L.control.htmllegend({
    position: 'bottomright',
    legends: [{
        nameTEST: 'Layer 1',
        layer: layer1,
        elements: [{
            label: 'Rectangle',
            html: '',
            style: {
                'background-color': 'red',
                'width': '10px',
                'height': '10px'
            }
        }, {
            label: 'Triangle',
            html: '',
            style: {
                'background-color': 'green',
                'width': '10px',
                'height': '10px'
            }
        }]
    }],
    collapseSimple: true,
    detectStretched: true,
    collapsedOnInit: true,
    defaultOpacity: 0.7,
    visibleIcon: 'icon icon-eye',
    hiddenIcon: 'icon icon-eye-slash'
})
map.addControl(htmlLegend1and2)

`

brentfraser avatar Jan 12 '20 19:01 brentfraser

One work-around is to use name: ' ',

brentfraser avatar Jan 12 '20 19:01 brentfraser

As for the "undefined", it appears when the html key is not included (not the label key as I originally reported above)

brentfraser avatar Jan 12 '20 19:01 brentfraser

Thanks for clarifying this. I looked at the first case again and the current behavior is intentional because right now I don't see an elegant way of having a collapsible legend without a label. I fixed the second part in #13.

ka7eh avatar Jan 12 '20 21:01 ka7eh