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
| Method | Signature | Description |
|---|---|---|
subscribe | (listener: () => void) => () => void | Call 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
| Method | Signature | Description |
|---|---|---|
getTable | () => TanStackTable<T> | The underlying TanStack Table instance, for anything the grid API doesn't cover. |
columnVisibility
| Method | Signature | Description |
|---|---|---|
showColumn | (columnId: string) => void | Show a hidden column. |
hideColumn | (columnId: string) => void | Hide a column. |
toggleColumn | (columnId: string) => void | Show the column if hidden, hide it if shown. |
showAllColumns | () => void | Show every column. |
hideAllColumns | () => void | Hide every column that can be hidden. |
resetColumnVisibility | () => void | Back to initialState.columnVisibility. |
openPanel | () => void | Open the toolbar "Columns" management panel (registered by the toolbar; no-op if it isn't mounted). |
columnOrdering
| Method | Signature | Description |
|---|---|---|
setColumnOrder | (columnOrder: ColumnOrderState) => void | Set the full column order (ids, left to right). |
moveColumn | (columnId: string, toIndex: number) => void | Move one column to a position in the current order. |
resetColumnOrder | () => void | Back to initialState.columnOrder (or the columns order). |
columnPinning
| Method | Signature | Description |
|---|---|---|
pinColumnLeft | (columnId: string) => void | Pin a column to the left (inline-start) edge. |
pinColumnRight | (columnId: string) => void | Pin a column to the right (inline-end) edge. |
unpinColumn | (columnId: string) => void | Unpin a column. |
setPinning | (pinning: ColumnPinningState) => void | Set the whole pinning state: { left: ids, right: ids }. |
resetColumnPinning | () => void | Back to initialState.columnPinning (and the default pins of the built-in columns). |
editing
Whole-row edit mode (editMode: 'row').
| Method | Signature | Description |
|---|---|---|
startRowEdit | (rowId: string) => void | Open a row for editing (all its editable cells). |
saveRowEdit | () => void | Commit the row being edited — fires processRowUpdate once. |
cancelRowEdit | () => void | Discard the row being edited. |
getEditingRowId | () => string | null | Id of the row in edit mode, or null. |
isRowInEditMode | (rowId: string) => boolean | Whether this row is in edit mode. |
pivot
Pivoting (enablePivoting, client data).
| Method | Signature | Description |
|---|---|---|
setPivotModel | (model: PivotModel | null) => void | Show a pivot table (null = back to the normal table). |
getPivotModel | () => PivotModel | null | The current pivot model, or null. |
grouping
Row grouping (enableRowGrouping, client data).
| Method | Signature | Description |
|---|---|---|
setGrouping | (columnIds: string[]) => void | Set the grouped columns, outermost first ([] ungroups). |
groupByColumn | (columnId: string) => void | Add a column as the innermost grouping level. |
ungroupColumn | (columnId: string) => void | Stop grouping by a column. |
clearGrouping | () => void | Remove all grouping. |
getGrouping | () => string[] | The grouped column ids, outermost first. |
expandAllGroups | (expanded: boolean) => void | Expand every group (true) or collapse every group (false). |
rowPinning
| Method | Signature | Description |
|---|---|---|
pinRowTop | (rowId: string) => void | Pin a row to the top band (client data only). |
pinRowBottom | (rowId: string) => void | Pin a row to the bottom band (client data only). |
unpinRow | (rowId: string) => void | Unpin a row. |
setRowPinning | (pinning: RowPinningState) => void | Set the whole row-pinning state: { top: ids, bottom: ids }. |
resetRowPinning | () => void | Back to initialState.rowPinning. |
getPinnedRowIds | () => RowPinningState | The pinned row ids: { top, bottom }. |
columnResizing
| Method | Signature | Description |
|---|---|---|
resizeColumn | (columnId: string, width: number) => void | Set a column's width in px. |
autoSizeColumn | (columnId: string) => void | Fit a column to its widest rendered cell (the header double-click). |
autoSizeAllColumns | () => void | Fit every resizable column to its content. |
resetColumnSizing | () => void | Back to initialState.columnSizing (or the column sizes). |
aggregation
| Method | Signature | Description |
|---|---|---|
getTotals | () => Record<string, any> | Per-column footer totals over the filtered rows: { [columnId]: value }. |
clipboard
| Method | Signature | Description |
|---|---|---|
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
| Method | Signature | Description |
|---|---|---|
setGlobalFilter | (filter: string) => void | Set the quick-search text (resets to page 1). |
clearGlobalFilter | () => void | Clear the quick-search text. |
setColumnFilters | (filters: ColumnFilterState, isApply?: boolean) => void | Replace the column-filter state; isApply: true applies it (resets the page, refetches in server mode). |
addColumnFilter | (columnId: string, operator: string, value: any) => void | Add an applied rule for a column (e.g. addColumnFilter('age', 'greaterThan', 30)). |
removeColumnFilter | (filterId: string) => void | Remove one applied rule by its rule id. |
clearAllFilters | () => void | Remove every column-filter rule (applied and pending). |
resetFilters | () => void | Back to initialState.columnFilter, applied. |
openColumnFilter | (columnId: string) => void | Open 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) => void | Remove all applied + pending filter rules for a single column (used by the column menu's "Clear filter"). |
sorting
| Method | Signature | Description |
|---|---|---|
setSorting | (sorting: SortingState) => void | Set the full sort model: [{ id, desc }]. |
sortColumn | (columnId: string, direction: 'asc' | 'desc' | false) => void | Sort by one column: 'asc', 'desc', or false to unsort it. |
clearSorting | () => void | Remove all sorting. |
resetSorting | () => void | Back to initialState.sorting. |
pagination
| Method | Signature | Description |
|---|---|---|
goToPage | (pageIndex: number) => void | Go to a page (0-based), clamped to the pages that exist. |
nextPage | () => void | Next page (stays on the last page). |
previousPage | () => void | Previous page (stays on the first page). |
setPageSize | (pageSize: number) => void | Change the page size and go to page 1. |
goToFirstPage | () => void | Go to page 1. |
goToLastPage | () => void | Go to the last page. |
resetPagination? | () => void | Back to initialState.pagination. |
selection
| Method | Signature | Description |
|---|---|---|
selectRow | (rowId: string) => void | Select a row (respects isRowSelectable). |
deselectRow | (rowId: string) => void | Deselect a row. |
toggleRowSelection | (rowId: string) => void | Select the row if not selected, deselect it if selected. |
selectAll | () => void | selectMode="page": add the current page's rows. "all": select every row. |
deselectAll | () => void | Clear the whole selection (every page). |
toggleSelectAll | () => void | What the header checkbox does: select / deselect the page (or everything in "all" mode). |
getSelectionState | () => SelectionState | The selection as { ids, type: 'include' | 'exclude' } (exclude after select-all in "all" mode). |
getSelectedCount | () => number | How 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) => boolean | Whether a row is selected. |
data
| Method | Signature | Description |
|---|---|---|
refresh | (options?: DataRefreshApiInput) => void | Re-run onFetchData with the current state and call onRefreshData. true / { resetPagination: true } goes to page 1. |
reload | (options?: DataRefreshApiOptions) => void | Like refresh, with meta.reason: 'reload'. |
resetAll | () => void | Reset 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 | undefined | A row's data by id (any loaded row, not just the current page). |
getRowByIndex | (index: number) => T | undefined | The row at a display index on the current page. |
updateRow | (rowId: string, updates: Partial<T>) => void | Merge 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>) => void | Merge changes into the row at an index in the data. |
insertRow | (newRow: T, index?: number) => void | Insert a row (at index, default the end). |
deleteRow | (rowId: string) => void | Remove a row by id. |
deleteRowByIndex | (index: number) => void | Remove the row at an index in the data. |
deleteSelectedRows | () => void | Remove the selected rows and clear the selection. No-op after select-all in selectMode="all" (no explicit id list). |
replaceAllData | (newData: T[]) => void | Replace every row. |
updateMultipleRows | (updates: Array<{ rowId: string; data: Partial<T> }>) => void | Merge changes into several rows: [{ rowId, data }]. |
insertMultipleRows | (newRows: T[], startIndex?: number) => void | Insert several rows (at startIndex, default the end). |
deleteMultipleRows | (rowIds: string[]) => void | Remove several rows by id. |
updateField | (rowId: string, fieldName: keyof T, value: any) => void | Set one field of a row (by id). |
updateFieldByIndex | (index: number, fieldName: keyof T, value: any) => void | Set 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) => number | Index in the data of the first row matching a predicate, or -1. |
getDataCount | () => number | Number of rows held (before filtering). |
getFilteredDataCount | () => number | Number of rows after filtering (client filtering). |
layout
| Method | Signature | Description |
|---|---|---|
reset | (actions?: ResetLayoutAction[]) => void | Reset 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 | () => void | Reset the ENTIRE view state (columns, filters, sorting, pagination, density). |
resetAll | () => void | resetLayout() + reload the data. |
saveLayout | () => SavedLayout | Snapshot of the layout (columns, sizing, pinning, filters, sorting, page, density) to persist yourself. |
restoreLayout | (layout: Partial<SavedLayout>) => void | Apply 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.
| Method | Signature | Description |
|---|---|---|
listViews | () => SavedView[] | All saved views. |
getActiveViewId | () => string | null | Id of the active view, or null for the default. |
getActiveView | () => SavedView | null | The active view, or null for the default. |
saveView | (name: string) => SavedView | Capture the current layout as a new named view and make it active. |
updateView | (id: string) => void | Overwrite a view's captured layout with the current one. |
applyView | (id: string) => void | Apply a saved view's layout and make it active. |
renameView | (id: string, name: string) => void | Rename a view. |
deleteView | (id: string) => void | Delete a view. Deleting the active one switches back to the default view and resets the layout. |
resetView | () => void | Clear the active view and reset the layout to its initial state. |
isDirty | () => boolean | True when the current layout diverges from the active view (or any layout when none is active). |
state
| Method | Signature | Description |
|---|---|---|
getTableState | () => Partial<TableState> | The full TanStack table state. |
getCurrentFilters | () => ColumnFilterState | The column-filter state: applied filters + logic, and the pending* drafts. |
getCurrentSorting | () => SortingState | The sort model: [{ id, desc }]. |
getCurrentPagination | () => PaginationModel | The page: { pageIndex, pageSize }. |
getCurrentSelection | () => SelectionState | Same as selection.getSelectionState(). |
getGlobalFilter | () => string | The quick-search text. |
getLocaleText | () => DataTableLocaleText | The resolved UI strings (your localeText merged over the English defaults). |
export
| Method | Signature | Description |
|---|---|---|
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 | () => boolean | Whether an export is running. |
cancelExport | () => void | Abort the running export. |