Cell Renderers
Implement cellRenderer on ColumnDef<T> to render badges, progress bars, or rich interactive elements inside pooled cell containers.
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, }, { key: 'customer', name: 'Customer', width: 175, flex: 1, }, { key: 'quantity', name: 'Volume Bar', width: 145, cellRenderer: ({ value, document: doc }) => { const wrap = doc.createElement('div'); wrap.style.cssText = 'display:flex;align-items:center;gap:8px;width:100%'; const track = doc.createElement('div'); track.style.cssText = 'flex:1;height:6px;border-radius:999px;background:rgba(148,163,184,0.25);overflow:hidden'; const bar = doc.createElement('div'); bar.style.cssText = 'height:100%;background:var(--next-grid-focus-color);border-radius:999px'; const label = doc.createElement('span'); label.style.cssText = 'font-variant-numeric:tabular-nums;min-width:24px;text-align:right'; const apply = (v: unknown) => { const n = Math.min(25, Math.max(0, Number(v) || 0)); bar.style.width = `${Math.round((n / 25) * 100)}%`; label.textContent = String(n); }; track.append(bar); wrap.append(track, label); apply(value); return { element: wrap, update: ({ value: next }) => { apply(next); return true; }, destroy: () => wrap.remove(), }; }, }, { key: 'status', name: 'Status', width: 115, cellRenderer: ({ formattedValue, document: doc }) => { const pill = doc.createElement('span'); const apply = (text: string) => { pill.textContent = text; const bg = text === 'Paid' ? 'rgba(34,197,94,0.16)' : text === 'Refunded' ? 'rgba(239,68,68,0.16)' : 'rgba(56,189,248,0.16)'; pill.style.cssText = `padding:2px 8px;border-radius:999px;font-size:12px;background:${bg}`; }; apply(formattedValue); return { element: pill, update: ({ formattedValue: next }) => { apply(next); return true; }, destroy: () => pill.remove(), }; }, }, ];
const grid = new Grid<OrderRow>({ container: stage, columnDefs, rowData: createOrders(30), getRowId: (row) => row.id, rowHeight: 32, }); const stopTheme = syncGridTheme(stage);
return () => { grid.destroy(); stopTheme(); };}CellRendererInstance & Recycling
Section titled “CellRendererInstance & Recycling”{ key: 'status', name: 'Status', cellRenderer: ({ formattedValue, document }) => { const badge = document.createElement('span'); badge.textContent = formattedValue; return { element: badge, update: ({ formattedValue: next }) => { badge.textContent = next; return true; // keep DOM node and skip destroy/recreate }, destroy: () => badge.remove(), }; },}update(params): boolean: Returntrueafter updating the existing DOM node in place so scrolling recycles nodes without destroying and recreating elements. Returningfalse(or omittingupdate) destroys and re-invokescellRenderer.destroy(): Called exactly once when the cell is released or replaced.pinnedCellRenderer: Pinned top/bottom rows do not carry bodysourceIndexvalues; configurepinnedCellRenderer(PinnedCellRendererParams<T>) for custom rendering in pinned regions.- Framework Component Renderers: See React, Vue, Svelte, and Angular.