View as Markdown

API reference: @salmonui/data-grid

DataGridApiinterface

import type { DataGridApi } from '@salmonui/data-grid';
interface DataGridApi<Row = unknown> extends Omit<CoreDataGridApi<Row>, 'engine' | 'getRowInfo' | keyof DataGridQueryApi>, DataGridSelectionApi<Row>, DataGridChromeApi, DataGridQueryApi, DataGridStateMethods, DataGridEditApi<Row>, DataGridOutputApi<Row>, DataGridAdaptiveApi, DataGridDetailApi, DataGridRowPinningApi, DataGridDataUiApi, DataGridFilterBuilderApi, DataGridViewApi, DataGridTreeApi<Row> {
    readonly engine: DataGridApiEngine<Row>;
    readonly element: HTMLDivElement | null;
    focus(): void;
    focusCell(rowId: RowId, field: string): void;
    focusHeader(field: string): void;
    getFocusedCell(): DataGridFocusedCell | null;
    registerKeyHandler(command: string, handler: DataGridKeyHandler, options?: {
        priority?: number;
    }): () => void;
    getRowInfo(target: RowId | Element | EventTarget | null): DataGridRowInfo<Row> | undefined;
    getRowElement(rowId: RowId): HTMLElement | null;
    getCellElement(rowId: RowId, field: string): HTMLElement | null;
    scrollToRow(rowId: RowId, options?: DataGridScrollOptions): void;
    scrollToColumn(field: string, options?: DataGridScrollOptions): void;
    autoSizeColumns(fields?: string | readonly string[]): void;
    setColumnWidth(field: string, width: number): DataGridActionResult;
    moveColumn(field: string, toIndex: number): DataGridActionResult;
    getRenderedRange(): {
        readonly rows: VirtualRange;
        readonly columns: VirtualRange;
    };
}

Imperative DataGrid API (ref, useDataGridApi()).

DataGridApi members (143, 127 inherited)
NameSignatureDescription
enginereadonly engine: DataGridApiEngine<Row>;

The engine, with the façades of the optional modules a prop registers (infiniteScroll).

elementreadonly element: HTMLDivElement | null;

The root element; null before mount and after unmount.

focusfocus(): void;

Focuses the grid content: the focused body cell, else the last focused body cell, else the first body cell (also from the header, like Alt+W).

focusCellfocusCell(rowId: RowId, field: string): void;

Scrolls the cell into view and focuses it.

focusHeaderfocusHeader(field: string): void;
getFocusedCellgetFocusedCell(): DataGridFocusedCell | null;
registerKeyHandlerregisterKeyHandler(command: string, handler: DataGridKeyHandler, options?: { priority?: number; }): () => void;

Registers a handler for a keyboard command (DEFAULT_KEY_BINDINGS); higher priority runs first. Returns the unregister function.

getRowInfogetRowInfo(target: RowId | Element | EventTarget | null): DataGridRowInfo<Row> | undefined;

Row info by row id, or by any element inside a row (cell, custom template content).

getRowElementgetRowElement(rowId: RowId): HTMLElement | null;
getCellElementgetCellElement(rowId: RowId, field: string): HTMLElement | null;
scrollToRowscrollToRow(rowId: RowId, options?: DataGridScrollOptions): void;
scrollToColumnscrollToColumn(field: string, options?: DataGridScrollOptions): void;
autoSizeColumnsautoSizeColumns(fields?: string | readonly string[]): void;

Fits widths to header and rendered content; every visible column when omitted.

setColumnWidthsetColumnWidth(field: string, width: number): DataGridActionResult;
moveColumnmoveColumn(field: string, toIndex: number): DataGridActionResult;
getRenderedRangegetRenderedRange(): { readonly rows: VirtualRange; readonly columns: VirtualRange; };

Rendered row range and center column range (overscan included).

getColumninheritedgetColumn: (fieldOrId: string) => CompiledColumn<Row> | undefined;

Leaf column by column id, else the first leaf with that field.

getColumnsinheritedgetColumns: () => readonly CompiledColumn<Row>[];

Every leaf column in definition order, including hidden ones.

getVisibleColumnsinheritedgetVisibleColumns: () => readonly CompiledColumn<Row>[];

Visible leaves in runtime order.

