Skip to content
Product block

Grouped Table with Subtotals

Collapsible group headers that carry their own subtotals while collapsed, with a grand total that sums every row rather than only the visible ones.

238 lineslucide-reactAdded 31 Aug 2026Updated 10 Sept 2026
  • table
  • grouping
  • subtotals
  • collapsible
  • reporting

What's included

  • components/data-table-grouped-rows.tsx
  • Needs lucide-react

Works with

  • React
  • Next.js
  • Tailwind CSS
  • TypeScript

npx hoverlab add data-table-grouped-rows

Or over MCP, from your editor's agent — no account needed.

License

Free to read, copy and install, for personal and non-commercial projects. Shipping it in client work or a paid product needs Pro ($79 once). The source lands in your repo and stops being ours — no attribution, nothing to upgrade.

Was this useful?

Start a page with this section — add more, order them, and leave with the page source.

Preview

Accounts by segment

3 segments · 8 accounts

AccountOwnerSeatsARR
516$619,200
Acme CorpR. Patel240$288,000
InitechS. Okafor180$216,000
UmbrellaR. Patel96$115,200
105$100,800
GlobexJ. Lindqvist48$46,080
SoylentS. Okafor32$30,720
HooliJ. Lindqvist25$24,000
15$12,960
All segmentsincluding collapsed636$732,960

Rendered live in your current theme — this is the same component whose source is below, not a screenshot of it.

Source

components/data-table-grouped-rows.tsx
'use client'

/**
 * <DataTableGroupedRows> — rows collapsed under a group header that carries
 * its own subtotals.
 *
 * The moment a table has a categorical column people stop reading rows and
 * start reading categories. Sorting by that column gets them half way and
 * then leaves them counting by eye; grouping is the other half.
 *
 * THE SUBTOTAL IS THE FEATURE
 *
 * A group header that only says "Enterprise (7)" saves a scroll and answers
 * nothing. The number people came for is the sum, and it has to be on the
 * header while the group is *collapsed* — a subtotal you can only see by
 * expanding the thing you collapsed is decoration.
 *
 * A GRAND TOTAL THAT DOES NOT LIE
 *
 * `<tfoot>` sums every row, not the expanded ones, and says so. Tables that
 * total the visible rows produce a footer that changes when you collapse a
 * group, which reads as a bug even when it is a choice.
 *
 * WHY `<tbody>` PER GROUP. HTML allows many bodies in one table and this is
 * exactly what they are for: the group header is a row inside its own body,
 * so collapsing hides a body rather than filtering a flat list, and the
 * column widths stay put instead of reflowing as groups open and close.
 *
 * ACCESSIBILITY: each header row's button carries `aria-expanded` and
 * `aria-controls` pointing at its `<tbody>` — the pairing the a11y audit
 * flags when only half of it is present. Counts and totals are inside the
 * button's accessible name, so a screen reader hears the summary without
 * having to expand the group to reach it.
 */

import * as React from 'react'
import { ChevronRight } from 'lucide-react'

export interface GroupedRow {
  id: string
  name: string
  owner: string
  seats: number
  arr: number
}

export interface RowGroup {
  id: string
  label: string
  rows: GroupedRow[]
}

export interface DataTableGroupedRowsProps {
  groups?: RowGroup[]
  /** Groups collapsed on first render. */
  initialCollapsed?: string[]
  className?: string
}

const DEFAULT_GROUPS: RowGroup[] = [
  {
    id: 'enterprise',
    label: 'Enterprise',
    rows: [
      { id: 'acme', name: 'Acme Corp', owner: 'R. Patel', seats: 240, arr: 288_000 },
      { id: 'initech', name: 'Initech', owner: 'S. Okafor', seats: 180, arr: 216_000 },
      { id: 'umbrella', name: 'Umbrella', owner: 'R. Patel', seats: 96, arr: 115_200 },
    ],
  },
  {
    id: 'mid-market',
    label: 'Mid-market',
    rows: [
      { id: 'globex', name: 'Globex', owner: 'J. Lindqvist', seats: 48, arr: 46_080 },
      { id: 'soylent', name: 'Soylent', owner: 'S. Okafor', seats: 32, arr: 30_720 },
      { id: 'hooli', name: 'Hooli', owner: 'J. Lindqvist', seats: 25, arr: 24_000 },
    ],
  },
  {
    id: 'smb',
    label: 'Small business',
    rows: [
      { id: 'vehement', name: 'Vehement Capital', owner: 'A. Moreau', seats: 9, arr: 7_776 },
      { id: 'massive', name: 'Massive Dynamic', owner: 'A. Moreau', seats: 6, arr: 5_184 },
    ],
  },
]

