Data table
Also called data grid or sortable table
A table built for working with records: click a column header to sort, type to filter, and check rows to act on several at once.
import { useState } from 'react';
import { motion } from 'motion/react';
type Customer = { id: string; name: string; plan: string; seats: number };
type Column = 'name' | 'plan' | 'seats';
export function CustomersTable({ rows }: { rows: Customer[] }) {
const [sort, setSort] = useState({ by: 'name' as Column, dir: 1 });
const [query, setQuery] = useState('');
const visible = rows
.filter((row) => row.name.toLowerCase().includes(query.toLowerCase()))
.sort((a, b) => (a[sort.by] > b[sort.by] ? sort.dir : -sort.dir));
const order = sort.dir > 0 ? 'ascending' : 'descending';
const arrow = sort.dir > 0 ? '↑' : '↓';
// Clicking a header sorts by it; clicking it again flips the direction.
const sortBy = (by: Column) =>
setSort({ by, dir: sort.by === by ? -sort.dir : 1 });
const header = (by: Column) => (
<th aria-sort={sort.by === by ? order : 'none'}>
<button onClick={() => sortBy(by)}>
{by} {sort.by === by && arrow}
</button>
</th>
);
return (
<div className='w-md text-sm'>
<input
value={query}
onChange={(event) => setQuery(event.target.value)}
placeholder='Filter by name'
aria-label='Filter customers by name'
className='mb-3 h-9 w-full rounded-lg border bg-background px-3'
/>
<table className='w-full text-left'>
<thead className='border-b text-muted-foreground [&_th]:py-2 [&_th]:font-medium [&_th]:capitalize [&_th:last-child]:text-right'>
<tr>
{header('name')}
{header('plan')}
{header('seats')}
</tr>
</thead>
<tbody>
{visible.map((row) => (
// `layout` slides each row from its old position to its new one.
<motion.tr
key={row.id}
layout
className='border-b hover:bg-muted/50'
>
<td className='py-2 font-medium'>{row.name}</td>
<td>{row.plan}</td>
<td className='text-right tabular-nums'>{row.seats}</td>
</motion.tr>
))}
</tbody>
</table>
</div>
);
}Turn the customers list into a data table with columns Name, Plan, and Seats. Clicking a header sorts by that column and toggles ascending and descending with an arrow icon, and rows slide to their new positions over 250ms. Add a search box that filters by name as you type, a checkbox on each row plus select-all in the header, and a "2 selected" bar with a Delete button whenever rows are checked; set aria-sort on the sorted header.
Paste into Layerwise or any AI coding tool.
What is Data table?
A data table shows records in rows and their fields in columns, then adds tools on top: column headers that sort when clicked, with an arrow for the direction; a search or filter box; checkboxes to select rows for bulk actions such as Delete or Export; and often pagination for long sets.
Use it whenever people need to scan, compare, or manage many similar items, such as orders, users, invoices, or files. Cards can read better for a few items with images; once people compare fields side by side, use a table.
Right-align numbers and use tabular figures so digits line up, keep the header visible while scrolling, and highlight the row under the pointer. Build it with real <table> markup, use <th scope="col"> for headers, and set aria-sort on the sorted column so screen readers know the order.
Related terms
All termsPagination
Navigation that splits a long list into numbered pages with Previous and Next buttons, so people browse a fixed number of items at a time.
Checkbox
A small square you tick to turn an option on or select an item, with a dash in a parent box when only some items are checked.
Tabular numbers
A font setting that gives every digit the same width, so numbers line up in columns and timers or counters stop shifting as they change.
Layout animation
An animation that smoothly moves and resizes elements when the layout changes, such as list items gliding to new spots after a sort or filter.
Build it with Layerwise
Describe your app, watch it run in a live preview, and publish it with a database, storage, and sign-in built in.