getHiddenColumnsinheritedgetHiddenColumns: () => readonly CompiledColumn<Row>[];

Leaves hidden at runtime, in definition order.

getColumnAtinheritedgetColumnAt: (index: number) => CompiledColumn<Row> | undefined;

Visible leaf at an index of getVisibleColumns().

getColumnIndexinheritedgetColumnIndex: (fieldOrId: string) => number;

Index of a column in getVisibleColumns(), or -1.

showColumnsinheritedshowColumns: (fieldsOrIds: string | readonly string[]) => CommandResult;
hideColumnsinheritedhideColumns: (fieldsOrIds: string | readonly string[]) => CommandResult;
getViewRowsinheritedgetViewRows: () => readonly Row[];

Rows of the current view (after the query pipeline), as raw references.

getFilteredRowsinheritedgetFilteredRows: () => readonly Row[];

Rows passing search and every filter, in sort order, across every page, as raw references. Without the filter module, every row.

getRowinheritedgetRow: (rowId: string) => Row | undefined;
getRowIndexinheritedgetRowIndex: (rowId: string) => number;

Visible index of a row, or -1.

getRowIdAtinheritedgetRowIdAt: (index: number) => string | undefined;

Row id at a visible index (the inverse of getRowIndex), or undefined.

getCellinheritedgetCell: (rowId: string, field: string) => CellSnapshot | undefined;
getDataSourceinheritedgetDataSource: () => DataSource<Row>;
getPrimaryKeyFieldsinheritedgetPrimaryKeyFields: () => readonly string[];

field of each primaryKey leaf column, in column order.

getColumnFacetinheritedgetColumnFacet: (field: string, options?: FacetOptions | undefined) => ColumnFacet | undefined;

Distinct values with counts for checkbox / Excel filters, or undefined without the filter module.

getPageInfoinheritedgetPageInfo: () => PageInfo | undefined;

Pager model, or undefined without the paging module.

groupColumninheritedgroupColumn: (field: string, index?: number | undefined) => CommandResult;

Needs the grouping module.

ungroupColumninheritedungroupColumn: (field: string) => CommandResult;
clearGroupinginheritedclearGrouping: () => CommandResult;
toggleGroupinheritedtoggleGroup: (group: GroupTarget, expanded?: boolean | undefined) => CommandResult;

Toggles (or sets) one group by row id, path key, or path.

expandAllGroupsinheritedexpandAllGroups: () => CommandResult;
collapseAllGroupsinheritedcollapseAllGroups: () => CommandResult;
getGroupinheritedgetGroup: (rowId: string) => GroupRowSnapshot | undefined;

Caption or footer snapshot of a group row id, or undefined.

getAggregatesinheritedgetAggregates: (groupId?: string | undefined) => Readonly<Record<string, Readonly<Record<string, unknown>>>> | undefined;

Without an argument the footer aggregates (undefined without the aggregates module); with a group row id that group's aggregates (undefined for an unknown id).

refreshinheritedrefresh: () => void;

Re-reads the data with the current query and keeps the grid state: engine.refresh().

disposeinheriteddispose: () => void;
getStateinheritedgetState: (options?: DataGridStateOptions | undefined) => DataGridState;

Persistable state (columns, pinning, sorting, filters, grouping, paging, search, selection, scroll).

restoreStateinheritedrestoreState: (state: string | DataGridState, options?: (DataGridStateOptions & DeserializeDataGridStateOptions) | undefined) => RestoreResult;

Restores a state (object or serialized) in one batch without recompiling columns.

resetStateinheritedresetState: () => RestoreResult;

Restores the state captured when this API was created.

pinColumninheritedpinColumn: (field: string, pinned: DataGridColumnPin | null) => CommandResult;

Pins a column (id or field) to a side or unpins it (null).

openColumnChooserinheritedopenColumnChooser: (position?: { readonly x: number; readonly y: number; } | undefined) => void;

Opens the column chooser; at the given viewport point, else at the grid start.

openColumnMenuinheritedopenColumnMenu: (field: string) => void;
openFilterMenuinheritedopenFilterMenu: (field: string) => void;
closeOverlaysinheritedcloseOverlays: () => void;

Closes any open menu, dialog, or chooser (focus returns to its invoker).