const money = (value: number) =>
  value.toLocaleString('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0 })

function subtotal(rows: GroupedRow[]) {
  return rows.reduce(
    (acc, row) => ({ seats: acc.seats + row.seats, arr: acc.arr + row.arr }),
    { seats: 0, arr: 0 },
  )
}

export function DataTableGroupedRows({
  groups = DEFAULT_GROUPS,
  initialCollapsed = ['smb'],
  className = '',
}: DataTableGroupedRowsProps) {
  const [collapsed, setCollapsed] = React.useState<string[]>(initialCollapsed)

  const toggle = (id: string) =>
    setCollapsed((current) =>
      current.includes(id) ? current.filter((value) => value !== id) : [...current, id],
    )

  const grand = subtotal(groups.flatMap((group) => group.rows))

  return (
    <section
      className={`overflow-hidden rounded-2xl border border-border bg-card text-card-foreground ${className}`}
    >
      <header className="flex flex-wrap items-center justify-between gap-3 border-b border-border p-5 sm:p-6">
        <div>
          <h2 className="text-base font-semibold">Accounts by segment</h2>
          <p className="mt-1 text-sm text-muted-foreground">
            {groups.length} segments · {groups.flatMap((g) => g.rows).length} accounts
          </p>
        </div>
        <button
          type="button"
          onClick={() => setCollapsed(collapsed.length ? [] : groups.map((g) => g.id))}
          className="rounded-lg border border-border px-3 py-1.5 text-sm font-medium text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
        >
          {collapsed.length ? 'Expand all' : 'Collapse all'}
        </button>
      </header>

      <div className="relative overflow-x-auto">
        <table className="w-full min-w-[40rem] border-collapse text-start text-sm">
          <thead>
            <tr className="border-b border-border">
              <th scope="col" className="px-5 py-2.5 font-semibold sm:px-6">
                Account
              </th>
              <th scope="col" className="px-5 py-2.5 font-semibold">
                Owner
              </th>
              <th scope="col" className="px-5 py-2.5 text-end font-semibold">
                Seats
              </th>
              <th scope="col" className="px-5 py-2.5 text-end font-semibold">
                ARR
              </th>
            </tr>
          </thead>

          {groups.map((group) => {
            const open = !collapsed.includes(group.id)
            const totals = subtotal(group.rows)
            const bodyId = `group-${group.id}`

            return (
              <tbody key={group.id} id={bodyId} className="border-b border-border last:border-0">
                <tr className="bg-muted/50">
                  <th scope="rowgroup" colSpan={2} className="px-5 py-2.5 sm:px-6">
                    <button
                      type="button"
                      onClick={() => toggle(group.id)}
                      aria-expanded={open}
                      aria-controls={bodyId}
                      className="inline-flex items-center gap-2 text-sm font-semibold transition-colors hover:text-primary focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
                    >
                      <ChevronRight
                        aria-hidden
                        className={`h-4 w-4 text-muted-foreground transition-transform ${
                          // Mirrored only while closed; down is down in RTL.
                          open ? 'rotate-90' : 'rtl:rotate-180'
                        }`}
                      />
                      {group.label}
                      <span className="font-normal text-muted-foreground">
                        ({group.rows.length})
                      </span>
                      {/*
                        Inside the button's name on purpose: a screen reader
                        user should hear the subtotal without expanding the
                        group to find it.
                      */}
                      <span className="sr-only">
                        — {totals.seats} seats, {money(totals.arr)} ARR
                      </span>
                    </button>
                  </th>
                  <td className="px-5 py-2.5 text-end font-semibold tabular-nums">
                    {totals.seats.toLocaleString('en-US')}
                  </td>
                  <td className="px-5 py-2.5 text-end font-semibold tabular-nums">
                    {money(totals.arr)}
                  </td>
                </tr>

                {open
                  ? group.rows.map((row) => (
                      <tr key={row.id} className="border-t border-border/50">
                        <th scope="row" className="px-5 py-3 ps-11 font-medium sm:px-6 sm:ps-12">
                          {row.name}
                        </th>
                        <td className="px-5 py-3 text-muted-foreground">{row.owner}</td>
                        <td className="px-5 py-3 text-end tabular-nums text-muted-foreground">
                          {row.seats.toLocaleString('en-US')}
                        </td>
                        <td className="px-5 py-3 text-end tabular-nums text-muted-foreground">
                          {money(row.arr)}
                        </td>
                      </tr>
                    ))
                  : null}
              </tbody>
            )
          })}

          <tfoot>
            <tr className="border-t-2 border-border bg-muted/30">
              <th scope="row" colSpan={2} className="px-5 py-3 font-semibold sm:px-6">
                All segments
                <span className="ms-2 font-normal text-muted-foreground">
                  including collapsed
                </span>
              </th>
              <td className="px-5 py-3 text-end font-semibold tabular-nums">
                {grand.seats.toLocaleString('en-US')}
              </td>
              <td className="px-5 py-3 text-end font-semibold tabular-nums">
                {money(grand.arr)}
              </td>
            </tr>
          </tfoot>
        </table>
      </div>
    </section>
  )
}

Before you paste

  • Styling is Tailwind utility classes on semantic tokens (bg-card, text-muted-foreground) — it inherits your theme instead of overriding it.
  • Install: npm i lucide-react
  • Every prop has a default, so it renders standalone before you wire it up.

Where it goes

Drop it at components/data-table-grouped-rows.tsx and import it where you need the section:

import { DataTableGroupedRows } from '@/components/data-table-grouped-rows'

Props

Read out of the component’s own type and signature, so this cannot drift from the source below. Every prop has a default — the component renders standalone before you pass it anything.

PropTypeDefault
groupsRowGroup[]DEFAULT_GROUPS
initialCollapsedGroups collapsed on first render.string[]['smb']
classNamestring''

Not using React?

The same block rendered once to markup, wrapped as a file your framework compiles. Tailwind classes are framework-agnostic, so the design transfers intact — the behaviour does not.

This block is interactive. The markup below is its initial state with the event handlers stripped — you will need to re-wire the behaviour in your framework.

data-table-grouped-rows.html
<!--
  Grouped Table with Subtotals — markup from the Hoverlab catalog.

  This is the block rendered once to HTML and wrapped as a component
  file. It is not a port of the React source: the Tailwind classes carry
  the design, which is the part that took the work, and they are the same
  in every framework.

  This block is interactive in React and the handlers are NOT here.
  Buttons, toggles and menus render in their initial state and do
  nothing until you wire them up.
-->
<section class="overflow-hidden rounded-2xl border border-border bg-card text-card-foreground ">
  <header class="flex flex-wrap items-center justify-between gap-3 border-b border-border p-5 sm:p-6">
    <div>
      <h2 class="text-base font-semibold">Accounts by segment</h2>
      <p class="mt-1 text-sm text-muted-foreground">3 segments · 8 accounts</p>
    </div>
    <button type="button" class="rounded-lg border border-border px-3 py-1.5 text-sm font-medium text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring">Expand all</button>
  </header>
  <div class="relative overflow-x-auto">
    <table class="w-full min-w-[40rem] border-collapse text-start text-sm">
      <thead>
        <tr class="border-b border-border">
          <th scope="col" class="px-5 py-2.5 font-semibold sm:px-6">Account</th>
          <th scope="col" class="px-5 py-2.5 font-semibold">Owner</th>
          <th scope="col" class="px-5 py-2.5 text-end font-semibold">Seats</th>
          <th scope="col" class="px-5 py-2.5 text-end font-semibold">ARR</th>
        </tr>
      </thead>
      <tbody id="group-enterprise" class="border-b border-border last:border-0">
        <tr class="bg-muted/50">
          <th scope="rowgroup" colSpan="2" class="px-5 py-2.5 sm:px-6">
            <button type="button" aria-expanded="true" aria-controls="group-enterprise" class="inline-flex items-center gap-2 text-sm font-semibold transition-colors hover:text-primary focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-chevron-right h-4 w-4 text-muted-foreground transition-transform rotate-90" aria-hidden="true"><path d="m9 18 6-6-6-6"></path></svg>Enterprise<span class="font-normal text-muted-foreground">(3)</span><span class="sr-only">— 516 seats, $619,200 ARR</span></button>
          </th>
          <td class="px-5 py-2.5 text-end font-semibold tabular-nums">516</td>
          <td class="px-5 py-2.5 text-end font-semibold tabular-nums">$619,200</td>
        </tr>
        <tr class="border-t border-border/50">
          <th scope="row" class="px-5 py-3 ps-11 font-medium sm:px-6 sm:ps-12">Acme Corp</th>
          <td class="px-5 py-3 text-muted-foreground">R. Patel</td>
          <td class="px-5 py-3 text-end tabular-nums text-muted-foreground">240</td>
          <td class="px-5 py-3 text-end tabular-nums text-muted-foreground">$288,000</td>
        </tr>
        <tr class="border-t border-border/50">
          <th scope="row" class="px-5 py-3 ps-11 font-medium sm:px-6 sm:ps-12">Initech</th>
          <td class="px-5 py-3 text-muted-foreground">S. Okafor</td>
          <td class="px-5 py-3 text-end tabular-nums text-muted-foreground">180</td>
          <td class="px-5 py-3 text-end tabular-nums text-muted-foreground">$216,000</td>
        </tr>
        <tr class="border-t border-border/50">
          <th scope="row" class="px-5 py-3 ps-11 font-medium sm:px-6 sm:ps-12">Umbrella</th>
          <td class="px-5 py-3 text-muted-foreground">R. Patel</td>
          <td class="px-5 py-3 text-end tabular-nums text-muted-foreground">96</td>
          <td class="px-5 py-3 text-end tabular-nums text-muted-foreground">$115,200</td>
        </tr>
      </tbody>
      <tbody id="group-mid-market" class="border-b border-border last:border-0">
        <tr class="bg-muted/50">
          <th scope="rowgroup" colSpan="2" class="px-5 py-2.5 sm:px-6">
            <button type="button" aria-expanded="true" aria-controls="group-mid-market" class="inline-flex items-center gap-2 text-sm font-semibold transition-colors hover:text-primary focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-chevron-right h-4 w-4 text-muted-foreground transition-transform rotate-90" aria-hidden="true"><path d="m9 18 6-6-6-6"></path></svg>Mid-market<span class="font-normal text-muted-foreground">(3)</span><span class="sr-only">— 105 seats, $100,800 ARR</span></button>
          </th>
          <td class="px-5 py-2.5 text-end font-semibold tabular-nums">105</td>
          <td class="px-5 py-2.5 text-end font-semibold tabular-nums">$100,800</td>
        </tr>
        <tr class="border-t border-border/50">
          <th scope="row" class="px-5 py-3 ps-11 font-medium sm:px-6 sm:ps-12">Globex</th>
          <td class="px-5 py-3 text-muted-foreground">J. Lindqvist</td>
          <td class="px-5 py-3 text-end tabular-nums text-muted-foreground">48</td>
          <td class="px-5 py-3 text-end tabular-nums text-muted-foreground">$46,080</td>
        </tr>
        <tr class="border-t border-border/50">
          <th scope="row" class="px-5 py-3 ps-11 font-medium sm:px-6 sm:ps-12">Soylent</th>
          <td class="px-5 py-3 text-muted-foreground">S. Okafor</td>
          <td class="px-5 py-3 text-end tabular-nums text-muted-foreground">32</td>
          <td class="px-5 py-3 text-end tabular-nums text-muted-foreground">$30,720</td>
        </tr>
        <tr class="border-t border-border/50">
          <th scope="row" class="px-5 py-3 ps-11 font-medium sm:px-6 sm:ps-12">Hooli</th>
          <td class="px-5 py-3 text-muted-foreground">J. Lindqvist</td>
          <td class="px-5 py-3 text-end tabular-nums text-muted-foreground">25</td>
          <td class="px-5 py-3 text-end tabular-nums text-muted-foreground">$24,000</td>
        </tr>
      </tbody>
      <tbody id="group-smb" class="border-b border-border last:border-0">
        <tr class="bg-muted/50">
          <th scope="rowgroup" colSpan="2" class="px-5 py-2.5 sm:px-6">
            <button type="button" aria-expanded="false" aria-controls="group-smb" class="inline-flex items-center gap-2 text-sm font-semibold transition-colors hover:text-primary focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-chevron-right h-4 w-4 text-muted-foreground transition-transform rtl:rotate-180" aria-hidden="true"><path d="m9 18 6-6-6-6"></path></svg>Small business<span class="font-normal text-muted-foreground">(2)</span><span class="sr-only">— 15 seats, $12,960 ARR</span></button>
          </th>
          <td class="px-5 py-2.5 text-end font-semibold tabular-nums">15</td>
          <td class="px-5 py-2.5 text-end font-semibold tabular-nums">$12,960</td>
        </tr>
      </tbody>
      <tfoot>
        <tr class="border-t-2 border-border bg-muted/30">
          <th scope="row" colSpan="2" class="px-5 py-3 font-semibold sm:px-6">All segments<span class="ms-2 font-normal text-muted-foreground">including collapsed</span></th>
          <td class="px-5 py-3 text-end font-semibold tabular-nums">636</td>
          <td class="px-5 py-3 text-end font-semibold tabular-nums">$732,960</td>
        </tr>
      </tfoot>
    </table>
  </div>
</section>
  • This is rendered HTML, not a translation of the React source. The Tailwind classes carry the design and work in any framework.
  • It is one frame: the component in its initial state, with no props applied beyond the defaults.
  • This block is interactive in React — toggles, menus or form state. None of that survives here; the markup is the closed/default state and the handlers are gone. Re-wire them in your own framework.
  • Requires Tailwind, and the design tokens the classes reference (bg-card, text-muted-foreground, and so on). The template ZIPs ship a globals.css that defines them.

What each framework gets across the whole catalog — effects convert properly; this rung is markup.

For AI

The component, its props, the design tokens it expects and the command that installs it — as one prompt. Paste it into Claude, Cursor, v0 or ChatGPT and what they build around it will match the rest of the catalog instead of inventing its own system.

See the prompt

Used in these pages

Want the whole screen instead of this one section? Open a page and copy it entire.

7 more blocks in Data Tables

All of them free to read, copy and install — no account, no locked tiles, no watermarked preview. The whole catalog is open, and so are the API and the CLI.

Browse Data Tables

Shipping one commercially

Copying the code is free. Putting it in client work or a paid product is what Pro is for — the licence, not the access.

  • A commercial licence for everything in the catalog
  • Unlimited bundle exports, in Vue, Svelte and Tailwind
  • One payment — no subscription, nothing to renew
Pro — $79 once

More Data Tables blocks

View category
Open the full page for this block

Sortable Selectable Table

Type-aware sorting with aria-sort, a header checkbox that goes properly indeterminate, and selection that survives a re-sort.

Data Tables297 lines1 dep
Open the full page for this block

Table Search & Filter Bar

Search, filter count and view switch, with active filters shown as removable chips so a filtered table never looks unfiltered.

Data Tables180 lines1 dep
Open the full page for this block

Expandable Detail Rows

Rows that open into a full-width detail panel via colSpan, with aria-expanded and aria-controls wired to the detail row.

Data Tables205 lines1 dep
Open the full page for this block

Column Show, Hide & Reorder

Reordering that works from the keyboard rather than by drag alone, pinned columns that cannot be hidden or moved, and a guard so the last visible column cannot be unchecked into a blank table.

Data Tables219 lines1 dep
Open the full page for this block

Bulk Action Bar

The selection bar that asks the question most tables skip: this page, or all 1,284 rows the filter matches. Typed confirmation above a threshold, an undo with the window counting down, and a select-all that names its own scope.

Data Tables405 lines1 dep
Open the full page for this block

Inline Editable Table

Edit cells in the grid with staged changes, per-cell validation, the original value kept beside the attempt, and a save that counts what is still pending.

Data Tables301 lines1 dep