ol-ext icon indicating copy to clipboard operation
ol-ext copied to clipboard

Feature: Allow some way to sort the Legend, or keep it sorted when adding/removing items.

Open laundmo opened this issue 5 months ago • 1 comments

In my use-case, I switch between separate modes of the map, upon which I change some of the Legend items. The issue is, when removing and re-adding them, they get added at the bottom. This requires keeping a sorted list of all legend items externally, and re-adding them all.

I would love some feature which allows keeping them sorted. Ideally, i'd imagine some kind of sort index as part of LegendItemOptions, by which they're sorted when rendering. Another option would be to allow disabling Items, which means they can keep their original position and just be disabled, instead of removing them.

laundmo avatar Jul 02 '25 10:07 laundmo

You can use the getItems() method to get the items list. This is an ol/Collection with insertAt, remove, removeAt method.

legend.getItems().insertAt(2, new ol.legend.Item({
   title: 'test',
   feature: f
}));

Viglino avatar Jul 02 '25 13:07 Viglino