registerCommandHandlerinheritedregisterCommandHandler: (command: DataGridUiCommand | (string & {}), handler: DataGridCommandHandler) => () => void;

Registers the handler of a UI command (toolbar, menus); returns the unregister function.

runCommandinheritedrunCommand: (command: DataGridUiCommand | (string & {}), payload?: Readonly<Record<string, unknown>> | undefined) => boolean;

Runs a UI command (handler, then a module command, else a dev warning). Returns true when handled.

getHeaderElementinheritedgetHeaderElement: (field: string) => HTMLElement | null;

Header cell of a column (id or field); null outside the rendered window.

getPartElementinheritedgetPartElement: (part: DataGridPart) => HTMLElement | null;

First rendered element of a part; null when it is not rendered.

openSortDialoginheritedopenSortDialog: () => void;

Opens the sort sheet (showAdaptiveSortDialog).

openFilterDialoginheritedopenFilterDialog: (field?: string | undefined) => void;

Opens the filter sheet of field, or the list of filterable columns (showAdaptiveFilterDialog).

isAdaptiveinheritedisAdaptive: () => boolean;

True while the adaptive layout applies (derived from the measured container width). Salmon

copyinheritedcopy: (options?: DataGridCopyOptions | undefined) => CommandResult;

Copies the cell range, else the selected rows, else the focused cell to the system clipboard (drafts included). Falls back to a copy dialog when the browser blocks clipboard access. rejected with nothingToCopy or cancelled (onBeforeCopy).

pasteinheritedpaste: (text?: string | undefined) => CommandResult;

Pastes TSV text at the selected range or focused cell. Without text, reads the system clipboard (async), or opens a paste dialog when the Clipboard API is unavailable.

startEditinheritedstartEdit: (rowId?: string | undefined, field?: string | undefined) => CommandResult;

Row and dialog mode edit the row, batch and cell mode the cell. Without a row id: the first selected row.

saveinheritedsave: () => CommandResult;

Saves: the active row or cell; in batch mode every pending change.

saveCellinheritedsaveCell: () => CommandResult;

Commits the active editor only (batch mode keeps the change pending).

cancelEditinheritedcancelEdit: () => CommandResult;

Discards the active editor; in batch mode without an open editor, every pending change.

addRowinheritedaddRow: (row?: Readonly<Record<string, unknown>> | undefined, index?: number | undefined) => CommandResult;

Index 0 adds at the top, at or past the end at the bottom.

deleteRowsinheriteddeleteRows: (rowIds?: string | readonly string[] | undefined) => CommandResult;

Without ids: the selected rows. Batch mode marks the rows until save.

updateRowinheritedupdateRow: (rowId: string, row: Readonly<Record<string, unknown>>) => CommandResult;
updateCellinheritedupdateCell: (rowId: string, field: string, value: unknown) => CommandResult;

Edits a cell as a user would (draft in batch mode).

setCellValueinheritedsetCellValue: (rowId: string, field: string, value: unknown) => CommandResult;

Silent data updates without the edit lifecycle (local sources).

setRowDatainheritedsetRowData: (rowId: string, row: Row) => CommandResult;
applyChangesinheritedapplyChanges: (changes: EditDataChanges<Row>) => CommandResult;
getPendingChangesinheritedgetPendingChanges: () => EditBatchChanges<Row>;
getBatchPreviewinheritedgetBatchPreview: () => EditPreview | undefined;
getEditSnapshotinheritedgetEditSnapshot: () => EditSnapshot | undefined;
hasUnsavedChangesinheritedhasUnsavedChanges: () => boolean;
whenEditSettledinheritedwhenEditSettled: () => Promise<void>;
validateBatchinheritedvalidateBatch: () => Promise<boolean>;

Validates every pending change without saving; resolves true when valid.

undoinheritedundo: () => CommandResult;

Undoes the last edit (needs editing.undo).

redoinheritedredo: () => CommandResult;
fillSelectioninheritedfillSelection: (rowId: string, field: string) => CommandResult;

Fills the selected cell range's content over the range extended to field of rowId (fill handle).

sortColumninheritedsortColumn: (field: string, direction: DataGridSortDirection, options?: { multiple?: boolean; } | undefined) => CommandResult;

