Skip to main content

apiRef — imperative control

Pass a ref to get an imperative handle for everything the grid can do — custom toolbars, external buttons, programmatic control. 120 methods in 20 namespaces.

import { useRef } from 'react';
import { DataTable, type DataTableApi } from '@ackplus/mui-tanstack-data-grid';

function MyGrid() {
const apiRef = useRef<DataTableApi<User> | null>(null);
return (
<>
<button onClick={() => apiRef.current?.export.exportCSV()}>Export</button>
<DataTable apiRef={apiRef} columns={columns} data={data} />
</>
);
}

apiRef.current is set after the first render, so call it from effects and event handlers.

Top level​

MethodSignatureDescription
subscribe(listener: () => void) => () => voidCall listener after every grid render (state, data or prop change). Returns the unsubscribe function. Used by <GridColumnsPanel> / <GridFilterPanel> / useGridApi to stay in sync with a grid rendered elsewhere.

table​

MethodSignatureDescription
getTable() => TanStackTable<T>The underlying TanStack Table instance, for anything the grid API doesn't cover.

columnVisibility​

MethodSignatureDescription
showColumn(columnId: string) => voidShow a hidden column.
hideColumn(columnId: string) => voidHide a column.
toggleColumn(columnId: string) => voidShow the column if hidden, hide it if shown.
showAllColumns() => voidShow every column.
hideAllColumns() => voidHide every column that can be hidden.
resetColumnVisibility() => voidBack to initialState.columnVisibility.
openPanel() => voidOpen the toolbar "Columns" management panel (registered by the toolbar; no-op if it isn't mounted).

columnOrdering​

MethodSignatureDescription
setColumnOrder(columnOrder: ColumnOrderState) => voidSet the full column order (ids, left to right).
moveColumn(columnId: string, toIndex: number) => voidMove one column to a position in the current order.
resetColumnOrder() => voidBack to initialState.columnOrder (or the columns order).

columnPinning​

MethodSignatureDescription
pinColumnLeft(columnId: string) => voidPin a column to the left (inline-start) edge.
pinColumnRight(columnId: string) => voidPin a column to the right (inline-end) edge.
unpinColumn(columnId: string) => voidUnpin a column.
setPinning(pinning: ColumnPinningState) => voidSet the whole pinning state: { left: ids, right: ids }.
resetColumnPinning() => voidBack to initialState.columnPinning (and the default pins of the built-in columns).

editing​

Whole-row edit mode (editMode: 'row').

MethodSignatureDescription
startRowEdit(rowId: string) => voidOpen a row for editing (all its editable cells).
saveRowEdit() => voidCommit the row being edited — fires processRowUpdate once.
cancelRowEdit() => voidDiscard the row being edited.
getEditingRowId() => string | nullId of the row in edit mode, or null.
isRowInEditMode(rowId: string) => booleanWhether this row is in edit mode.

pivot​

Pivoting (enablePivoting, client data).

MethodSignatureDescription
setPivotModel(model: PivotModel | null) => voidShow a pivot table (null = back to the normal table).
getPivotModel() => PivotModel | nullThe current pivot model, or null.

grouping​

Row grouping (enableRowGrouping, client data).

MethodSignatureDescription
setGrouping(columnIds: string[]) => voidSet the grouped columns, outermost first ([] ungroups).
groupByColumn(columnId: string) => voidAdd a column as the innermost grouping level.
ungroupColumn(columnId: string) => voidStop grouping by a column.
clearGrouping() => voidRemove all grouping.
getGrouping() => string[]The grouped column ids, outermost first.
expandAllGroups(expanded: boolean) => voidExpand every group (true) or collapse every group (false).

rowPinning​

MethodSignatureDescription
pinRowTop(rowId: string) => voidPin a row to the top band (client data only).
pinRowBottom(rowId: string) => voidPin a row to the bottom band (client data only).
unpinRow(rowId: string) => voidUnpin a row.
setRowPinning(pinning: RowPinningState) => voidSet the whole row-pinning state: { top: ids, bottom: ids }.
resetRowPinning() => voidBack to initialState.rowPinning.
getPinnedRowIds() => RowPinningStateThe pinned row ids: { top, bottom }.

columnResizing​

MethodSignatureDescription
resizeColumn(columnId: string, width: number) => voidSet a column's width in px.
autoSizeColumn(columnId: string) => voidFit a column to its widest rendered cell (the header double-click).
autoSizeAllColumns() => voidFit every resizable column to its content.
resetColumnSizing() => voidBack to initialState.columnSizing (or the column sizes).

aggregation​

MethodSignatureDescription
getTotals() => Record<string, any>Per-column footer totals over the filtered rows: { [columnId]: value }.

clipboard​

MethodSignatureDescription
copySelectedRows(options?: { includeHeaders?: boolean; delimiter?: string }) => Promise<number>Copy the selected rows (visible export columns) to the clipboard as delimited text; resolves the count copied.

filtering​

MethodSignatureDescription
setGlobalFilter(filter: string) => voidSet the quick-search text (resets to page 1).
clearGlobalFilter() => voidClear the quick-search text.
setColumnFilters(filters: ColumnFilterState, isApply?: boolean) => voidReplace the column-filter state; isApply: true applies it (resets the page, refetches in server mode).
addColumnFilter(columnId: string, operator: string, value: any) => voidAdd an applied rule for a column (e.g. addColumnFilter('age', 'greaterThan', 30)).
removeColumnFilter(filterId: string) => voidRemove one applied rule by its rule id.
clearAllFilters() => voidRemove every column-filter rule (applied and pending).
resetFilters() => voidBack to initialState.columnFilter, applied.
openColumnFilter(columnId: string) => voidOpen the toolbar column-filter panel with a rule pre-targeting columnId (registered by the filter control; no-op if it isn't mounted).
clearColumnFilter(columnId: string) => voidRemove all applied + pending filter rules for a single column (used by the column menu's "Clear filter").

sorting​

MethodSignatureDescription
setSorting(sorting: SortingState) => voidSet the full sort model: [{ id, desc }].
sortColumn(columnId: string, direction: 'asc' | 'desc' | false) => voidSort by one column: 'asc', 'desc', or false to unsort it.
clearSorting() => voidRemove all sorting.
resetSorting() => voidBack to initialState.sorting.

pagination​

MethodSignatureDescription
goToPage(pageIndex: number) => voidGo to a page (0-based), clamped to the pages that exist.
nextPage() => voidNext page (stays on the last page).
previousPage() => voidPrevious page (stays on the first page).
setPageSize(pageSize: number) => voidChange the page size and go to page 1.
goToFirstPage() => voidGo to page 1.
goToLastPage() => voidGo to the last page.
resetPagination?() => voidBack to initialState.pagination.

selection​

MethodSignatureDescription
selectRow(rowId: string) => voidSelect a row (respects isRowSelectable).
deselectRow(rowId: string) => voidDeselect a row.
toggleRowSelection(rowId: string) => voidSelect the row if not selected, deselect it if selected.
selectAll() => voidselectMode="page": add the current page's rows. "all": select every row.
deselectAll() => voidClear the whole selection (every page).
toggleSelectAll() => voidWhat the header checkbox does: select / deselect the page (or everything in "all" mode).
getSelectionState() => SelectionStateThe selection as { ids, type: 'include' | 'exclude' } (exclude after select-all in "all" mode).
getSelectedCount() => numberHow many rows are selected (in "all" mode: total minus the excluded ids).
getSelectedRows() => Row<T>[]The selected rows that are loaded (server mode: the current page only).
isRowSelected(rowId: string) => booleanWhether a row is selected.

data​

MethodSignatureDescription
refresh(options?: DataRefreshApiInput) => voidRe-run onFetchData with the current state and call onRefreshData. true / { resetPagination: true } goes to page 1.
reload(options?: DataRefreshApiOptions) => voidLike refresh, with meta.reason: 'reload'.
resetAll() => voidReset the grid's whole state (columns, filters, sorting, page, selection, expansion) to its initial values, then reload.
getAllData() => T[]All rows the grid holds (client: the full data; server: the loaded page). A copy.
getRowData(rowId: string) => T | undefinedA row's data by id (any loaded row, not just the current page).
getRowByIndex(index: number) => T | undefinedThe row at a display index on the current page.
updateRow(rowId: string, updates: Partial<T>) => voidMerge changes into a row (by id). The grid keeps a local copy until a new data prop or fetch replaces it.
updateRowByIndex(index: number, updates: Partial<T>) => voidMerge changes into the row at an index in the data.
insertRow(newRow: T, index?: number) => voidInsert a row (at index, default the end).
deleteRow(rowId: string) => voidRemove a row by id.
deleteRowByIndex(index: number) => voidRemove the row at an index in the data.
deleteSelectedRows() => voidRemove the selected rows and clear the selection. No-op after select-all in selectMode="all" (no explicit id list).
replaceAllData(newData: T[]) => voidReplace every row.
updateMultipleRows(updates: Array<{ rowId: string; data: Partial<T> }>) => voidMerge changes into several rows: [{ rowId, data }].
insertMultipleRows(newRows: T[], startIndex?: number) => voidInsert several rows (at startIndex, default the end).
deleteMultipleRows(rowIds: string[]) => voidRemove several rows by id.
updateField(rowId: string, fieldName: keyof T, value: any) => voidSet one field of a row (by id).
updateFieldByIndex(index: number, fieldName: keyof T, value: any) => voidSet one field of the row at an index in the data.
findRows(predicate: (row: T) => boolean) => T[]Rows matching a predicate (searches all held rows, ignoring filters).
findRowIndex(predicate: (row: T) => boolean) => numberIndex in the data of the first row matching a predicate, or -1.
getDataCount() => numberNumber of rows held (before filtering).
getFilteredDataCount() => numberNumber of rows after filtering (client filtering).

layout​

MethodSignatureDescription
reset(actions?: ResetLayoutAction[]) => voidReset a configurable subset of the layout to its initial values, without touching data (no reload). Defaults to DEFAULT_RESET_ACTIONS (columnOrder + columnPinning + columnSizing). This backs the toolbar "Reset layout" button.
resetLayout() => voidReset the ENTIRE view state (columns, filters, sorting, pagination, density).
resetAll() => voidresetLayout() + reload the data.
saveLayout() => SavedLayoutSnapshot of the layout (columns, sizing, pinning, filters, sorting, page, density) to persist yourself.
restoreLayout(layout: Partial<SavedLayout>) => voidApply a snapshot from saveLayout() (missing keys are left as they are).

views​

Saved/named views — capture the current layout as a named preset and switch between them.

MethodSignatureDescription
listViews() => SavedView[]All saved views.
getActiveViewId() => string | nullId of the active view, or null for the default.
getActiveView() => SavedView | nullThe active view, or null for the default.
saveView(name: string) => SavedViewCapture the current layout as a new named view and make it active.
updateView(id: string) => voidOverwrite a view's captured layout with the current one.
applyView(id: string) => voidApply a saved view's layout and make it active.
renameView(id: string, name: string) => voidRename a view.
deleteView(id: string) => voidDelete a view. Deleting the active one switches back to the default view and resets the layout.
resetView() => voidClear the active view and reset the layout to its initial state.
isDirty() => booleanTrue when the current layout diverges from the active view (or any layout when none is active).

state​

MethodSignatureDescription
getTableState() => Partial<TableState>The full TanStack table state.
getCurrentFilters() => ColumnFilterStateThe column-filter state: applied filters + logic, and the pending* drafts.
getCurrentSorting() => SortingStateThe sort model: [{ id, desc }].
getCurrentPagination() => PaginationModelThe page: { pageIndex, pageSize }.
getCurrentSelection() => SelectionStateSame as selection.getSelectionState().
getGlobalFilter() => stringThe quick-search text.
getLocaleText() => DataTableLocaleTextThe resolved UI strings (your localeText merged over the English defaults).

export​

MethodSignatureDescription
exportCSV(options?: DataTableExportApiOptions) => Promise<void>Export to CSV using the active (or per-call) export mode.
exportExcel(options?: DataTableExportApiOptions) => Promise<void>Export to XLSX (small/medium only — Excel caps at 1,048,576 rows).
isExporting() => booleanWhether an export is running.
cancelExport() => voidAbort the running export.