Sorting
Click any sortable column header to toggle ascending/descending/cleared sort, or hold Shift while clicking headers to build multi-column sorts.
import { Grid, type ColumnDef } from 'next-grid-js';import { syncGridTheme } from './theme';import type { DemoMountContext } from './types';import { addButton, addInput } from './ui';
interface TaskRow { id: string; service: string; department: string; priority: 'Critical' | 'High' | 'Normal'; score: number; active: boolean;}
const TASK_ROWS: readonly TaskRow[] = [ { id: 'T-01', service: 'Auth Gateway', department: 'Platform', priority: 'Critical', score: 94, active: true, }, { id: 'T-02', service: 'Billing Worker', department: 'Payments', priority: 'High', score: 88, active: true, }, { id: 'T-03', service: 'Search Indexer', department: 'Core', priority: 'Normal', score: 76, active: true, }, { id: 'T-04', service: 'Edge Cache', department: 'Infra', priority: 'Critical', score: 97, active: true, }, { id: 'T-05', service: 'Webhook Relay', department: 'Payments', priority: 'High', score: 82, active: false, }, { id: 'T-06', service: 'Audit Stream', department: 'Platform', priority: 'Normal', score: 71, active: true, }, { id: 'T-07', service: 'Metrics Collector', department: 'Infra', priority: 'High', score: 91, active: true, }, { id: 'T-08', service: 'Batch Exporter', department: 'Core', priority: 'Normal', score: 68, active: false, },];
export function mount({ stage, controls, meta,}: DemoMountContext): () => void { const priorityRank: Record<string, number> = { Critical: 0, High: 1, Normal: 2, };
const columnDefs: readonly ColumnDef<TaskRow>[] = [ { key: 'id', name: 'ID', width: 80, isSortable: true, }, { key: 'service', name: 'Service', width: 175, minWidth: 145, flex: 1, isSortable: true, isFilterable: true, }, { key: 'department', name: 'Dept', width: 120, minWidth: 100, isSortable: true, isFilterable: true, }, { key: 'priority', name: 'Priority', width: 115, minWidth: 100, isSortable: true, comparator: ({ valueA, valueB }) => (priorityRank[String(valueA)] ?? 99) - (priorityRank[String(valueB)] ?? 99), }, { key: 'score', name: 'Score', width: 95, minWidth: 80, cellDataType: 'number', isSortable: true, }, { key: 'active', name: 'Active', width: 90, minWidth: 80, cellDataType: 'boolean', isSortable: true, }, ];
const grid = new Grid<TaskRow>({ container: stage, columnDefs, rowData: [...TASK_ROWS], getRowId: (row) => row.id, }); const stopTheme = syncGridTheme(stage);
grid.addEventListener('modelUpdated', (event) => { meta.textContent = `${event.rowCount} / ${TASK_ROWS.length} rows`; }); grid.setSortModel([ { colId: 'priority', direction: 'asc', }, { colId: 'score', direction: 'desc', }, ]);
let input!: HTMLInputElement; let resetBtn!: HTMLButtonElement; const initialSortJson = JSON.stringify(grid.getSortModel());
const syncSortReset = () => { const sortChanged = JSON.stringify(grid.getSortModel()) !== initialSortJson; resetBtn.disabled = !sortChanged && input.value.length === 0; };
input = addInput(controls, { type: 'search', placeholder: 'Quick filter...', ariaLabel: 'Quick filter rows', onInput: (val) => { grid.setQuickFilter(val); syncSortReset(); }, });
resetBtn = addButton( controls, 'Reset', () => { input.value = ''; grid.setQuickFilter(''); grid.setSortModel([ { colId: 'priority', direction: 'asc', }, { colId: 'score', direction: 'desc', }, ]); syncSortReset(); }, { disabled: true }, );
grid.addEventListener('sortChanged', syncSortReset); meta.textContent = `${TASK_ROWS.length} / ${TASK_ROWS.length} rows`;
return () => { grid.destroy(); stopTheme(); };}Configuration & Programmatic API
Section titled “Configuration & Programmatic API”- Enable per column: Set
isSortable: trueonColumnDef<T>ordefaultColDef. - Custom ordering: Provide
comparator: ({ valueA, valueB, dataA, dataB, colId }) => numberfor domain-specific ordering (such as priority ranks). - Type-aware defaults: When
comparatoris omitted, NextGrid compares values according tocellDataType(number,boolean,date,dateTime,text, or'auto'). Client sorting is stable. - Programmatic control:
grid.setSortModel([ { colId: 'priority', direction: 'asc' }, { colId: 'score', direction: 'desc' },]);
const current = grid.getSortModel();- Remote models: For
'infinite','serverSide', and'viewport',setSortModelsendsrequest.sortto your datasource without sorting locally.