AdaptTable for Chakra

Pivot tables in Chakra

Grouping answers “what is the total per team”. A pivot answers “what is the total per team per status”, and that second dimension becomes columns your data never had.

Fields move between the three zones with buttons rather than drag, so the pivot can be built from the keyboard. Subtotals close every group and a grand total closes the table.

The configuration — axes, measures, aggregation and what you folded — lives in the URL, so a pivot you build is a pivot you can send someone.

Each zone is a Stack rendered as a fieldset with a Text legend — Chakra's reset strips the browser's own frame, so the border is the kit's — and the moves are xs outline Buttons beside NativeSelect pickers.

The code

import { DataTable, PivotPanel } from "@adapttable/chakra";
import { usePivotUrlState } from "@adapttable/core/pivot";

export function Spend({ rows, fields }) {
  const pivot = usePivotUrlState({
    urlKey: "p",
    defaultConfig: {
      rows: ["team"],
      columns: ["status"],
      measures: [{ key: "budget", agg: "sum" }],
    },
  });
  return (
    <>
      <PivotPanel fields={fields} {...pivot} />
      <DataTable
        data={rows}
        columns={pivot.columns}
        rowKey={(row) => row.id}
      />
    </>
  );
}

Install

pnpm add @adapttable/chakra @adapttable/core @chakra-ui/react @emotion/react

The same feature in the other kits

More Chakra features

Reference: pivot. More of this kit: AdaptTable for Chakra, or the live demo.