DataTable
Easy to use table and datagrids built using TanStack Table .
Want to skip the docs? Try our MCP Server
#
Documentation
#
#
Install
#
Install the required peer dependencies to start using this component:
npm install @tanstack/react-tableDataTable works with both TanStack Table v8 and v9. The demos use v8, except
for features that need v9.
#
Usage
#
Use the table prop to pass the table instance returned from the useReactTable() hook, with columns, data, and getCoreRowModel. A v8 table has every feature, so every accessor column renders a sort button: pass enableSorting: false unless the table sorts its rows, otherwise clicking a header flips the arrow without reordering anything.
On v9, pass the table returned from useTable() with columns, data, and the features the table uses. Register each feature in tableFeatures(), such as rowSelectionFeature for DataTableCheckbox or rowPaginationFeature for DataTableFooter: their controls do nothing without it.
First Name | Last Name | Status | Amount |
|---|---|---|---|
| Arthur | Morgan | success | $20 |
| John | Marston | processing | $12.75 |
| Micah | Bell | failed | $1.25 |
App.tsx
"use client";
import { DataTable, DataTableBody } from "@optiaxiom/react";
import { getCoreRowModel, useReactTable } from "@tanstack/react-table";
import { columns } from "./columns";
import { data } from "./data";
export function App() {
const table = useReactTable({
columns,
data,
enableSorting: false,
getCoreRowModel: getCoreRowModel(),
});
return (
<DataTable table={table}>
<DataTableBody />
</DataTable>
);
}#
Anatomy
#
import { DataTable, DataTableBody, DataTableFooter } from "@optiaxiom/react";
export default () => (
<DataTable>
<DataTableBody>
<DataTableAction />
<DataTableCheckbox />
<DataTableLabel />
</DataTableBody>
<DataTableFooter />
</DataTable>
);#
Vertical scrolling
#
Headers in DataTable are sticky by default. Set a height on the component to allow scrolling vertically when a large number of rows are present.
ID | First Name | Last Name | Amount |
|---|---|---|---|
| 1 | Krystel | Wuckert | $696.47 |
| 2 | Faye | Beier | $684.83 |
| 3 | Cloyd | Nicolas | $398.04 |
| 4 | Nathan | Farrell | $849.44 |
| 5 | Harvey | Kautzer | $293.71 |
| 6 | Teagan | Buckridge | $312.26 |
| 7 | Destany | Leannon-Moore | $317.28 |
| 8 | Johnny | Johnson | $120.62 |
| 9 | Marco | Padberg | $681.30 |
| 10 | Delia | Boyer | $842.35 |
| 11 | Agustin | Cronin | $885.33 |
| 12 | Maxwell | Walsh | $153.07 |
| 13 | Emelie | Ziemann | $841.67 |
| 14 | Maxwell | Fisher | $355.91 |
| 15 | Branson | Okuneva | $343.45 |
| 16 | Garrison | D'Amore | $846.51 |
| 17 | Lelah | Yundt | $829.12 |
| 18 | Katlynn | Gerhold-Maggio | $905.35 |
| 19 | Cara | Wyman | $806.97 |
| 20 | Jazlyn | Gulgowski | $79.36 |
| 21 | Omar | Quitzon | $927.59 |
| 22 | Elinore | Brown | $839.25 |
| 23 | Henri | Mann | $664.87 |
| 24 | Esteban | Marvin | $84.90 |
| 25 | Rebeca | Bode | $751.65 |
| 26 | Lauren | Schroeder | $138.41 |
| 27 | Jordon | DuBuque | $467.99 |
| 28 | Alessandra | Rodriguez | $717.76 |
| 29 | Anahi | Runte-Marvin | $472.91 |
| 30 | Anibal | Kautzer | $102.63 |
| 31 | Cayla | Dibbert | $788.19 |
| 32 | Toby | Kohler | $417.29 |
| 33 | Dejah | Schowalter | $502.22 |
| 34 | Willa | Abshire | $894.98 |
| 35 | Brad | Heaney-Schmeler | $484.91 |
| 36 | Mckenna | Wisoky | $798.85 |
| 37 | Dock | Zemlak | $650.75 |
| 38 | Athena | Nicolas | $236.46 |
| 39 | Georgiana | Metz | $281.23 |
| 40 | Stefan | Sanford | $293.15 |
| 41 | Ryann | Keeling-Swaniawski | $45.89 |
| 42 | Tyrese | Casper | $355.96 |
| 43 | Addie | Swift | $596.49 |
| 44 | Darryl | Gusikowski | $363.57 |
| 45 | London | Kutch | $418.63 |
| 46 | Clemmie | Hintz | $3.53 |
| 47 | Mina | Schulist | $857.16 |
| 48 | Myrtis | Wehner | $335.54 |
| 49 | Edyth | Hansen | $908.02 |
| 50 | Waino | Carroll | $882.28 |
App.tsx
"use client";
import { DataTable, DataTableBody } from "@optiaxiom/react";
import { getCoreRowModel, useReactTable } from "@tanstack/react-table";
import { columns } from "./columns";
import { data } from "./data";
export function App() {
const table = useReactTable({
columns,
data,
enableSorting: false,
getCoreRowModel: getCoreRowModel(),
});
return (
<DataTable maxH="xs" table={table}>
<DataTableBody />
</DataTable>
);
}#
Column sizing
#
Set a column’s width in pixels with the size column option (150 by default). Columns start at size and share any leftover table width in proportion to their size.
To keep a column at exactly size, such as a status or action column, set enableResizing: false on it. On v9, register columnResizingFeature for that option, which also lets users drag the edges of the other columns. The column resizing demo fixes its ID column this way.
#
Column pinning
#
Follow the column pinning guide and use the columnPinning option to pin columns to the left or right of the table (start or end on v9). On v9, register columnPinningFeature and columnSizingFeature.
ID | Username | Email | First Name | Last Name | Job Title | Amount |
|---|---|---|---|---|---|---|
| 1 | Korbin.Hettinger | Megane34@gmail.com | Helena | Nicolas | Legacy Directives Officer | $696.47 |
| 2 | Lily68 | Itzel_Trantow@hotmail.com | Ila | Moore | Human Functionality Analyst | $92.10 |
| 3 | Maxwell_Walsh35 | Marlon.Runolfsdottir@yahoo.com | Lucinda | Cremin | Legacy Solutions Planner | $510.42 |
| 4 | Elwin.Simonis | Harley.Pouros39@hotmail.com | Malvina | Lueilwitz | Direct Research Director | $304.77 |
| 5 | Kristin45 | Cordie_Turcotte@gmail.com | Newton | Keebler | Corporate Communications Facilitator | $521.53 |
| 6 | Ollie82 | Claud_Haag@gmail.com | Henri | Schultz | District Accounts Planner | $741.86 |
| 7 | Norberto65 | Isabelle46@gmail.com | Therese | Barrows | Customer Implementation Developer | $128.63 |
| 8 | Ignacio.Beahan9 | Maiya.Leannon@hotmail.com | Essie | Von | Internal Directives Administrator | $769.40 |
| 9 | Vilma.Bailey50 | Willa.Abshire@yahoo.com | Vesta | Rau | National Data Consultant | $807.79 |
| 10 | Alma42 | Athena.Nicolas36@gmail.com | Carla | Armstrong | Customer Response Orchestrator | $993.04 |
| 11 | Jean_White50 | Rickie.Metz72@yahoo.com | Serena | Okuneva | National Operations Planner | $355.96 |
| 12 | Leopold88 | Hilda68@yahoo.com | Ervin | Wehner | National Markets Producer | $480.89 |
| 13 | Dawson46 | Beaulah_Bosco@yahoo.com | Cordelia | Powlowski | Future Mobility Designer | $709.63 |
| 14 | Drew.Vandervort | Paolo_Kovacek67@gmail.com | Trinity | Erdman | Central Functionality Coordinator | $237.48 |
| 15 | Annamarie.Jacobi95 | Twila.Moore@gmail.com | Edwin | Jast-O'Reilly | Chief Creative Director | $444.22 |
| 16 | Joany23 | Sarah_VonRueden@hotmail.com | Rosalee | Bailey | Regional Branding Representative | $625.21 |
| 17 | Betty98 | Modesto.Zulauf64@gmail.com | Bill | Klein | Investor Mobility Technician | $134.61 |
| 18 | Jabari.Zemlak22 | Geoffrey.Trantow42@hotmail.com | Ray | Kiehn | Human Factors Specialist | $919.48 |
| 19 | Johnson_Dare75 | Alfred_Beer37@gmail.com | Marcus | Schmeler | Future Operations Designer | $147.72 |
| 20 | Rusty60 | Deondre_Rowe@gmail.com | Patricia | Kemmer-Torp | Central Division Specialist | $817.10 |
| 21 | Darrin47 | Demarco.Jacobson@gmail.com | Alex | Koss | Lead Quality Developer | $715.75 |
| 22 | Merritt_Welch62 | Shea_Rolfson-Lindgren@gmail.com | Enrico | Koepp-Paucek | Corporate Applications Engineer | $31.66 |
| 23 | Keaton.Breitenberg | Franco_Buckridge80@yahoo.com | Nelle | McGlynn | Internal Directives Strategist | $225.53 |
| 24 | Zoe29 | Ottis.Blick46@hotmail.com | Jayme | Ryan | Direct Implementation Engineer | $328.33 |
| 25 | May_Gottlieb-Bernier69 | Zetta_Mertz66@yahoo.com | Anastacio | Stokes | International Implementation Supervisor | $15.01 |
| 26 | Omari_McGlynn | Dandre_Runte@hotmail.com | Talon | Goyette | Chief Interactions Producer | $338.58 |
| 27 | Eugenia.Spencer41 | Clinton_OHara22@yahoo.com | Felipe | Langworth | Future Research Strategist | $554.41 |
| 28 | Victoria.Ernser | Ashleigh.Hudson@yahoo.com | Tara | Pollich | Global Functionality Strategist | $205.35 |
| 29 | Leonie.Fisher | Devyn.Reilly91@gmail.com | Faustino | Kunde-Rolfson | Investor Functionality Manager | $83.34 |
| 30 | Josephine_Hickle32 | Victor87@yahoo.com | Rico | Morar | Lead Branding Technician | $393.42 |
| 31 | Dakota72 | Jeff42@yahoo.com | Favian | Hoppe | Dynamic Directives Executive | $751.12 |
| 32 | Garland33 | Maryann_Powlowski@yahoo.com | Alanis | Batz | Global Group Liaison | $668.00 |
| 33 | Lynn.Johns | Miracle39@gmail.com | Benedict | Friesen | Direct Accountability Architect | $458.82 |
| 34 | Vella_Beer | Katie_Lang@hotmail.com | Ressie | Halvorson | Product Applications Supervisor | $497.47 |
| 35 | Issac_OConnell | Elody_Green49@yahoo.com | Leah | Bernhard | Direct Branding Director | $264.40 |
| 36 | Terence0 | Janie.Dickinson17@yahoo.com | Amos | Hackett | Chief Integration Liaison | $447.36 |
| 37 | Zackery_Zemlak16 | Angela62@yahoo.com | Zelda | Armstrong | Global Data Supervisor | $356.24 |
| 38 | Brandy75 | Darrell.Marvin44@gmail.com | Angus | Marvin | International Brand Designer | $680.19 |
| 39 | Maya97 | Sallie.Crona@gmail.com | Meggie | Bruen | Legacy Directives Director | $957.85 |
| 40 | Isai.Beahan | Maximus.Schmidt@yahoo.com | Retha | Swaniawski | Direct Factors Executive | $673.63 |
| 41 | Meredith86 | Ashleigh_Raynor29@gmail.com | Ashleigh | Shanahan | Lead Response Officer | $493.83 |
| 42 | Garrick71 | Merritt84@yahoo.com | Hassie | Spencer | Central Factors Consultant | $193.32 |
| 43 | Guido.Crist59 | Morris.Witting61@yahoo.com | Myah | Quitzon | Product Operations Officer | $863.86 |
| 44 | Eden27 | Lilian.Collier@yahoo.com | Loyal | Rau-Schowalter | Legacy Infrastructure Supervisor | $238.55 |
| 45 | Jonatan80 | Sadie.Huels86@gmail.com | Cathryn | Schimmel | Investor Mobility Officer | $347.06 |
| 46 | Leola_Block67 | Jensen_Leannon5@gmail.com | Dell | Daugherty | Lead Data Facilitator | $704.51 |
| 47 | Danial80 | Leatha95@yahoo.com | Delaney | Bergnaum | Dynamic Implementation Representative | $828.99 |
| 48 | Archibald_Sipes92 | Seamus.Corkery54@gmail.com | Fabian | Blick | Regional Data Director | $711.01 |
| 49 | Alonzo8 | Roel91@gmail.com | Ezekiel | Collier | District Marketing Analyst | $937.37 |
| 50 | Louis75 | Jensen_Stamm53@hotmail.com | Jeannette | Dooley | Forward Web Developer | $3.65 |
App.tsx
"use client";
import { DataTable, DataTableBody } from "@optiaxiom/react";
import { getCoreRowModel, useReactTable } from "@tanstack/react-table";
import { columns } from "./columns";
import { data } from "./data";
export function App() {
const table = useReactTable({
columns,
data,
enableSorting: false,
getCoreRowModel: getCoreRowModel(),
state: {
columnPinning: {
left: ["id", "username"],
},
},
});
return (
<DataTable maxH="xs" table={table}>
<DataTableBody />
</DataTable>
);
}#
Column resizing
#
Every v8 column is resizable by default: users drag the edge of a column header to resize it, and double click the edge to reset it. Set enableResizing: false on a column to keep its width fixed. On v9, register columnSizingFeature and columnResizingFeature to resize columns.
By default the new width applies when the drag ends. Set columnResizeMode: "onChange" to resize the column while dragging.
Hover over the column headers and drag their edges to resize:
ID | First Name | Last Name | Amount |
|---|---|---|---|
| 1 | Krystel | Wuckert | $696.47 |
| 2 | Faye | Beier | $684.83 |
| 3 | Cloyd | Nicolas | $398.04 |
| 4 | Nathan | Farrell | $849.44 |
| 5 | Harvey | Kautzer | $293.71 |
| 6 | Teagan | Buckridge | $312.26 |
| 7 | Destany | Leannon-Moore | $317.28 |
| 8 | Johnny | Johnson | $120.62 |
| 9 | Marco | Padberg | $681.30 |
| 10 | Delia | Boyer | $842.35 |
| 11 | Agustin | Cronin | $885.33 |
| 12 | Maxwell | Walsh | $153.07 |
| 13 | Emelie | Ziemann | $841.67 |
| 14 | Maxwell | Fisher | $355.91 |
| 15 | Branson | Okuneva | $343.45 |
| 16 | Garrison | D'Amore | $846.51 |
| 17 | Lelah | Yundt | $829.12 |
| 18 | Katlynn | Gerhold-Maggio | $905.35 |
| 19 | Cara | Wyman | $806.97 |
| 20 | Jazlyn | Gulgowski | $79.36 |
| 21 | Omar | Quitzon | $927.59 |
| 22 | Elinore | Brown | $839.25 |
| 23 | Henri | Mann | $664.87 |
| 24 | Esteban | Marvin | $84.90 |
| 25 | Rebeca | Bode | $751.65 |
| 26 | Lauren | Schroeder | $138.41 |
| 27 | Jordon | DuBuque | $467.99 |
| 28 | Alessandra | Rodriguez | $717.76 |
| 29 | Anahi | Runte-Marvin | $472.91 |
| 30 | Anibal | Kautzer | $102.63 |
| 31 | Cayla | Dibbert | $788.19 |
| 32 | Toby | Kohler | $417.29 |
| 33 | Dejah | Schowalter | $502.22 |
| 34 | Willa | Abshire | $894.98 |
| 35 | Brad | Heaney-Schmeler | $484.91 |
| 36 | Mckenna | Wisoky | $798.85 |
| 37 | Dock | Zemlak | $650.75 |
| 38 | Athena | Nicolas | $236.46 |
| 39 | Georgiana | Metz | $281.23 |
| 40 | Stefan | Sanford | $293.15 |
| 41 | Ryann | Keeling-Swaniawski | $45.89 |
| 42 | Tyrese | Casper | $355.96 |
| 43 | Addie | Swift | $596.49 |
| 44 | Darryl | Gusikowski | $363.57 |
| 45 | London | Kutch | $418.63 |
| 46 | Clemmie | Hintz | $3.53 |
| 47 | Mina | Schulist | $857.16 |
| 48 | Myrtis | Wehner | $335.54 |
| 49 | Edyth | Hansen | $908.02 |
| 50 | Waino | Carroll | $882.28 |
App.tsx
"use client";
import { DataTable, DataTableBody } from "@optiaxiom/react";
import { getCoreRowModel, useReactTable } from "@tanstack/react-table";
import { columns } from "./columns";
import { data } from "./data";
export function App() {
const table = useReactTable({
columns,
data,
enableSorting: false,
getCoreRowModel: getCoreRowModel(),
});
return (
<DataTable maxH="xs" table={table}>
<DataTableBody />
</DataTable>
);
}#
Row selection
#
Follow the row selection guide to allow selecting and highlighting rows. On v9, register rowSelectionFeature.
Use the DataTableCheckbox component to automatically attach selection handlers and make the whole row selectable.
Combine Checkbox/DataTableCheckbox with Cover to make them easier to interact with by spanning the whole cell.
Finally, make sure to use the DataTableLabel in your primary cell to set the label of the row and the checkbox for screen readers.
Hover over the rows to see the checkboxes:
ID | Name | Amount | |
|---|---|---|---|
| Check to select | 1 | Krystel Kassulke MD | $696.47 |
| Check to select | 2 | Meredith Beier | $480.93 |
| Check to select | 3 | Mrs. Kelley Nicolas | $738.00 |
| Check to select | 4 | Dana Murray | $722.45 |
| Check to select | 5 | Itzel Kautzer DVM | $433.70 |
App.tsx
"use client";
import { DataTable, DataTableBody } from "@optiaxiom/react";
import { getCoreRowModel, useReactTable } from "@tanstack/react-table";
import { columns } from "./columns";
import { data } from "./data";
export function App() {
const table = useReactTable({
columns,
data,
enableSorting: false,
getCoreRowModel: getCoreRowModel(),
});
return (
<DataTable table={table}>
<DataTableBody />
</DataTable>
);
}#
Row actions
#
Use the DataTableAction component to show links, buttons, or dropdown menus within rows that are only visible when the user hovers over the row.
You can enable the primary prop on any one of those actions to mark that as the primary action. This will automatically enable keyboard navigation within the table:
- Double click on a row to perform the primary action. When the table has no row selection (
enableRowSelection: false, or a v9 table withoutrowSelectionFeature), or usesselectionBehavior="toggle", a single click does too. - Use up/down/left/right arrow keys to navigate between rows and actions.
Finally, make sure to use the DataTableLabel in your primary cell to set the label of the row for screen readers.
Hover over the rows to perform more actions:
Name | Modified | Modified By |
|---|---|---|
Dec 4, 2026 | Krystel Kassulke MD | |
Dec 4, 2026 | Miracle Daniel | |
Dec 4, 2026 | Forest Greenholt | |
Dec 4, 2026 | Shyanne Kuvalis | |
Dec 4, 2026 | Mrs. Kayden Cartwright |
App.tsx
"use client";
import { DataTable, DataTableBody } from "@optiaxiom/react";
import { getCoreRowModel, useReactTable } from "@tanstack/react-table";
import { columns } from "./columns";
import { data } from "./data";
export function App() {
const table = useReactTable({
columns,
data,
enableRowSelection: false,
enableSorting: false,
getCoreRowModel: getCoreRowModel(),
});
return (
<DataTable table={table}>
<DataTableBody />
</DataTable>
);
}#
Selection behavior
#
By default (selectionBehavior="replace"), a click selects only the clicked row, a double click performs the primary action, and the arrow keys move the selection.
Set selectionBehavior="toggle" on DataTable when a click should run the row’s action instead, such as opening a row while other rows stay checked:
- Click a row to perform its primary action. A row without one toggles its selection.
- Use the checkbox or Space to select a row, and Shift with the up/down arrow keys to extend the selection. The arrow keys alone only move between rows.
Name | Actions | |
|---|---|---|
| Check to select | Krystel Kassulke MD | |
| Check to select | Meredith Beier | |
| Check to select | Mrs. Kelley Nicolas | |
| Check to select | Dana Murray | |
| Check to select | Itzel Kautzer DVM |
App.tsx
"use client";
import { DataTable, DataTableBody } from "@optiaxiom/react";
import { getCoreRowModel, useReactTable } from "@tanstack/react-table";
import { columns } from "./columns";
import { data } from "./data";
export function App() {
const table = useReactTable({
columns,
data,
enableSorting: false,
getCoreRowModel: getCoreRowModel(),
});
return (
<DataTable selectionBehavior="toggle" table={table}>
<DataTableBody />
</DataTable>
);
}#
Highlighted row
#
DataTable keeps track of the row the keyboard is on: focusing a row, clicking it and the arrow keys move this highlight, and Tab returns to it. On TanStack Table v9, register rowHighlightingFeature to keep it in table state, so the app can follow it (for example to show the row in a details pane) or set it:
import { rowHighlightingFeature } from "@optiaxiom/react";
const features = tableFeatures({ rowHighlightingFeature });
const [highlightedRowId, setHighlightedRowId] = useState<null | string>(null);
const table = useTable({
columns,
data,
features,
getRowId: (row) => row.id,
onHighlightedRowIdChange: setHighlightedRowId,
state: { highlightedRowId },
});The value is the TanStack row id, so set getRowId to keep it stable across sorting and pagination. table.setHighlightedRowId() moves the tab stop without moving focus, and cells can read row.getIsHighlighted().
A newly highlighted row scrolls into view when it isn’t already, even when the table virtualizes it out of the DOM. Highlight a row the user can focus, one with a primary DataTableAction or a DataTableCheckbox, so Tab can reach it. To return to a list at the row the user left from, pass its id as initialState.highlightedRowId, and loading to DataTableBody while the rows load: the table scrolls to it once they have. A row on another page is not on the table yet, so set the page first.
Name | Actions |
|---|---|
Krystel Kassulke MD | |
Meredith Beier | |
Mrs. Kelley Nicolas | |
Dana Murray | |
Itzel Kautzer DVM |
Tab into the table and use the arrow keys.
App.tsx
"use client";
import { DataTable, DataTableBody, Flex, Text } from "@optiaxiom/react";
import { useTable } from "@tanstack/react-table";
import { useState } from "react";
import { columns, features } from "./columns";
import { data } from "./data";
export function App() {
const [highlightedRowId, setHighlightedRowId] = useState<null | string>(null);
const table = useTable({
columns,
data,
features,
getRowId: (row) => String(row.id),
onHighlightedRowIdChange: setHighlightedRowId,
state: { highlightedRowId },
});
const highlighted = highlightedRowId
? table.getRowModel().rowsById[highlightedRowId]?.original
: undefined;
return (
<Flex w="full">
<DataTable table={table}>
<DataTableBody />
</DataTable>
<Text role="status">
{highlighted
? `Highlighted: ${highlighted.name}`
: "Tab into the table and use the arrow keys."}
</Text>
</Flex>
);
}#
Row intent
#
On TanStack Table v9, register rowIntentFeature to color rows. getRowIntent reads each row, so the app keeps the state the color describes:
import { rowIntentFeature } from "@optiaxiom/react";
const features = tableFeatures({ rowIntentFeature, rowSelectionFeature });
const table = useTable({
columns,
data,
features,
getRowIntent: (row) => (row.id === openRowId ? "primary" : undefined),
});primary gives a row the selected rows’ color without selecting it, such as the row shown in a details pane. A row with an intent keeps its color on hover.
Name | Actions | |
|---|---|---|
| Check to select | Homepage hero | |
| Check to select | Spring campaign | |
| Check to select | Pricing table | |
| Check to select | Partner logos | |
| Check to select | Footer links |
App.tsx
"use client";
import { DataTable, DataTableBody } from "@optiaxiom/react";
import { useTable } from "@tanstack/react-table";
import { useMemo, useState } from "react";
import { columns, features } from "./columns";
import { data } from "./data";
export function App() {
const [openId, setOpenId] = useState<null | string>("2");
const table = useTable({
columns: useMemo(() => columns(setOpenId), []),
data,
features,
getRowId: (row) => row.id,
getRowIntent: (row) => (row.id === openId ? "primary" : undefined),
});
return (
<DataTable selectionBehavior="toggle" table={table}>
<DataTableBody />
</DataTable>
);
}#
Pagination
#
DataTable supports both client-side and server-side pagination. Follow the pagination guide to learn how to implement pagination with TanStack Table.
#
Client side
#
Provide getPaginationRowModel: getPaginationRowModel() and set the pagination state to use client-side pagination. On v9, register rowPaginationFeature with paginatedRowModel: createPaginatedRowModel() instead.
ID | First Name | Last Name | Amount |
|---|---|---|---|
| 1 | Krystel | Wuckert | $696.47 |
| 2 | Faye | Beier | $684.83 |
| 3 | Cloyd | Nicolas | $398.04 |
| 4 | Nathan | Farrell | $849.44 |
| 5 | Harvey | Kautzer | $293.71 |
| 6 | Teagan | Buckridge | $312.26 |
| 7 | Destany | Leannon-Moore | $317.28 |
| 8 | Johnny | Johnson | $120.62 |
| 9 | Marco | Padberg | $681.30 |
| 10 | Delia | Boyer | $842.35 |
1 - 10 of 50
App.tsx
"use client";
import { DataTable, DataTableBody, DataTableFooter } from "@optiaxiom/react";
import {
getCoreRowModel,
getPaginationRowModel,
useReactTable,
} from "@tanstack/react-table";
import { columns } from "./columns";
import { data } from "./data";
export function App() {
const table = useReactTable({
columns,
data,
enableSorting: false,
getCoreRowModel: getCoreRowModel(),
getPaginationRowModel: getPaginationRowModel(),
initialState: { pagination: { pageIndex: 0, pageSize: 10 } },
});
return (
<DataTable maxH="xs" table={table}>
<DataTableBody />
<DataTableFooter />
</DataTable>
);
}#
Server side
#
To implement manual server-side pagination we can use manualPagination and rowCount, with rowPaginationFeature registered on v9. Then combine it with onPaginationChange and state.pagination to control pagination and react to changes.
ID | First Name | Last Name | Amount |
|---|---|---|---|
| 1 | Krystel | Wuckert | $696.47 |
| 2 | Faye | Beier | $684.83 |
| 3 | Cloyd | Nicolas | $398.04 |
| 4 | Nathan | Farrell | $849.44 |
| 5 | Harvey | Kautzer | $293.71 |
| 6 | Teagan | Buckridge | $312.26 |
| 7 | Destany | Leannon-Moore | $317.28 |
| 8 | Johnny | Johnson | $120.62 |
| 9 | Marco | Padberg | $681.30 |
| 10 | Delia | Boyer | $842.35 |
1 - 10 of 50
App.tsx
"use client";
import { DataTable, DataTableBody, DataTableFooter } from "@optiaxiom/react";
import { getCoreRowModel, useReactTable } from "@tanstack/react-table";
import { useMemo, useState } from "react";
import { columns } from "./columns";
import { data } from "./data";
/**
* Simulate fetching data from server.
*/
const fetchData = ({ pageIndex = 0, pageSize = 10 }) =>
data.slice(pageIndex * pageSize, (pageIndex + 1) * pageSize);
export function App() {
const [pagination, setPagination] = useState({
pageIndex: 0,
pageSize: 10,
});
const table = useReactTable({
columns,
data: useMemo(() => fetchData(pagination), [pagination]),
enableSorting: false,
getCoreRowModel: getCoreRowModel(),
manualPagination: true,
onPaginationChange: setPagination,
rowCount: data.length,
state: { pagination },
});
return (
<DataTable maxH="xs" table={table}>
<DataTableBody />
<DataTableFooter />
</DataTable>
);
}#
Loading state
#
Set the loading prop to true to show a loading skeleton while your table data is loading.
ID | First Name | Last Name | Amount |
|---|---|---|---|
App.tsx
"use client";
import { DataTable, DataTableBody, Group, Switch } from "@optiaxiom/react";
import { getCoreRowModel, useReactTable } from "@tanstack/react-table";
import { useState } from "react";
import { columns } from "./columns";
export function App() {
const [data] = useState([]);
const table = useReactTable({
columns,
data,
enableSorting: false,
getCoreRowModel: getCoreRowModel(),
});
const [loading, setLoading] = useState(true);
return (
<Group alignItems="start" flexDirection="column" gap="16" maxW="full">
<Switch checked={loading} onCheckedChange={setLoading}>
Loading
</Switch>
<DataTable maxH="xs" table={table}>
<DataTableBody loading={loading} />
</DataTable>
</Group>
);
}Alternatively you can provide an object to the loading prop to denote individual loading state of sub rows.
ID | First Name | Last Name | Amount |
|---|---|---|---|
| 1 | Krystel | Wuckert | $696.47 |
| 2 | Faye | Beier | $684.83 |
| 3 | Cloyd | Nicolas | $398.04 |
App.tsx
"use client";
import { DataTable, DataTableBody, Group, Switch } from "@optiaxiom/react";
import { getCoreRowModel, useReactTable } from "@tanstack/react-table";
import { useState } from "react";
import { columns } from "./columns";
import { data } from "./data";
export function App() {
const table = useReactTable({
columns,
data,
enableSorting: false,
getCoreRowModel: getCoreRowModel(),
getRowId: (row) => row.id.toString(),
});
const [loading, setLoading] = useState<Record<string, "sub-rows" | false>>({
"1": "sub-rows",
});
return (
<Group alignItems="start" flexDirection="column" gap="16" maxW="full">
<Switch
checked={loading["1"] === "sub-rows"}
onCheckedChange={(checked) =>
setLoading({ "1": checked ? "sub-rows" : false })
}
>
Loading
</Switch>
<DataTable maxH="xs" table={table}>
<DataTableBody loading={loading} />
</DataTable>
</Group>
);
}#
Empty state
#
Use the empty prop on DataTableBody to show content in a full-width row when the table has no rows.
ID | First Name | Last Name | Amount |
|---|---|---|---|
No payments yet. | |||
App.tsx
"use client";
import { DataTable, DataTableBody } from "@optiaxiom/react";
import { getCoreRowModel, useReactTable } from "@tanstack/react-table";
import { columns } from "./columns";
const data: never[] = [];
export function App() {
const table = useReactTable({
columns,
data,
enableSorting: false,
getCoreRowModel: getCoreRowModel(),
});
return (
<DataTable table={table}>
<DataTableBody empty="No payments yet." />
</DataTable>
);
}#
Sorting
#
DataTable supports both client-side and server-side sorting. Follow the sorting guide to learn how to implement sorting with TanStack Table.
#
Client side
#
Provide getSortedRowModel: getSortedRowModel() to sort rows on the client. On v9, register rowSortingFeature with sortedRowModel: createSortedRowModel() and sortFns instead. Every accessor column is then sortable; set enableSorting: false on a column to opt it out.
Hover over and click the table headers to sort by columns:
ID | |||
|---|---|---|---|
| 1 | Krystel | Wuckert | $696.47 |
| 2 | Faye | Beier | $684.83 |
| 3 | Cloyd | Nicolas | $398.04 |
| 4 | Nathan | Farrell | $849.44 |
| 5 | Harvey | Kautzer | $293.71 |
| 6 | Teagan | Buckridge | $312.26 |
| 7 | Destany | Leannon-Moore | $317.28 |
| 8 | Johnny | Johnson | $120.62 |
| 9 | Marco | Padberg | $681.30 |
| 10 | Delia | Boyer | $842.35 |
App.tsx
"use client";
import { DataTable, DataTableBody } from "@optiaxiom/react";
import {
getCoreRowModel,
getSortedRowModel,
useReactTable,
} from "@tanstack/react-table";
import { columns } from "./columns";
import { data } from "./data";
export function App() {
const table = useReactTable({
columns,
data,
getCoreRowModel: getCoreRowModel(),
getSortedRowModel: getSortedRowModel(),
});
return (
<DataTable maxH="xs" table={table}>
<DataTableBody />
</DataTable>
);
}#
Server side
#
To sort on the server, register rowSortingFeature on v9, control sorting with onSortingChange and state.sorting, and pass rows in the order the server returns them. Leave out getSortedRowModel (sortedRowModel on v9), or set manualSorting, so the table keeps that order.
Hover over and click the table headers to sort by columns:
ID | |||
|---|---|---|---|
| 3 | Cloyd | Nicolas | $398.04 |
| 10 | Delia | Boyer | $842.35 |
| 7 | Destany | Leannon-Moore | $317.28 |
| 2 | Faye | Beier | $684.83 |
| 5 | Harvey | Kautzer | $293.71 |
| 8 | Johnny | Johnson | $120.62 |
| 1 | Krystel | Wuckert | $696.47 |
| 9 | Marco | Padberg | $681.30 |
| 4 | Nathan | Farrell | $849.44 |
| 6 | Teagan | Buckridge | $312.26 |
App.tsx
"use client";
import { DataTable, DataTableBody } from "@optiaxiom/react";
import {
getCoreRowModel,
type SortingState,
useReactTable,
} from "@tanstack/react-table";
import { useMemo, useState } from "react";
import { columns } from "./columns";
import { data } from "./data";
export function App() {
const [sorting, setSorting] = useState([
{
desc: false,
id: "firstName",
},
]);
const table = useReactTable({
columns,
data: useMemo(() => sortData(sorting), [sorting]),
getCoreRowModel: getCoreRowModel(),
onSortingChange: setSorting,
state: { sorting },
});
return (
<DataTable maxH="xs" table={table}>
<DataTableBody />
</DataTable>
);
}
/**
* Simulate sorting data from server.
*/
const sortData = (sorting: SortingState) =>
data.toSorted((a, b) => {
for (const state of sorting) {
if (!(
state.id === "amount" ||
state.id === "firstName" ||
state.id === "lastName"
)) {
return 0;
}
const result =
(state.desc ? -1 : 1) *
a[state.id].localeCompare(b[state.id], undefined, { numeric: true });
if (result !== 0) {
return result;
}
}
return 0;
});#
List view
#
A collection page combines a search input and filter menus above the table with a primary create action. Wire search to globalFilter and each menu to its column’s filter value, add the filtered and paginated row models (getFilteredRowModel and getPaginationRowModel, or filteredRowModel and paginatedRowModel with their features on v9), and TanStack resets to the first page whenever the criteria change. Put row commands in a trailing column behind one EllipsisMenuButton, and use empty to offer a way out when nothing matches.
Name | Status | Owner | |
|---|---|---|---|
Item 1 | Draft | Ada Lovelace | |
Item 2 | Ready | Grace Hopper | |
Item 3 | Ready | Alan Turing | |
Item 4 | Draft | Ada Lovelace | |
Item 5 | Ready | Grace Hopper | |
Item 6 | Ready | Alan Turing | |
Item 7 | Draft | Ada Lovelace | |
Item 8 | Ready | Grace Hopper | |
Item 9 | Ready | Alan Turing | |
Item 10 | Draft | Ada Lovelace |
1 - 10 of 24
App.tsx
"use client";
import { IconPlus } from "@optiaxiom/icons";
import {
Button,
DataTable,
DataTableBody,
DataTableFooter,
Group,
LabelMenuButton,
Menu,
MenuContent,
MenuTrigger,
SearchInput,
toaster,
} from "@optiaxiom/react";
import {
type ColumnFiltersState,
getCoreRowModel,
getFilteredRowModel,
getPaginationRowModel,
useReactTable,
} from "@tanstack/react-table";
import { useRef, useState } from "react";
import { columns } from "./columns";
import { data } from "./data";
const filters = [
{ id: "status", label: "Status", values: ["Draft", "Ready"] },
{
id: "owner",
label: "Owner",
values: [...new Set(data.map((item) => item.owner))].sort(),
},
];
export function App() {
const searchRef = useRef<HTMLInputElement>(null);
const [globalFilter, setGlobalFilter] = useState("");
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([]);
const table = useReactTable({
columns,
data,
enableSorting: false,
getCoreRowModel: getCoreRowModel(),
getFilteredRowModel: getFilteredRowModel(),
getPaginationRowModel: getPaginationRowModel(),
globalFilterFn: "includesString",
initialState: { pagination: { pageIndex: 0, pageSize: 10 } },
onColumnFiltersChange: setColumnFilters,
onGlobalFilterChange: setGlobalFilter,
state: { columnFilters, globalFilter },
});
return (
<Group flexDirection="column" gap="16" maxW="full" w="full">
<Group flexWrap="wrap" gap="12" justifyContent="space-between">
<Group flexWrap="wrap" gap="8">
<SearchInput
aria-label="Search items"
onValueChange={setGlobalFilter}
placeholder="Search items"
ref={searchRef}
size="lg"
value={globalFilter}
/>
{filters.map((filter) => {
const column = table.getColumn(filter.id);
const value = column?.getFilterValue() as string | undefined;
return (
<Menu
key={filter.id}
options={[undefined, ...filter.values].map((option) => ({
execute: () => column?.setFilterValue(option),
label: option ?? "All",
selected: value === option,
}))}
>
<MenuTrigger asChild>
<LabelMenuButton label={filter.label}>
{value ?? "All"}
</LabelMenuButton>
</MenuTrigger>
<MenuContent />
</Menu>
);
})}
</Group>
<Button
appearance="primary"
icon={<IconPlus />}
onClick={() => toaster.create("Create item")}
size="lg"
>
Create item
</Button>
</Group>
<DataTable table={table}>
<DataTableBody
empty={
<>
No items match your filters.
<Button
appearance="subtle"
mt="8"
onClick={() => {
setGlobalFilter("");
setColumnFilters([]);
searchRef.current?.focus();
}}
>
Clear filters
</Button>
</>
}
/>
<DataTableFooter />
</DataTable>
</Group>
);
}#
Expanding rows ALPHA
#
Follow the expanding guide to allow expanding and collapsing rows with getExpandedRowModel and getSubRows. On v9, register rowExpandingFeature with expandedRowModel: createExpandedRowModel().
Use the DataTableExpandableHeader and DataTableExpandableCell components to render columns with built-in buttons for toggling rows.
Finally, combine it with the loading prop to show a loading state while expanded rows are being fetched. On v9, set autoResetExpanded: false when fetched rows go into data: v9 collapses every row whenever data changes.
ID | Name | Amount |
|---|---|---|
| 1 | Clothing | $1415.94 |
| 2 | Books | $919.50 |
App.tsx
"use client";
import { DataTable, DataTableBody } from "@optiaxiom/react";
import {
getCoreRowModel,
getExpandedRowModel,
useReactTable,
} from "@tanstack/react-table";
import { columns } from "./columns";
import { data } from "./data";
export function App() {
const table = useReactTable({
columns,
data,
enableSorting: false,
getCoreRowModel: getCoreRowModel(),
getExpandedRowModel: getExpandedRowModel(),
getSubRows: (row) => row.items,
});
return (
<DataTable table={table}>
<DataTableBody />
</DataTable>
);
}#
Virtualized
#
Table automatically uses virtualized rendering when rows and columns cross a certain threshold to improve rendering performance.
Columns will be virtualized when there are more than 20 columns in a table and rows will be virtualized when there are more than 20 rows rendered or if columns are also being virtualized.
Timer: 0s (to simulate re-rendering)
Column 1 | Column 2 | Column 3 | Column 4 | Column 5 | Column 6 | Column 7 | Column 8 | Column 9 | Column 10 | Column 11 | Column 12 | Column 13 | Column 14 | Column 15 | Column 16 | Column 17 | Column 18 | Column 19 | Column 20 | Column 21 | Column 22 | Column 23 | Column 24 | Column 25 | Column 26 | Column 27 | Column 28 | Column 29 | Column 30 | Column 31 | Column 32 | Column 33 | Column 34 | Column 35 | Column 36 | Column 37 | Column 38 | Column 39 | Column 40 | Column 41 | Column 42 | Column 43 | Column 44 | Column 45 | Column 46 | Column 47 | Column 48 | Column 49 | Column 50 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
App.tsx
"use client";
import { faker } from "@faker-js/faker";
import { DataTable, DataTableBody, Group, Text } from "@optiaxiom/react";
import { getCoreRowModel, useReactTable } from "@tanstack/react-table";
import { useInViewTimer } from "./useInViewTimer";
faker.seed(123);
const cols = 50;
const rows = 500;
const data = Array.from({ length: rows }, () =>
Object.fromEntries(
Array.from({ length: cols }, (_, index) => [
`col${index}`,
faker.book.title(),
]),
),
);
const columns = Array.from({ length: cols }, (_, index) => ({
accessorKey: `col${index}`,
header: `Column ${index + 1}`,
}));
export function App() {
const table = useReactTable({
columns,
data,
enableSorting: false,
getCoreRowModel: getCoreRowModel(),
});
/**
* A simple hook to re-render the table when it is visible on the screen.
*/
const [count, ref] = useInViewTimer();
return (
<Group flexDirection="column" gap="16" maxW="full" ref={ref}>
<Text>Timer: {count}s (to simulate re-rendering)</Text>
<DataTable maxH="sm" table={table}>
<DataTableBody />
</DataTable>
</Group>
);
}#
Related
#
Table
Display tabular data using rows and columns.
Pagination
Display active page and navigate between multiple pages.
#
Props
#
#
DataTable
#
import { DataTable } from "@optiaxiom/react";
// or
import { DataTable } from "@optiaxiom/react/data-table";Supports all Box props in addition to its own. Renders a <div> element.
Prop |
|---|
asChildChange the default rendered element for the one passed as a child, merging their props and behavior. Read the Composition guide for more details.
|
children
|
className
|
selectionBehaviorWhat a row click and the arrow keys do in a table with row selection.
Default: |
table*Pass the table instance returned from TanStack Table’s
|
#
DataTableBody
#
import { DataTableBody } from "@optiaxiom/react";
// or
import { DataTableBody } from "@optiaxiom/react/data-table";Supports all Box props in addition to its own. Renders a <div> element.
Prop |
|---|
asChildChange the default rendered element for the one passed as a child, merging their props and behavior. Read the Composition guide for more details.
|
children
|
className
|
emptyContent to show in a full-width row when the table has no rows and isn’t loading.
|
estimatedRowHeightThe estimated height of rows in pixels when virtualization is enabled.
Default: |
loadingWhether to show skeleton for the whole table or specific rows.
|
#
DataTableFooter
#
import { DataTableFooter } from "@optiaxiom/react";
// or
import { DataTableFooter } from "@optiaxiom/react/data-table";Supports all Box props in addition to its own. Renders a <div> element.
Prop |
|---|
asChildChange the default rendered element for the one passed as a child, merging their props and behavior. Read the Composition guide for more details.
|
children
|
className
|
pageSizeOptionsSpecify a custom set of page size options.
Default: |
showPageSizeOptionsWhether to show a dropdown that allows controlling how many rows to show per page.
|
#
DataTableAction
#
import { DataTableAction } from "@optiaxiom/react";
// or
import { DataTableAction } from "@optiaxiom/react/data-table";Supports all Box props in addition to its own. Renders a <div> element.
Prop |
|---|
asChildChange the default rendered element for the one passed as a child, merging their props and behavior. Read the Composition guide for more details.
|
children
|
className
|
primaryWhether this is the primary action within the row.
|
visibleControl whether to always show the contents or only when user is hovering or interacting with them.
|
#
DataTableCheckbox
#
import { DataTableCheckbox } from "@optiaxiom/react";
// or
import { DataTableCheckbox } from "@optiaxiom/react/data-table";Supports all Box props in addition to its own. Renders a <div> element.
Prop |
|---|
asChildChange the default rendered element for the one passed as a child, merging their props and behavior. Read the Composition guide for more details.
|
checkedWhether the input is checked. Use with
|
children
|
className
|
defaultCheckedWhether the input is checked initially, when uncontrolled.
|
descriptionAdd secondary text after the label.
|
disabledWhether the input is disabled.
|
indeterminateDisplay a partially checked icon instead of the regular checkmark.
|
nameThe name of the form control element.
|
onCheckedChangeHandler that is called when the checked state changes.
|
requiredWhether selecting this input is required.
|
sizeControl the size of the checkbox.
|
valueThe value of the form control element.
|
visibleControl whether to always show the contents or only when user is hovering or interacting with them.
|
#
DataTableLabel
#
import { DataTableLabel } from "@optiaxiom/react";
// or
import { DataTableLabel } from "@optiaxiom/react/data-table";Supports all Box props in addition to its own. Renders a <div> element.
Prop |
|---|
asChildChange the default rendered element for the one passed as a child, merging their props and behavior. Read the Composition guide for more details.
|
children
|
className
|
#
Accessibility
#
#
Labels
#
- Put
DataTableLabelin each row’s primary cell. It names the row and its selection checkbox for screen readers. - Pass
aria-labelledby(pointing at the table’s visible heading) oraria-labeltoDataTableBody. It names the region around the table, which takes keyboard focus when the table scrolls.
#
Keyboard navigation
#
- Mark one
DataTableActionper row asprimaryto enable arrow key navigation between rows and actions.
#
Changelog
#
#
3.7.0
#
- Added support for TanStack Table v9 tables from
useTable(), alongside v8. - Added
selectionBehaviorprop so a row click can run the primary action while rows stay selected. - Added
rowHighlightingFeatureto read and set the highlighted row on TanStack Table v9. - Added
rowIntentFeatureto give rows the selected color on TanStack Table v9.
#
1.5.0
#
- Added
DataTableActioncomponent - Added
DataTableCheckboxcomponent - Added
DataTableLabelcomponent
#
1.4.0
#
-
Moved component out of Alpha.
// Before import { DataTable } from "@optiaxiom/react/unstable"; // After import { DataTable } from "@optiaxiom/react";
#
0.3.0
#
- Added component