Sorts by a column; multiple adds it to a multi-column sort.

removeSortinheritedremoveSort: (field: string) => CommandResult;
clearSortinginheritedclearSorting: () => CommandResult;
filterColumninheritedfilterColumn: (field: string, operator: DataGridFilterOperator, value: Json, options?: { logic?: "and" | "or"; matchCase?: boolean; ignoreAccents?: boolean; } | undefined) => CommandResult;

Replaces the column's top-level conditions with one condition (an array value with a scalar operator ORs one condition per value).

removeFilterinheritedremoveFilter: (field: string) => CommandResult;

Removes the filters of one column.

clearFilteringinheritedclearFiltering: (fields?: string | readonly string[] | undefined) => CommandResult;

Removes the filters of the given columns, or every filter.

searchinheritedsearch: (text: string) => CommandResult;
clearSearchinheritedclearSearch: () => CommandResult;
goToPageinheritedgoToPage: (page: number) => CommandResult;
setPageSizeinheritedsetPageSize: (pageSize: number) => CommandResult;
expandDetailinheritedexpandDetail: (rowId: string) => CommandResult;
collapseDetailinheritedcollapseDetail: (rowId: string) => CommandResult;
toggleDetailinheritedtoggleDetail: (rowId: string, expanded?: boolean | undefined) => CommandResult;

Toggles one detail row; expanded forces a state.

expandAllDetailsinheritedexpandAllDetails: () => CommandResult;
collapseAllDetailsinheritedcollapseAllDetails: () => CommandResult;
isDetailExpandedinheritedisDetailExpanded: (rowId: string) => boolean;
getChildGridinheritedgetChildGrid: (rowId: string) => DataGridApi<unknown> | null;

The mounted child grid of a parent row (null when collapsed or not rendered).

moveRowsinheritedmoveRows: (rowIds: string | readonly string[], to: number | DataGridMoveTarget) => CommandResult;

Moves rows so the first one ends at data row index toIndex, or next to / into a target row ({ targetRowId, position }; into makes them the last children of the target in tree mode).

exportToExcelinheritedexportToExcel: (options?: DataGridExcelExportOptions<Row> | undefined) => Promise<ExportFile<Uint8Array<ArrayBufferLike>> | null>;

XLSX export (excelExport); resolves to the file, or null when prevented. Downloads unless download: false.

exportToPdfinheritedexportToPdf: (options?: DataGridPdfExportOptions<Row> | undefined) => Promise<ExportFile<Uint8Array<ArrayBufferLike>> | null>;

PDF export (pdfExport).

exportToCsvinheritedexportToCsv: (options?: DataGridCsvExportOptions<Row> | undefined) => Promise<ExportFile<string> | null>;

CSV export (csvExport); no writer package needed.

printinheritedprint: (options?: DataGridPrintOptions<Row> | undefined) => Promise<boolean>;

Prints the grid (every row of the scope, no virtualization). Resolves false when prevented or outside a browser.

setLoadinginheritedsetLoading: (loading: boolean) => void;

Shows the loading indicator until setLoading(false). A data load in progress shows it regardless.

expandAllTreeRowsinheritedexpandAllTreeRows: () => CommandResult;
collapseAllTreeRowsinheritedcollapseAllTreeRows: () => CommandResult;
expandTreeLevelinheritedexpandTreeLevel: (depth: number) => CommandResult;

Expands the parents at depth (0: roots).

collapseTreeLevelinheritedcollapseTreeLevel: (depth: number) => CommandResult;
expandTreeRowsinheritedexpandTreeRows: (rowIds: string | readonly string[]) => CommandResult;
collapseTreeRowsinheritedcollapseTreeRows: (rowIds: string | readonly string[]) => CommandResult;
toggleTreeRowinheritedtoggleTreeRow: (rowId: string, expanded?: boolean | undefined) => CommandResult;
indentRowsinheritedindentRows: (rowIds: string | readonly string[]) => CommandResult;

Moves rows under their previous sibling.

outdentRowsinheritedoutdentRows: (rowIds: string | readonly string[]) => CommandResult;

Moves rows after their parent, as its siblings.

