Skip to main content

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, this guide shows you 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.

Installation

Add the Table component:

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 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

Column Definitions

First, define your columns:
app/payments/columns.tsx
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

Create a <DataTable /> component to render your table:
app/payments/data-table.tsx
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 the Table

Finally, render your table in your page component:
app/payments/page.tsx

Cell Formatting

Format the amount cell to display the dollar amount and align it to the right:
app/payments/columns.tsx

Row Actions

Add row actions using a dropdown menu:
app/payments/columns.tsx

Pagination

Add pagination to your table:
app/payments/data-table.tsx

Sorting

Add sorting to your table:
app/payments/data-table.tsx
Make the email column sortable:
app/payments/columns.tsx

Filtering

Add a search input to filter emails:
app/payments/data-table.tsx

Visibility

Add column visibility controls:
app/payments/data-table.tsx

Row Selection

Add row selection with checkboxes:
app/payments/columns.tsx
Update the data table:
app/payments/data-table.tsx
Show selected rows count:

API Reference

See the TanStack Table documentation for complete API details.