ProGridSign in

Features

Saving layout and filters

Persist column widths, order, visibility, grouping, aggregations, sort, filters, and page size to localStorage, sessionStorage, or your own store — and restore them safely, even after the columns themselves have changed.

Choose what to keep

Persistence is off until persist.key is given — nothing is ever stored by accident. local and session each take a list of what to keep there, or true for a sensible default set (columns, sort, filters, grouping, column aggregations, page size, and pivot layout).

<ProGrid
  columns={columns}
  data={orders}
  getRowId={(order) => order.id}
  persist={{
    key: "orders-grid",
    local: ["columns", "sort", "filters", "group"],
    session: ["pagination"],
  }}
/>

This is the reader’s current layout. For named layouts the reader switches between, including ones that leave their filters alone, see Saved views.

Live demo

A layout that survives a reload

Drag a header, resize a column, sort, filter, group by region, or change the page size — then reload this page. Everything comes back exactly as you left it.

import { useRef, useState } from "react"
import { ProGrid, type ProColumnDef, type ProGridHandle } 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)

/**
 * Reorder, resize, hide, sort, filter, group, or change page size, then reload
 * this page — the layout comes back exactly as it was left. `persist` is the
 * only new prop; nothing else about the columns or data changes.
 */
export function PersistenceDocsDemo() {
  const gridRef = useRef<ProGridHandle<OperationOrder>>(null)
  const [clearedAt, setClearedAt] = useState<number | null>(null)

  return (
    <>
      <div className="docs-demo__status" aria-live="polite">
        <span>
          {clearedAt
            ? "Saved layout cleared — reload to see the defaults."
            : "Change anything, then reload this page."}
        </span>
        <button
          type="button"
          className="underline"
          onClick={() => {
            void gridRef.current?.clearPersistedState()
            setClearedAt(Date.now())
          }}
        >
          Clear saved layout
        </button>
      </div>
      <ProGrid<OperationOrder>
        ref={gridRef}
        columns={baseColumns}
        data={groupingDemoRows}
        getRowId={(row) => row.id}
        height={390}
        pagination={{ pageSize: 8, pageSizeOptions: [8, 16, 24] }}
        persist={{
          key: "docs-saved-layout",
          local: ["columns", "sort", "filters", "group"],
          session: ["pagination"],
        }}
        title="Orders queue"
      />
    </>
  )
}
Change anything, then reload this page.

Orders queue

Order
Customer
Region
Status
Units
Revenue
Total
Sum13230
Sum$2,811,630
60 rows
Rows per page
1–8 of 60

Groups, or individual slices

A group is the set of slices that only ever move together — nobody saves a column’s width without its position. Name individual slices instead for the rarer grid that wants only one piece of a group; groups and slices compose freely in the same array.

type PersistGroup =
  | "columns" | "sort" | "filters" | "group" | "aggregations" | "pagination" | "selection" | "pivot"
//   columns      → columnOrder + columnVisibility + columnSizing + columnPinning
//   sort         → sorting
//   filters      → columnFilters + quickFilter
//   group        → grouping
//   aggregations → the aggregation picked in each column's menu
//   pagination   → pageSize (not pageIndex — see below)
//   selection    → rowSelection (never included by "true" — see below)
//   pivot        → pivotConfig + pivotExpansion

// Groups and individual slices compose, so the rare "widths but not order" case
// still fits in one array:
persist={{ key: "orders-grid", local: ["columnVisibility", "columnSizing", "sort"] }}

A custom store

adapter takes the place of local/session for whichever slices you name in slices — a database, an API, anything. Async is fine; the grid restores whenever the promise resolves, holding the default layout until then.

import type { GridStateAdapter } from "@/components/pro-grid"

const dbAdapter: GridStateAdapter = {
  async load(key) {
    const res = await fetch(`/api/grid-views/${key}`)
    return res.ok ? res.json() : null // a GridPersistPayload, or null
  },
  async save(key, payload) {
    await fetch(`/api/grid-views/${key}`, {
      method: "PUT",
      body: JSON.stringify(payload),
    })
  },
  async clear(key) {
    await fetch(`/api/grid-views/${key}`, { method: "DELETE" })
  },
}

<ProGrid
  columns={columns}
  data={orders}
  persist={{ key: "orders-grid", adapter: dbAdapter, slices: ["columns", "filters"] }}
/>

load failing or returning null just means “use the defaults” — it never blocks the grid and never shows an error to the reader. Layout is a preference, not data whose loss should be loud; a discarded save looks like a freshly opened grid, one drag from fixed.

Versioning and failures

Bump version when a column is renamed or restructured enough that an old save no longer makes sense — every saved layout under that key is discarded rather than applied against columns it wasn’t written for. A saved filter is additionally checked against the column’s current type: if a column’s type changed, that one filter is dropped even though the rest of the layout still applies.

persist={{
  key: "orders-grid",
  local: true, // the default group set
  version: 2,  // bump when a column is renamed or restructured — discards old saves
  onError: (error) => reportToSentry(error), // load/save/clear failures; never thrown
}}

Reading and resetting state

The handle can read the same shape it would otherwise only report through onStateChange, and reset layout independently of interaction state.

const gridRef = useRef<ProGridHandle<Order>>(null)

gridRef.current.getState()            // the full state onStateChange would report
gridRef.current.resetLayout()         // columns back to initialState
gridRef.current.reset()               // sort, filters, quick filter, page
await gridRef.current.clearPersistedState() // both of the above, plus erase storage