ProGridSign in

Examples

Grouping and aggregation

Group flat rows by one or more columns and roll numeric measures up with sum, average, min, max or count — from column metadata alone, with no pre-nested data.

Live demo

Grouping and aggregation

Drag a header into the group bar · Reorder the group chips · Expand and collapse a branch · Change an aggregation from the column menu

import { ProGrid, type ProColumnDef } from "@/components/pro-grid"
import { formatCurrency, operationOrders, type OperationOrder } from "@/lib/operations-data"

function StatusCell({ value }: { value: unknown }) {
  const status = String(value)
  return <span className={`order-status order-status--${status.toLowerCase().replaceAll(" ", "-")}`}>{status}</span>
}

const baseColumns: ProColumnDef<OperationOrder>[] = [
  { accessorKey: "id", header: "Order", size: 118, meta: { label: "Order", type: "string" } },
  { accessorKey: "customer", header: "Customer", size: 190, meta: { label: "Customer", type: "string" } },
  {
    accessorKey: "region",
    header: "Region",
    size: 145,
    meta: {
      label: "Region",
      type: "string", filterVariant: "set",
      filterOptions: ["North America", "Europe", "Asia Pacific", "Latin America"],
      groupable: true,
      pivotable: true,
    },
  },
  {
    accessorKey: "status",
    header: "Status",
    size: 120,
    cell: ({ getValue }) => <StatusCell value={getValue()} />,
    meta: {
      label: "Status",
      type: "string", filterVariant: "set",
      filterOptions: ["On track", "At risk", "Delayed", "Complete"],
      groupable: true,
      pivotable: true,
    },
  },
  {
    accessorKey: "units",
    header: "Units",
    size: 88,
    meta: { label: "Units", type: "number", align: "right", aggregable: true, defaultAgg: "sum" },
  },
  {
    accessorKey: "revenue",
    header: "Revenue",
    size: 128,
    meta: {
      label: "Revenue",
      type: "number",
      align: "right",
      aggregable: true,
      defaultAgg: "sum",
      formatValue: (value) => formatCurrency(Number(value)),
    },
  },
]

const groupingDemoRows = operationOrders.slice(0, 60)

export function GroupingDocsDemo() {
  return (
    <ProGrid<OperationOrder>
      columns={baseColumns}
      data={groupingDemoRows}
      getRowId={(row) => row.id}
      height={390}
      initialGrouping={["region", "status"]}
      initialState={{ columnVisibility: { id: false, customer: false } }}
      title="Orders by region and status"
      toolbar={{ quickFilter: false, pivot: false, chart: false, refresh: false }}
    />
  )
}

Orders by region and status

Row GroupsRegionStatus
Region
Status
Units
Revenue
Total
Sum13230
Sum$2,811,630
60 rows

Things to try

  • Drag a header into the group bar
  • Reorder the group chips
  • Expand and collapse a branch
  • Change an aggregation from the column menu

The Code tab above is the component rendering this page; the Data tab is what reaches the grid. The props behind it are explained in Grouping and aggregation.

Use ProGrid in your project