Skip to content

Data Grid state schema

Describes a Data Grid’s view state for tools, such as an AI assistant that changes the view on request:

  • dataGridStateSchema(grid): a JSON Schema (draft 2020-12) of the state setState accepts, with this grid’s column ids, filter kinds, groupable columns, and pivot aggregates.
  • describeState(grid): a short English summary of the current view.
  • applyState(grid, state): validates and applies a proposed state; returns { ok: true } or { ok: false, error } so the tool can correct its proposal.
import { DataGrid } from '@absoluteui/components/data-grid';
import { applyState, dataGridStateSchema, describeState } from '@absoluteui/components/data-grid/schema';
interface Person {
id: string;
name: string;
age: number;
}
const host = document.createElement('div');
host.style.setProperty('block-size', '200px');
document.body.append(host);
const grid = DataGrid.create<Person>(host, {
label: 'People',
rows: [{ id: '1', name: 'Ada', age: 36 }],
rowId: (r) => r.id,
columns: [
{ id: 'name', header: 'Name', field: 'name' },
{ id: 'age', header: 'Age', field: 'age', type: 'number' },
],
});
console.log(dataGridStateSchema(grid), describeState(grid));
const result = applyState(grid, { sort: [{ column: 'age', direction: 'descending' }] });
if (!result.ok) console.log(result.error);
  • Only view state: sort, columns, paging, filters, quick and advanced filters, grouping, and the pivot. No row data, no edits, no row ids: the schema and summary are safe to send to a service that should not see the data.
  • Treat the tool’s proposal as untrusted input: applyState validates it like setState and changes nothing when it is invalid.

applyState

Apply a proposed view state; returns the validation message instead of throwing (for tool loops).

function applyState<Row>(grid: DataGrid<Row>, state: unknown): {
readonly ok: true;
} | {
readonly ok: false;
readonly error: string;
};

dataGridStateSchema

A JSON Schema of the view state setState accepts for this grid (draft 2020-12): sort, columns (order, widths, pinning, visibility, collapsed groups, header names), paging, column filters by their kinds, the quick filter, grouping and expansion, master/detail expansion, and the pivot. Column ids are enumerated; the description lists each column’s header and type.

function dataGridStateSchema<Row>(grid: DataGrid<Row>): JsonSchema;

describeState

A short English description of the current view (for a tool’s context, not for display).

function describeState<Row>(grid: DataGrid<Row>): string;

JsonSchema

A JSON Schema object (plain JSON).

type JsonSchema = Readonly<Record<string, unknown>>;