gridcn2.0.0
Recipes and custom code

Remember filters and layout

Save sort, filters, search, and column layout in the browser, and restore them on the next visit.

Four default props seed the view once: defaultSortState, defaultFilterState, defaultSearchText, and defaultColumnLayout. Each one has a change callback. Save from the callbacks and seed from the saved value.

Before you start

  • Pick a place to store the view. The sample uses localStorage.
  • Give every column a stable id. A saved sort, filter, or layout names columns by id.

Steps

  1. Pass a function to each of the four default props. It reads the saved part.
  2. Save the new value in each of the four callbacks.

Save and restore the view

The sample merges every callback into one stored object. ./orders stands for your own module of columns and orders rows:

"use client";

import {
  DataGrid,
  type ColumnLayout,
  type FilterNode,
  type SortSpec,
} from "@/components/data-grid/data-grid";
import { columns, orders } from "./orders";

type SavedView = {
  sort: SortSpec[];
  filters: FilterNode[];
  search: string;
  layout: ColumnLayout;
};

const VIEW_KEY = "orders-view";

function readView(): Partial<SavedView> {
  try {
    return JSON.parse(localStorage.getItem(VIEW_KEY) ?? "{}");
  } catch {
    return {};
  }
}

function saveView(patch: Partial<SavedView>) {
  const next = { ...readView(), ...patch };
  localStorage.setItem(VIEW_KEY, JSON.stringify(next));
}

export function OrdersGrid() {
  return (
    <DataGrid
      className="h-96"
      defaultData={orders}
      columns={columns}
      defaultSortState={() => readView().sort}
      defaultFilterState={() => readView().filters}
      defaultSearchText={() => readView().search}
      defaultColumnLayout={() => readView().layout}
      onSortChange={(sort) => saveView({ sort })}
      onFilterChange={(filters) => saveView({ filters })}
      onSearchTextChange={(search) => saveView({ search })}
      onColumnLayoutChange={(layout) => saveView({ layout })}
    />
  );
}

Result

A reload restores the sort, the filters, the search text, and the width, order, pins, and hidden state of the columns. An undefined result leaves that part at its normal start. The grid reads each default once at mount and ignores later changes. A function seed runs once on the client after mount, so the server render and the first client render match.

The grid drops a saved layout id that names no column. It drops a saved sort or filter that names no column too, and warns once in development. A new column joins at the end, in the order of columns.

A view saved before gridcn 1.3.0 can hold a joinOperator. Upgrade gridcn shows how to read it.

The saved views add-on saves several named views with a picker. To share a view as a link, use URL state. To save the view for each user on a server, send the same patches from the callbacks to your API.

Server mode

A grid fed by useDataGridServerRows takes its sort, filters, and search from the hook. The grid ignores the three default props and their callbacks there. Seed the hook with defaultSpec, and save from its onSpecChange. Pass a function to defaultSpec to read the saved spec. It runs once on the client after mount and returns the spec or undefined. The first request waits for it. The column layout stays browser state: seed it with defaultColumnLayout and save it from onColumnLayoutChange.

On this page