leaflet-panel-layers icon indicating copy to clipboard operation
leaflet-panel-layers copied to clipboard

Enhance 'buildItem' to be layer specific instead of just group specific

Open brentfraser opened this issue 4 years ago • 1 comments

I'd like to use the 'buildItem' function for just one layer in the layer control, or have different buildItem functions for each layer.

brentfraser avatar Apr 13 '20 17:04 brentfraser

For those developers wanting to experiment with this type of functionality, edit the custom-item.html example:

      { 
        name: "Parking",
        background: 'images/icons/parking.png',
        layer: L.geoJson(Parking, {pointToLayer: featureToMarker }),
        
        buildItem: function(item) {
          var points = item.layer.getLayers ? item.layer.getLayers().length : 0,
            label = points ? ' ('+points+')' : '',
            size = Math.min(18,Math.max(9,points))+'px',
            node = L.DomUtil.create('span','');
          node.innerHTML = label;
          if(points)
            node.style.fontSize = size;
          if(item.background) {
            node.style.background = "url('"+item.background+"') center left no-repeat";
            node.style.paddingLeft = '24px';
          }
          return node;
        }

      }
    ],
    //options
    { buildItem: function(item) {
        var node;
        if (item.buildItem) {
          node = item.buildItem.call(this, item);
        } else {
          node = L.DomUtil.create('span','');
        }
        return node;
      }
    }
  ) );

Note the icon and number of points is shown only for the Parking layer: image

brentfraser avatar Apr 13 '20 17:04 brentfraser