Master / detail (nested grids)

Master/detail is the AG-Grid pattern where each master row expands to reveal a detail panel - most often a nested grid of the row's child records. SvGrid builds it from two props on <SvGrid>:

Pattern

Keep an expanded set, and splice a "detail" sentinel row into the data right after each expanded master row. isDetailRow recognises the sentinels; renderDetailRow renders the child grid.

<script lang="ts">
  type Row = Account | { kind: 'detail'; parentId: string; children: Call[] }

  let expanded = $state(new Set<string>())
  const toggle = (id: string) => {
    const next = new Set(expanded)
    next.has(id) ? next.delete(id) : next.add(id)
    expanded = next
  }

  // Master rows + a detail sentinel after each expanded one.
  const visible = $derived.by(() => {
    const out: Row[] = []
    for (const a of accounts) {
      out.push(a)
      if (expanded.has(a.id)) out.push({ kind: 'detail', parentId: a.id, children: a.callRecords })
    }
    return out
  })
</script>

{#snippet DetailGrid(p: { row: Row })}
  {#if p.row.kind === 'detail'}
    <div style="height: 200px;">
      <SvGrid data={p.row.children} columns={detailColumns} containerHeight="100%" />
    </div>
  {/if}
{/snippet}

<SvGrid
  data={visible}
  {columns}
  isDetailRow={(row) => row.kind === 'detail'}
  renderDetailRow={DetailGrid}
/>

The first master column typically renders a chevron whose click calls toggle(row.id).

Notes

See also