Data Table
Powerful table and datagrids built using TanStack Table.
Status | Amount | |||
---|---|---|---|---|
success | ken99@yahoo.com | $316.00 | ||
success | Abe45@gmail.com | $242.00 | ||
processing | Monserrat44@gmail.com | $837.00 | ||
success | Silas22@gmail.com | $874.00 | ||
failed | carmella@hotmail.com | $721.00 |
Introduction
Every data table or datagrid I've created has been unique. They all behave differently, have specific sorting and filtering requirements, and work with different data sources.
It doesn't make sense to combine all of these variations into a single component. If we do that, we'll lose the flexibility that headless UI provides.
So instead of a data-table component, I thought it would be more helpful to provide a guide on how to build your own.
We'll start with the basic <Table />
component and build a complex data table from scratch.
If you find yourself using the same table in multiple places in your app, you can always extract it into a reusable component.
Table of contents
This guide will show you how to use TanStack Table and the <Table />
component to build your own custom data table.
We'll cover the following topics:
Installation
- Add the
<Table />
component to your project. - Install the
@tanstack/react-table
package.
Prerequisites
We are going to build a table to show recent payments. Here's what our data looks like:
Project structure
Start by creating the following file structure:
I'm using a Next.js (app router) example here but this works for any other React framework.
columns.tsx
(client component) will contain our column definitions.data-table.tsx
(client component) will contain our<DataTable />
component.page.tsx
(server component) is where we'll fetch data and render our table.
Basic Table
Let's start by building a basic table.
Column definitions
First, we'll define our columns.
Columns are where you define the core of what your table will look like. They define the data that will be displayed, how it will be formatted, sorted and filtered.
<DataTable />
component
Next, we'll create a <DataTable />
component to render our table.
If you find yourself using <DataTable />
in multiple places, this is the component you could make reusable by extracting it to components/ui/data-table.tsx
.
Render table
Finally, we'll render our table in our page component.
Cell formatting
Let's format the amount cell to display the dollar amount. We'll also align the cell to the right.
Update column definitions
Update the header
and cell
definitions for amount as follows:
You can use the same approach to format other cells and headers.
Row actions
Let's add row actions to our table. We'll use a <Dropdown />
component for this.
Update column definitions
Update our columns definition to add a new actions
column. The actions
cell returns a <Dropdown />
component.
You can access the row data using row.original
in the cell
function.
Use this to handle actions for your row eg. use the id
to make a DELETE
call to your API.
Pagination
Next, we'll add pagination to our table.
Update <DataTable />
This will automatically paginate your rows into pages of 10. See the pagination docs for more information on customizing page size and implementing manual pagination.
Add pagination controls
We can add pagination controls to our table using the <Button />
component and the table.previousPage()
, table.nextPage()
API methods.
See Reusable Components section for a more advanced pagination component.
Sorting
Let's make the email column sortable.
Make header cell sortable
We can now update the email
header cell to add sorting controls.
This will automatically sort the table (asc and desc) when the user toggles on the header cell.
Filtering
Let's add a search input to filter emails in our table.
Update <DataTable />
Filtering is now enabled for the email
column.
You can add filters to other columns as well.
See the filtering docs for more information on customizing filters.
Visibility
Adding column visibility is fairly simple using @tanstack/react-table
visibility API.
Update <DataTable />
This adds a dropdown menu that you can use to toggle column visibility.
Row selection
Next, we're going to add row selection to our table.
Update <DataTable />
This adds a checkbox to each row and a checkbox in the header to select all rows.
Show selected rows
You can show the number of selected rows using the table.getFilteredSelectedRowModel()
API.
Reusable components
Here are some components you can use to build your data tables.
Column header
Make any column header sortable and hideable.
Pagination
Add pagination controls to your table including page size and selection count.
Column toggle
A component to toggle column visibility.