ProGridSign in

Examples

Saving layout and filters

Column widths, order, visibility, grouping, sort, filters and page size saved to localStorage, sessionStorage or your own store, and restored safely even after the columns change.

Live demo

Saving layout and filters

Resize and reorder columns · Reload the page · Hide a column, then clear the saved layout

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

Things to try

  • Resize and reorder columns
  • Reload the page
  • Hide a column, then clear the saved layout

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 Saving layout and filters.

Use ProGrid in your project