Column Definitions
columnDefs defines the schema, data extraction, formatting, and capabilities of each leaf column.
import { Grid, type ColumnDef } from 'next-grid-js';import { createOrders, type OrderRow } from './data/orders';import { syncGridTheme } from './theme';import type { DemoMountContext } from './types';
export function mount({ stage }: DemoMountContext): () => void { const columnDefs: readonly ColumnDef<OrderRow>[] = [ { key: 'id', name: 'ID', width: 96, pinned: 'start', }, { key: 'customer', name: 'Customer', width: 175, flex: 1, }, { key: 'quantity', name: 'Qty', width: 78, cellDataType: 'number', }, { key: 'price', name: 'Unit Price', width: 105, cellDataType: 'number', formatter: ({ value }) => `$${Number(value).toFixed(2)}`, }, { colId: 'lineTotal', name: 'Computed Total', width: 135, cellDataType: 'number', valueGetter: ({ data }) => data.quantity * data.price, formatter: ({ value }) => `$${Number(value).toLocaleString('en-US')}`, }, { key: 'orderedAt', name: 'Order Date', width: 120, cellDataType: 'date', }, ];
const grid = new Grid<OrderRow>({ container: stage, defaultColDef: { isSortable: true, isResizable: true, }, columnDefs, rowData: createOrders(30), getRowId: (row) => row.id, }); const stopTheme = syncGridTheme(stage);
return () => { grid.destroy(); stopTheme(); };}key vs. colId & Computed Columns
Section titled “key vs. colId & Computed Columns”- Data column (
DataColumnDef<T>): Setkeyto a property of your row typeT.colIddefaults tokey; provide an explicitcolIdif two columns read the same property. - Computed column (
ComputedColumnDef<T>): Omitkey, set a uniquecolId, and providevalueGetter: ({ data }) => ....
defaultColDef Merge Order
Section titled “defaultColDef Merge Order”Properties in defaultColDef apply to every column unless overridden on that column’s ColumnDef<T>:
const grid = new Grid<Order>({ container, defaultColDef: { isSortable: true, isResizable: true, minWidth: 80 }, columnDefs: [ { key: 'id', name: 'ID', width: 96, isResizable: false }, { key: 'customer', name: 'Customer', flex: 1 }, ], rowData,});cellDataType & Date Semantics
Section titled “cellDataType & Date Semantics”cellDataType |
Sorting | Filtering | Built-in Editor | Excel Export Cell Type |
|---|---|---|---|---|
'auto' |
Infers type from values | Text / type-aware | Text or boolean <select> |
Typed string/number/boolean/date |
'text' |
Locale/lexical string compare | Text operators (contains, startsWith, …) |
<input type="text"> |
Inline string |
'number' |
Numeric compare (NaN sorted deterministically) |
Comparable operators (equals, inRange, …) |
<input type="text" inputMode="decimal"> |
Numeric cell |
'boolean' |
Boolean compare | 'equals', 'blank', 'notBlank' |
<select> ('', 'True', 'False') |
Boolean cell |
'date' |
Calendar-day UTC timestamp | YYYY-MM-DD parsed as UTC midnight |
<input type="text"> |
Excel date cell |
'dateTime' |
Millisecond timestamp | ISO-8601 date-time with timezone or UTC | <input type="text"> |
Excel date cell |
- Date values:
'date'filter operands requireYYYY-MM-DD(validated as UTC calendar days), and'dateTime'parses valid ISO timestamps. Formatting (formatter) renders astextContentand never parses HTML. - Initial visibility: Set
isVisible: falseon a column to include it in state and data models while hiding it from initial rendering.