Dev Helpers
← Back

Build a compound shelf index

Build a compound shelf index. Transform the records in JavaScript and lay out the resulting entries with CSS Grid.

Open this example in the interactive library

Source code

<style>
  dl {
    display: grid;
    gap: 0.6rem;
    margin: 0;
  }
  dl > div {
    display: grid;
    gap: 0.35rem;
    padding: 0.6rem;
    border-bottom: 1px solid #bbb;
  }
  dt {
    font-weight: 700;
  }
  dd {
    margin: 0;
    overflow-wrap: anywhere;
  }
  @media (min-width: 32rem) {
    dl > div {
      grid-template-columns: minmax(5rem, 1fr) 3fr;
    }
  }
</style>
<h2>Build a compound shelf index</h2>
<dl class="" id="result"></dl>
<script>
  const rows = [
    { sku: "pen", bin: "A", qty: 4 },
    { sku: "pad", bin: "B", qty: 0 },
    { sku: "pen", bin: "B", qty: 3 },
    { sku: "clip", bin: "A", qty: 9 },
  ];
  const result = Object.fromEntries(
    rows.map((r) => [JSON.stringify([r.bin, r.sku]), r.qty]),
  );
  const list = document.querySelector("#result");
  list.replaceChildren();
  for (const [key, value] of Object.entries(
    typeof result === "object" && result !== null ? result : { value: result },
  )) {
    const row = document.createElement("div");
    const label = document.createElement("dt");
    label.textContent = key;
    const detail = document.createElement("dd");
    detail.textContent =
      typeof value === "string" ? value : JSON.stringify(value);
    row.append(label, detail);
    list.append(row);
  }
  if (!list.children.length) list.after("No matching entries.");
</script>