getTreeRowinheritedgetTreeRow: (rowId: string) => DataGridTreeRowInfo | null;
getChildRowsinheritedgetChildRows: (rowId: string | null) => readonly Row[];

Kept children of a row in view order (null: the roots).

reloadTreeChildreninheritedreloadTreeChildren: (rowId: string) => CommandResult;

Salmon: retries a failed lazy child load.

getVisibleRowsinheritedgetVisibleRows: () => readonly Row[];

Visible data rows in view order.

pinRowsinheritedpinRows: (rowIds: string | readonly string[]) => CommandResult;

Pins rows to the top zone (rejected unless isRowPinned or defaultPinnedRows is set).

unpinRowsinheritedunpinRows: (rowIds: string | readonly string[]) => CommandResult;

Unpins rows, also rows isRowPinned matches.

isRowPinnedinheritedisRowPinned: (rowId: string) => boolean;

True when the row is pinned, whether or not the current filter shows it.

getPinnedRowIdsinheritedgetPinnedRowIds: () => readonly string[];

Ids of the pinned rows shown, in display order.

openFilterBuilderinheritedopenFilterBuilder: () => void;

Opens the filter builder dialog. Ignored unless filtering.builder is set.

closeFilterBuilderinheritedcloseFilterBuilder: () => void;

Closes the filter builder dialog without applying.

getViewportinheritedgetViewport: () => DataGridViewport;

Scroll sync extension point: the current body viewport.

subscribeViewportinheritedsubscribeViewport: (listener: () => void) => () => void;

Calls listener outside React on every body scroll and layout change; returns the unsubscribe function.

getRowOffsetsinheritedgetRowOffsets: () => RowMetrics;

Row height extension point: the row offsets of the visible row model (the body's own prefix sums).

scrollToinheritedscrollTo: (options: DataGridScrollToOptions) => void;

Scrolls the body to an offset. An omitted axis keeps its position; offsets are clamped to the scroll range.

getPageSizeForHeightinheritedgetPageSizeForHeight: (height: number) => number;

Whole data rows that fit in height px after the toolbar, group drop area, header rows, footer aggregate rows and pager; 0 when none fit. Rows count rowHeight (the estimate with measured rows, the card height in adaptive vertical mode). Before mount only the header and footer rows are subtracted.

selectRowsinheritedselectRows: (rowIds: string | readonly string[], options?: { mode?: "add" | "remove" | "replace" | "toggle"; } | undefined) => CommandResult;
selectRowRangeinheritedselectRowRange: (fromRowId: string, toRowId: string, options?: { mode?: "add" | "replace"; } | undefined) => CommandResult;
selectAllinheritedselectAll: (options?: { scope?: "filtered" | "page"; } | undefined) => CommandResult;

page: every row of the current page (all rows without paging). filtered (default with paging off): every row matching the query, O(1).

clearSelectioninheritedclearSelection: (parts?: { rows?: boolean; cells?: boolean; columns?: boolean; } | undefined) => CommandResult;
selectCellsinheritedselectCells: (cells: readonly DataGridCellKey[], options?: { mode?: "add" | "replace" | "toggle"; } | undefined) => CommandResult;
selectCellRangeinheritedselectCellRange: (anchor: DataGridCellKey, focus: DataGridCellKey, options?: { mode?: "add" | "replace"; } | undefined) => CommandResult;
selectColumnsinheritedselectColumns: (fields: string | readonly string[], options?: { mode?: "add" | "replace" | "toggle"; } | undefined) => CommandResult;
isRowSelectedinheritedisRowSelected: (rowId: string) => boolean;
getSelectedRowIdsinheritedgetSelectedRowIds: () => readonly string[];
getSelectedRowsinheritedgetSelectedRows: () => readonly Row[];

Raw rows, in the order of getSelectedRowIds().

getSelectedCellsinheritedgetSelectedCells: () => readonly DataGridCellKey[];
getSelectedColumnsinheritedgetSelectedColumns: () => readonly string[];
getSelectioninheritedgetSelection: () => SerializedSelection | undefined;

Serializable selection (scope, ids or excluded ids, cell ranges, columns).

setSelectioninheritedsetSelection: (selection: SerializedSelection) => CommandResult;
getSelectionSummaryinheritedgetSelectionSummary: () => SelectionSummary | undefined;