# Filter by group

Offices by region and country, nested in the list, with a checkbox for each region and country.

## How it works

A company's offices around the world, each in a region and a country: its `group` is a path, outermost first, so the list nests the countries under their regions, each heading counting everything under it, and a checkbox for each region and country above the list filters the map and the list.

- **A partly ticked group**: it shows its ticked subgroups, not entries of its own (those whose path ends at it, such as `group: ["Europe"]`): a group in the filter always takes all its subgroups. This map has none at a region.
- **HTML**: `group='["Europe","Italy"]'` on `<maplace-location>`, the path as JSON: a `group` starting with `[` is read as a path ([Location attributes](/element/#location-attributes)); `group-filter`, `group-counts` and `search` on `<maplace-map>` ([Map attributes](/element/#map-attributes)).
- **Script** and **Bundler**: `group: ["Europe", "Italy"]` on each office, up to 3 names ([Every location](/locations/#every-location)), and `groupFilter`, `groupCounts` and `search` on the list ([List view options](/store/#list-view-options)). The boxes call [`setFilter`](/store/#methods) for you: `setFilter([["Europe", "Italy"]])` asks for one path, Italy's offices, while `setFilter(["Europe", "Italy"])` asks for two groups, Europe and a top-level Italy; `setFilter(["Europe"])` shows Europe's countries too.
- **React**: `<LocationList groupFilter groupCounts search />` inside `<MaplaceProvider>` ([`<LocationList>`](/react/#locationlist)).

## Code

### HTML on MapLibre

`packages/maplace/examples/offices/maplibre/page.html`

```html
<!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element-maplibre.iife.js"></script>
<style>
  /* On a phone the list goes under the map, its half staying that size. */
  @media (width < 40rem) {
    #offices-maplibre-map { height: 36rem; }
    #offices-maplibre-map > .maplace-list { flex: 0 0 50%; max-height: 50%; }
  }
</style>
<!-- A company's offices around the world, each in a region and a country (a group path, as JSON in the group attribute): the list nests the countries under their regions, with how many each holds, and a checkbox for each region and country above it filters the map and the list. -->
<maplace-map id="offices-maplibre-map" list list-heading="Offices" group-counts group-filter search
  search-label="Find an office">
  <maplace-location location-id="milan" lat="45.46" lng="9.19" title="Milan" group='["Europe","Italy"]'></maplace-location>
  <maplace-location location-id="rome" lat="41.9" lng="12.5" title="Rome" group='["Europe","Italy"]'></maplace-location>
  <maplace-location location-id="turin" lat="45.07" lng="7.69" title="Turin" group='["Europe","Italy"]'></maplace-location>
  <maplace-location location-id="paris" lat="48.86" lng="2.35" title="Paris" group='["Europe","France"]'></maplace-location>
  <maplace-location location-id="lyon" lat="45.76" lng="4.84" title="Lyon" group='["Europe","France"]'></maplace-location>
  <maplace-location location-id="london" lat="51.51" lng="-0.13" title="London" group='["Europe","United Kingdom"]'></maplace-location>
  <maplace-location location-id="manchester" lat="53.48" lng="-2.24" title="Manchester" group='["Europe","United Kingdom"]'></maplace-location>
  <maplace-location location-id="tokyo" lat="35.68" lng="139.69" title="Tokyo" group='["Asia","Japan"]'></maplace-location>
  <maplace-location location-id="osaka" lat="34.69" lng="135.5" title="Osaka" group='["Asia","Japan"]'></maplace-location>
  <maplace-location location-id="singapore" lat="1.29" lng="103.85" title="Singapore" group='["Asia","Singapore"]'></maplace-location>
  <maplace-location location-id="new-york" lat="40.71" lng="-74.01" title="New York" group='["Americas","United States"]'></maplace-location>
  <maplace-location location-id="chicago" lat="41.88" lng="-87.63" title="Chicago" group='["Americas","United States"]'></maplace-location>
  <maplace-location location-id="san-francisco" lat="37.77" lng="-122.42" title="San Francisco" group='["Americas","United States"]'></maplace-location>
  <maplace-location location-id="sao-paulo" lat="-23.55" lng="-46.63" title="São Paulo" group='["Americas","Brazil"]'></maplace-location>
  <maplace-location location-id="rio" lat="-22.91" lng="-43.17" title="Rio de Janeiro" group='["Americas","Brazil"]'></maplace-location>
  <maplace-location location-id="recife" lat="-8.05" lng="-34.88" title="Recife" group='["Americas","Brazil"]'></maplace-location>
</maplace-map>
```

### HTML on Google

`packages/maplace/examples/offices/page.html`

```html
<!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element.iife.js"></script>
<style>
  /* On a phone the list goes under the map, its half staying that size. */
  @media (width < 40rem) {
    #offices-map { height: 36rem; }
    #offices-map > .maplace-list { flex: 0 0 50%; max-height: 50%; }
  }
</style>
<!-- A company's offices around the world, each in a region and a country (a group path, as JSON in the group attribute): the list nests the countries under their regions, with how many each holds, and a checkbox for each region and country above it filters the map and the list. -->
<maplace-map id="offices-map" api-key="YOUR_KEY" list list-heading="Offices" group-counts group-filter search
  search-label="Find an office">
  <maplace-location location-id="milan" lat="45.46" lng="9.19" title="Milan" group='["Europe","Italy"]'></maplace-location>
  <maplace-location location-id="rome" lat="41.9" lng="12.5" title="Rome" group='["Europe","Italy"]'></maplace-location>
  <maplace-location location-id="turin" lat="45.07" lng="7.69" title="Turin" group='["Europe","Italy"]'></maplace-location>
  <maplace-location location-id="paris" lat="48.86" lng="2.35" title="Paris" group='["Europe","France"]'></maplace-location>
  <maplace-location location-id="lyon" lat="45.76" lng="4.84" title="Lyon" group='["Europe","France"]'></maplace-location>
  <maplace-location location-id="london" lat="51.51" lng="-0.13" title="London" group='["Europe","United Kingdom"]'></maplace-location>
  <maplace-location location-id="manchester" lat="53.48" lng="-2.24" title="Manchester" group='["Europe","United Kingdom"]'></maplace-location>
  <maplace-location location-id="tokyo" lat="35.68" lng="139.69" title="Tokyo" group='["Asia","Japan"]'></maplace-location>
  <maplace-location location-id="osaka" lat="34.69" lng="135.5" title="Osaka" group='["Asia","Japan"]'></maplace-location>
  <maplace-location location-id="singapore" lat="1.29" lng="103.85" title="Singapore" group='["Asia","Singapore"]'></maplace-location>
  <maplace-location location-id="new-york" lat="40.71" lng="-74.01" title="New York" group='["Americas","United States"]'></maplace-location>
  <maplace-location location-id="chicago" lat="41.88" lng="-87.63" title="Chicago" group='["Americas","United States"]'></maplace-location>
  <maplace-location location-id="san-francisco" lat="37.77" lng="-122.42" title="San Francisco" group='["Americas","United States"]'></maplace-location>
  <maplace-location location-id="sao-paulo" lat="-23.55" lng="-46.63" title="São Paulo" group='["Americas","Brazil"]'></maplace-location>
  <maplace-location location-id="rio" lat="-22.91" lng="-43.17" title="Rio de Janeiro" group='["Americas","Brazil"]'></maplace-location>
  <maplace-location location-id="recife" lat="-8.05" lng="-34.88" title="Recife" group='["Americas","Brazil"]'></maplace-location>
</maplace-map>
```

### Script on MapLibre

`packages/maplace/examples/offices/maplibre/script.html`

```html
<!doctype html>
<!-- A page of your own script: the MapLibre CDN script, which bundles maplibre-gl and its stylesheet and what the map and its list mount on, and the map made with Maplace; the offices by region and country, a checkbox for each above the list. -->
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element-maplibre.iife.js"></script>
<style>
  #offices-maplibre-script-map-and-list { display: flex; height: 24rem; }
  #offices-maplibre-script-map { flex: 1 1 auto; min-width: 0; }
  #offices-maplibre-script-list { display: contents; }
  #offices-maplibre-script-list .maplace-list { flex: 0 1 16rem; }
  /* On a phone the list goes under the map, its half staying that size. */
  @media (width < 40rem) {
    #offices-maplibre-script-map-and-list { flex-direction: column; height: 36rem; }
    #offices-maplibre-script-list .maplace-list { flex: 0 0 50%; max-height: 50%; }
  }
</style>
<div id="offices-maplibre-script-map-and-list">
  <div id="offices-maplibre-script-map"></div>
  <div id="offices-maplibre-script-list"></div>
</div>
<script type="module">
  // Each office in a region and a country: a group path, outermost first.
  const offices = await fetch("/examples/data/offices.json").then((response) => response.json());
  Maplace.createMapLibreMap("#offices-maplibre-script-map", {
    locations: offices,
    list: {
      container: "#offices-maplibre-script-list",
      heading: "Offices",
      groupCounts: true,
      groupFilter: true,
      search: true,
      searchLabel: "Find an office",
    },
  });
</script>
```

### Script on Google

`packages/maplace/examples/offices/script.html`

```html
<!doctype html>
<!-- A page of your own script: the CDN script and what the map and its list mount on, and the map made with Maplace, which adds Maps JS with your key; the offices by region and country, a checkbox for each above the list. -->
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element.iife.js"></script>
<style>
  #offices-script-map-and-list { display: flex; height: 24rem; }
  #offices-script-map { flex: 1 1 auto; min-width: 0; }
  #offices-script-list { display: contents; }
  #offices-script-list .maplace-list { flex: 0 1 16rem; }
  /* On a phone the list goes under the map, its half staying that size. */
  @media (width < 40rem) {
    #offices-script-map-and-list { flex-direction: column; height: 36rem; }
    #offices-script-list .maplace-list { flex: 0 0 50%; max-height: 50%; }
  }
</style>
<div id="offices-script-map-and-list">
  <div id="offices-script-map"></div>
  <div id="offices-script-list"></div>
</div>
<script type="module">
  // Each office in a region and a country: a group path, outermost first.
  const offices = await fetch("/examples/data/offices.json").then((response) => response.json());
  Maplace.createGoogleMap("#offices-script-map", {
    apiKey: "YOUR_KEY",
    locations: offices,
    list: {
      container: "#offices-script-list",
      heading: "Offices",
      groupCounts: true,
      groupFilter: true,
      search: true,
      searchLabel: "Find an office",
    },
  });
</script>
```

### Bundler on MapLibre

`packages/maplace/examples/offices/maplibre/vanilla.html`

```html
<!doctype html>
<!-- The page vanilla.ts runs on: what the map and its list mount on, and the module, which brings maplibre-gl and its stylesheet. -->
<style>
  #offices-maplibre-vanilla-map-and-list { display: flex; height: 24rem; }
  #offices-maplibre-vanilla-map { flex: 1 1 auto; min-width: 0; }
  #offices-maplibre-vanilla-list { display: contents; }
  #offices-maplibre-vanilla-list .maplace-list { flex: 0 1 16rem; }
  /* On a phone the list goes under the map, its half staying that size. */
  @media (width < 40rem) {
    #offices-maplibre-vanilla-map-and-list { flex-direction: column; height: 36rem; }
    #offices-maplibre-vanilla-list .maplace-list { flex: 0 0 50%; max-height: 50%; }
  }
</style>
<div id="offices-maplibre-vanilla-map-and-list">
  <div id="offices-maplibre-vanilla-map"></div>
  <div id="offices-maplibre-vanilla-list"></div>
</div>
<script type="module" src="./vanilla.ts"></script>
```

`packages/maplace/examples/offices/maplibre/vanilla.ts`

```ts
import "maplibre-gl/dist/maplibre-gl.css";
import type { Location } from "@maplacejs/maplace";
import { createMapLibreMap } from "@maplacejs/maplace/maplibre";

// Each office in a region and a country: a group path, outermost first.
const offices: Location[] = await (await fetch("/examples/data/offices.json")).json();

createMapLibreMap("#offices-maplibre-vanilla-map", {
  locations: offices,
  list: {
    container: "#offices-maplibre-vanilla-list",
    heading: "Offices",
    groupCounts: true,
    groupFilter: true,
    search: true,
    searchLabel: "Find an office",
  },
});
```

### Bundler on Google

`packages/maplace/examples/offices/vanilla.html`

```html
<!doctype html>
<!-- The page vanilla.ts runs on: Maps JS from Google's bootstrap loader, what the module mounts on, the module. -->
<script>
  (g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})({
    key: "YOUR_KEY",
    v: "weekly",
  });
</script>
<style>
  #offices-vanilla-map-and-list { display: flex; height: 24rem; }
  #offices-vanilla-map { flex: 1 1 auto; min-width: 0; }
  #offices-vanilla-list { display: contents; }
  #offices-vanilla-list .maplace-list { flex: 0 1 16rem; }
  /* On a phone the list goes under the map, its half staying that size. */
  @media (width < 40rem) {
    #offices-vanilla-map-and-list { flex-direction: column; height: 36rem; }
    #offices-vanilla-list .maplace-list { flex: 0 0 50%; max-height: 50%; }
  }
</style>
<div id="offices-vanilla-map-and-list">
  <div id="offices-vanilla-map"></div>
  <div id="offices-vanilla-list"></div>
</div>
<script type="module" src="./vanilla.ts"></script>
```

`packages/maplace/examples/offices/vanilla.ts`

```ts
import type { Location } from "@maplacejs/maplace";
import { createGoogleMap } from "@maplacejs/maplace/google";

// Each office in a region and a country: a group path, outermost first.
const offices: Location[] = await (await fetch("/examples/data/offices.json")).json();

createGoogleMap("#offices-vanilla-map", {
  locations: offices,
  list: {
    container: "#offices-vanilla-list",
    heading: "Offices",
    groupCounts: true,
    groupFilter: true,
    search: true,
    searchLabel: "Find an office",
  },
});
```

### React on MapLibre

`packages/maplace/examples/offices/maplibre/react.html`

```html
<!doctype html>
<!-- The page react.tsx renders into: what the module mounts on, and the module, which brings maplibre-gl and its stylesheet. -->
<style>
  #offices-maplibre-react-root .map-and-list { display: flex; height: 24rem; }
  #offices-maplibre-react-root .map { flex: 1 1 auto; min-width: 0; }
  #offices-maplibre-react-root .maplace-list { flex: 0 1 16rem; }
  /* On a phone the list goes under the map, its half staying that size. */
  @media (width < 40rem) {
    #offices-maplibre-react-root .map-and-list { flex-direction: column; height: 36rem; }
    #offices-maplibre-react-root .maplace-list { flex: 0 0 50%; max-height: 50%; }
  }
</style>
<div id="offices-maplibre-react-root"></div>
<script type="module" src="./react.tsx"></script>
```

`packages/maplace/examples/offices/maplibre/react.tsx`

```tsx
import "maplibre-gl/dist/maplibre-gl.css";
import type { Location } from "@maplacejs/maplace";
import { createMapLibreAdapter } from "@maplacejs/maplace/maplibre";
import { LocationList, MaplaceMap, MaplaceProvider } from "@maplacejs/maplace/react";
import { StrictMode, useEffect, useState } from "react";
import { createRoot } from "react-dom/client";

function Offices() {
  // Each office in a region and a country: a group path, outermost first.
  const [offices, setOffices] = useState<readonly Location[]>([]);
  useEffect(() => {
    fetch("/examples/data/offices.json")
      .then((response) => response.json())
      .then(setOffices);
  }, []);
  return (
    <MaplaceProvider locations={offices}>
      <div className="map-and-list">
        <MaplaceMap createAdapter={createMapLibreAdapter} className="map" />
        <LocationList
          heading="Offices"
          groupCounts
          groupFilter
          search
          searchLabel="Find an office"
        />
      </div>
    </MaplaceProvider>
  );
}

createRoot(document.getElementById("offices-maplibre-react-root") as HTMLElement).render(
  <StrictMode>
    <Offices />
  </StrictMode>,
);
```

### React on Google

`packages/maplace/examples/offices/react.html`

```html
<!doctype html>
<!-- The page react.tsx renders into: Maps JS from Google's bootstrap loader, what the module mounts on, the module. -->
<script>
  (g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})({
    key: "YOUR_KEY",
    v: "weekly",
  });
</script>
<style>
  #offices-react-root .map-and-list { display: flex; height: 24rem; }
  #offices-react-root .map { flex: 1 1 auto; min-width: 0; }
  #offices-react-root .maplace-list { flex: 0 1 16rem; }
  /* On a phone the list goes under the map, its half staying that size. */
  @media (width < 40rem) {
    #offices-react-root .map-and-list { flex-direction: column; height: 36rem; }
    #offices-react-root .maplace-list { flex: 0 0 50%; max-height: 50%; }
  }
</style>
<div id="offices-react-root"></div>
<script type="module" src="./react.tsx"></script>
```

`packages/maplace/examples/offices/react.tsx`

```tsx
import type { Location } from "@maplacejs/maplace";
import { createGoogleAdapter } from "@maplacejs/maplace/google";
import { LocationList, MaplaceMap, MaplaceProvider } from "@maplacejs/maplace/react";
import { StrictMode, useEffect, useState } from "react";
import { createRoot } from "react-dom/client";

function Offices() {
  // Each office in a region and a country: a group path, outermost first.
  const [offices, setOffices] = useState<readonly Location[]>([]);
  useEffect(() => {
    fetch("/examples/data/offices.json")
      .then((response) => response.json())
      .then(setOffices);
  }, []);
  return (
    <MaplaceProvider locations={offices}>
      <div className="map-and-list">
        <MaplaceMap createAdapter={createGoogleAdapter} className="map" />
        <LocationList
          heading="Offices"
          groupCounts
          groupFilter
          search
          searchLabel="Find an office"
        />
      </div>
    </MaplaceProvider>
  );
}

createRoot(document.getElementById("offices-react-root") as HTMLElement).render(
  <StrictMode>
    <Offices />
  </StrictMode>,
);
```
