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
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: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
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
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
app/payments/data-table.tsx