Skip to content
All components
Data display

Data table

Let people find, order and act on rows of loaded data with sorting, filtering, pagination, row selection and column visibility, built on native table semantics.

Sorting, filtering, pagination, row selection and column visibility on TanStack Table.

Installation

tdpxl is not an npm dependency of your app. The @tdpxl/cli command (not published to npm yet) copies the source into your project, with the registry items and npm packages it imports.

npx @tdpxl/cli add data-table

Then import it from @/components/ui/data-table. New project? Start with the installation guide.

Examples

Each example is one branch of the official demo, rendered in its own frame. The code is that branch as you would write it, with imports pointing at the default install paths.

Initial sort

Start sorted by a column with initialState. Without a selection column the footer reports a row count, and the sorted header exposes aria-sort. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import { Badge } from "@/components/ui/badge"

import {
  createDataTableColumnHelper,
  createDataTableSelectColumn,
  DataTable,
  DataTableColumnHeader,
} from "@/components/ui/data-table"

type Order = {
  id: string
  customer: string
  email: string
  status: "Paid" | "Pending" | "Refunded"
  amount: number
}

const orders: Order[] = [
  { id: "ORD-1001", customer: "Ada Lovelace", email: "ada@example.com", status: "Paid", amount: 320 },
  { id: "ORD-1002", customer: "Grace Hopper", email: "grace@example.com", status: "Pending", amount: 145.5 },
  { id: "ORD-1003", customer: "Alan Turing", email: "alan@example.com", status: "Paid", amount: 89 },
  { id: "ORD-1004", customer: "Katherine Johnson", email: "katherine@example.com", status: "Refunded", amount: 410 },
  { id: "ORD-1005", customer: "Linus Torvalds", email: "linus@example.com", status: "Paid", amount: 56.25 },
  { id: "ORD-1006", customer: "Margaret Hamilton", email: "margaret@example.com", status: "Pending", amount: 272 },
  { id: "ORD-1007", customer: "Tim Berners-Lee", email: "tim@example.com", status: "Paid", amount: 199.99 },
  { id: "ORD-1008", customer: "Barbara Liskov", email: "barbara@example.com", status: "Paid", amount: 130 },
  { id: "ORD-1009", customer: "Dennis Ritchie", email: "dennis@example.com", status: "Refunded", amount: 64 },
  { id: "ORD-1010", customer: "Radia Perlman", email: "radia@example.com", status: "Pending", amount: 380 },
  { id: "ORD-1011", customer: "Ken Thompson", email: "ken@example.com", status: "Paid", amount: 225 },
  { id: "ORD-1012", customer: "Frances Allen", email: "frances@example.com", status: "Paid", amount: 98.4 },
]

const statusColor = { Paid: "green", Pending: "orange", Refunded: "gray" } as const
const currency = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" })

const column = createDataTableColumnHelper<Order>()
const columns = column.columns([
  createDataTableSelectColumn<Order>({ getRowLabel: (order) => `order ${order.id}` }),
  column.accessor("id", {
    header: ({ column }) => <DataTableColumnHeader column={column} title="Order" />,
    cell: ({ getValue }) => <span className="font-mono">{getValue()}</span>,
    meta: { label: "Order" },
  }),
  column.accessor("customer", {
    header: ({ column }) => <DataTableColumnHeader column={column} title="Customer" />,
    meta: { label: "Customer" },
  }),
  column.accessor("email", {
    header: "Email",
    cell: ({ getValue }) => <span className="text-sub-600">{getValue()}</span>,
    enableSorting: false,
  }),
  column.accessor("status", {
    header: ({ column }) => <DataTableColumnHeader column={column} title="Status" />,
    cell: ({ getValue }) => (
      <Badge color={statusColor[getValue()]} emphasis="lighter">
        {getValue()}
      </Badge>
    ),
    meta: { label: "Status" },
  }),
  column.accessor("amount", {
    header: ({ column }) => (
      <div className="flex justify-end">
        <DataTableColumnHeader column={column} title="Amount" className="ml-0 -mr-2.5" />
      </div>
    ),
    cell: ({ getValue }) => <span className="block text-right tabular-nums">{currency.format(getValue())}</span>,
    meta: { label: "Amount", headerClassName: "text-right" },
  }),
])

// Without a selection column the footer shows a row count instead of a selection count.
const readOnlyColumns = column.columns([
  column.accessor("id", {
    header: ({ column }) => <DataTableColumnHeader column={column} title="Order" />,
    cell: ({ getValue }) => <span className="font-mono">{getValue()}</span>,
    meta: { label: "Order" },
  }),
  column.accessor("customer", {
    header: ({ column }) => <DataTableColumnHeader column={column} title="Customer" />,
    meta: { label: "Customer" },
  }),
  column.accessor("amount", {
    header: ({ column }) => (
      <div className="flex justify-end">
        <DataTableColumnHeader column={column} title="Amount" className="ml-0 -mr-2.5" />
      </div>
    ),
    cell: ({ getValue }) => <span className="block text-right tabular-nums">{currency.format(getValue())}</span>,
    meta: { label: "Amount", headerClassName: "text-right" },
  }),
])

export function DataTableInitialSort() {
  return (
    <div className="flex w-full max-w-4xl flex-col gap-12">
      <div className="w-full max-w-2xl">
        <DataTable
          caption="Largest orders"
          columns={readOnlyColumns}
          data={orders}
          getRowId={(order) => order.id}
          showFilter={false}
          showViewOptions={false}
          pageSizeOptions={[5, 10]}
          initialState={{
            sorting: [{ id: "amount", desc: true }],
            pagination: { pageIndex: 0, pageSize: 5 },
          }}
        />
      </div>
    </div>
  )
}

Filter one column

Filter a single column through filterColumn. The input label says which column it searches, and a status message reports the number of results. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import { Badge } from "@/components/ui/badge"

import {
  createDataTableColumnHelper,
  createDataTableSelectColumn,
  DataTable,
  DataTableColumnHeader,
} from "@/components/ui/data-table"

type Order = {
  id: string
  customer: string
  email: string
  status: "Paid" | "Pending" | "Refunded"
  amount: number
}

const orders: Order[] = [
  { id: "ORD-1001", customer: "Ada Lovelace", email: "ada@example.com", status: "Paid", amount: 320 },
  { id: "ORD-1002", customer: "Grace Hopper", email: "grace@example.com", status: "Pending", amount: 145.5 },
  { id: "ORD-1003", customer: "Alan Turing", email: "alan@example.com", status: "Paid", amount: 89 },
  { id: "ORD-1004", customer: "Katherine Johnson", email: "katherine@example.com", status: "Refunded", amount: 410 },
  { id: "ORD-1005", customer: "Linus Torvalds", email: "linus@example.com", status: "Paid", amount: 56.25 },
  { id: "ORD-1006", customer: "Margaret Hamilton", email: "margaret@example.com", status: "Pending", amount: 272 },
  { id: "ORD-1007", customer: "Tim Berners-Lee", email: "tim@example.com", status: "Paid", amount: 199.99 },
  { id: "ORD-1008", customer: "Barbara Liskov", email: "barbara@example.com", status: "Paid", amount: 130 },
  { id: "ORD-1009", customer: "Dennis Ritchie", email: "dennis@example.com", status: "Refunded", amount: 64 },
  { id: "ORD-1010", customer: "Radia Perlman", email: "radia@example.com", status: "Pending", amount: 380 },
  { id: "ORD-1011", customer: "Ken Thompson", email: "ken@example.com", status: "Paid", amount: 225 },
  { id: "ORD-1012", customer: "Frances Allen", email: "frances@example.com", status: "Paid", amount: 98.4 },
]

const statusColor = { Paid: "green", Pending: "orange", Refunded: "gray" } as const
const currency = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" })

const column = createDataTableColumnHelper<Order>()
const columns = column.columns([
  createDataTableSelectColumn<Order>({ getRowLabel: (order) => `order ${order.id}` }),
  column.accessor("id", {
    header: ({ column }) => <DataTableColumnHeader column={column} title="Order" />,
    cell: ({ getValue }) => <span className="font-mono">{getValue()}</span>,
    meta: { label: "Order" },
  }),
  column.accessor("customer", {
    header: ({ column }) => <DataTableColumnHeader column={column} title="Customer" />,
    meta: { label: "Customer" },
  }),
  column.accessor("email", {
    header: "Email",
    cell: ({ getValue }) => <span className="text-sub-600">{getValue()}</span>,
    enableSorting: false,
  }),
  column.accessor("status", {
    header: ({ column }) => <DataTableColumnHeader column={column} title="Status" />,
    cell: ({ getValue }) => (
      <Badge color={statusColor[getValue()]} emphasis="lighter">
        {getValue()}
      </Badge>
    ),
    meta: { label: "Status" },
  }),
  column.accessor("amount", {
    header: ({ column }) => (
      <div className="flex justify-end">
        <DataTableColumnHeader column={column} title="Amount" className="ml-0 -mr-2.5" />
      </div>
    ),
    cell: ({ getValue }) => <span className="block text-right tabular-nums">{currency.format(getValue())}</span>,
    meta: { label: "Amount", headerClassName: "text-right" },
  }),
])

// Without a selection column the footer shows a row count instead of a selection count.
const readOnlyColumns = column.columns([
  column.accessor("id", {
    header: ({ column }) => <DataTableColumnHeader column={column} title="Order" />,
    cell: ({ getValue }) => <span className="font-mono">{getValue()}</span>,
    meta: { label: "Order" },
  }),
  column.accessor("customer", {
    header: ({ column }) => <DataTableColumnHeader column={column} title="Customer" />,
    meta: { label: "Customer" },
  }),
  column.accessor("amount", {
    header: ({ column }) => (
      <div className="flex justify-end">
        <DataTableColumnHeader column={column} title="Amount" className="ml-0 -mr-2.5" />
      </div>
    ),
    cell: ({ getValue }) => <span className="block text-right tabular-nums">{currency.format(getValue())}</span>,
    meta: { label: "Amount", headerClassName: "text-right" },
  }),
])

export function DataTableFilterOneColumn() {
  return (
    <div className="flex w-full max-w-4xl flex-col gap-12">
      <div className="w-full max-w-2xl">
        <DataTable
          caption="Orders by customer"
          columns={readOnlyColumns}
          data={orders}
          getRowId={(order) => order.id}
          filterColumn="customer"
          filterLabel="Filter by customer name"
          filterPlaceholder="Customer name…"
          showViewOptions={false}
          pageSizeOptions={[5, 10]}
          initialState={{ pagination: { pageIndex: 0, pageSize: 5 } }}
        />
      </div>
    </div>
  )
}

Bulk selection

Control row selection and drive a bulk action from it. The toolbar action states how many rows it affects and stays disabled until something is selected. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import { DownloadIcon } from "lucide-react"
import * as React from "react"

import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import {
  createDataTableColumnHelper,
  createDataTableSelectColumn,
  DataTable,
  DataTableColumnHeader,
} from "@/components/ui/data-table"

type Order = {
  id: string
  customer: string
  email: string
  status: "Paid" | "Pending" | "Refunded"
  amount: number
}

const orders: Order[] = [
  { id: "ORD-1001", customer: "Ada Lovelace", email: "ada@example.com", status: "Paid", amount: 320 },
  { id: "ORD-1002", customer: "Grace Hopper", email: "grace@example.com", status: "Pending", amount: 145.5 },
  { id: "ORD-1003", customer: "Alan Turing", email: "alan@example.com", status: "Paid", amount: 89 },
  { id: "ORD-1004", customer: "Katherine Johnson", email: "katherine@example.com", status: "Refunded", amount: 410 },
  { id: "ORD-1005", customer: "Linus Torvalds", email: "linus@example.com", status: "Paid", amount: 56.25 },
  { id: "ORD-1006", customer: "Margaret Hamilton", email: "margaret@example.com", status: "Pending", amount: 272 },
  { id: "ORD-1007", customer: "Tim Berners-Lee", email: "tim@example.com", status: "Paid", amount: 199.99 },
  { id: "ORD-1008", customer: "Barbara Liskov", email: "barbara@example.com", status: "Paid", amount: 130 },
  { id: "ORD-1009", customer: "Dennis Ritchie", email: "dennis@example.com", status: "Refunded", amount: 64 },
  { id: "ORD-1010", customer: "Radia Perlman", email: "radia@example.com", status: "Pending", amount: 380 },
  { id: "ORD-1011", customer: "Ken Thompson", email: "ken@example.com", status: "Paid", amount: 225 },
  { id: "ORD-1012", customer: "Frances Allen", email: "frances@example.com", status: "Paid", amount: 98.4 },
]

const statusColor = { Paid: "green", Pending: "orange", Refunded: "gray" } as const
const currency = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" })

const column = createDataTableColumnHelper<Order>()
const columns = column.columns([
  createDataTableSelectColumn<Order>({ getRowLabel: (order) => `order ${order.id}` }),
  column.accessor("id", {
    header: ({ column }) => <DataTableColumnHeader column={column} title="Order" />,
    cell: ({ getValue }) => <span className="font-mono">{getValue()}</span>,
    meta: { label: "Order" },
  }),
  column.accessor("customer", {
    header: ({ column }) => <DataTableColumnHeader column={column} title="Customer" />,
    meta: { label: "Customer" },
  }),
  column.accessor("email", {
    header: "Email",
    cell: ({ getValue }) => <span className="text-sub-600">{getValue()}</span>,
    enableSorting: false,
  }),
  column.accessor("status", {
    header: ({ column }) => <DataTableColumnHeader column={column} title="Status" />,
    cell: ({ getValue }) => (
      <Badge color={statusColor[getValue()]} emphasis="lighter">
        {getValue()}
      </Badge>
    ),
    meta: { label: "Status" },
  }),
  column.accessor("amount", {
    header: ({ column }) => (
      <div className="flex justify-end">
        <DataTableColumnHeader column={column} title="Amount" className="ml-0 -mr-2.5" />
      </div>
    ),
    cell: ({ getValue }) => <span className="block text-right tabular-nums">{currency.format(getValue())}</span>,
    meta: { label: "Amount", headerClassName: "text-right" },
  }),
])

function SelectionWithBulkAction() {
  const [rowSelection, setRowSelection] = React.useState<Record<string, true>>({})
  const selected = Object.keys(rowSelection).length

  return (
    <DataTable
      caption="Orders to export"
      columns={columns}
      data={orders}
      getRowId={(order) => order.id}
      rowSelection={rowSelection}
      onRowSelectionChange={setRowSelection}
      filterLabel="Filter orders to export"
      filterPlaceholder="Filter orders…"
      pageSizeOptions={[5, 10, 20]}
      initialState={{ pagination: { pageIndex: 0, pageSize: 5 } }}
      toolbar={
        <Button type="button" size="sm" intent="neutral" emphasis="stroke" disabled={selected === 0}>
          <DownloadIcon aria-hidden="true" />
          {selected === 0 ? "Export selected" : `Export ${selected} selected`}
        </Button>
      }
    />
  )
}

export function DataTableBulkSelection() {
  return (
    <div className="flex w-full max-w-4xl flex-col gap-12">
      <div className="w-full max-w-4xl">
        <SelectionWithBulkAction />
      </div>
    </div>
  )
}

Load error

Explain a failed load with a retry action inside the table. emptyState replaces the default message, so the explanation stays in the table body under the column headers. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import { CircleAlertIcon } from "lucide-react"

import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import {
  createDataTableColumnHelper,
  createDataTableSelectColumn,
  DataTable,
  DataTableColumnHeader,
} from "@/components/ui/data-table"
import {
  Empty,
  EmptyContent,
  EmptyDescription,
  EmptyMedia,
  EmptyTitle,
} from "@/components/ui/empty"

type Order = {
  id: string
  customer: string
  email: string
  status: "Paid" | "Pending" | "Refunded"
  amount: number
}

const orders: Order[] = [
  { id: "ORD-1001", customer: "Ada Lovelace", email: "ada@example.com", status: "Paid", amount: 320 },
  { id: "ORD-1002", customer: "Grace Hopper", email: "grace@example.com", status: "Pending", amount: 145.5 },
  { id: "ORD-1003", customer: "Alan Turing", email: "alan@example.com", status: "Paid", amount: 89 },
  { id: "ORD-1004", customer: "Katherine Johnson", email: "katherine@example.com", status: "Refunded", amount: 410 },
  { id: "ORD-1005", customer: "Linus Torvalds", email: "linus@example.com", status: "Paid", amount: 56.25 },
  { id: "ORD-1006", customer: "Margaret Hamilton", email: "margaret@example.com", status: "Pending", amount: 272 },
  { id: "ORD-1007", customer: "Tim Berners-Lee", email: "tim@example.com", status: "Paid", amount: 199.99 },
  { id: "ORD-1008", customer: "Barbara Liskov", email: "barbara@example.com", status: "Paid", amount: 130 },
  { id: "ORD-1009", customer: "Dennis Ritchie", email: "dennis@example.com", status: "Refunded", amount: 64 },
  { id: "ORD-1010", customer: "Radia Perlman", email: "radia@example.com", status: "Pending", amount: 380 },
  { id: "ORD-1011", customer: "Ken Thompson", email: "ken@example.com", status: "Paid", amount: 225 },
  { id: "ORD-1012", customer: "Frances Allen", email: "frances@example.com", status: "Paid", amount: 98.4 },
]

const statusColor = { Paid: "green", Pending: "orange", Refunded: "gray" } as const
const currency = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" })

const column = createDataTableColumnHelper<Order>()
const columns = column.columns([
  createDataTableSelectColumn<Order>({ getRowLabel: (order) => `order ${order.id}` }),
  column.accessor("id", {
    header: ({ column }) => <DataTableColumnHeader column={column} title="Order" />,
    cell: ({ getValue }) => <span className="font-mono">{getValue()}</span>,
    meta: { label: "Order" },
  }),
  column.accessor("customer", {
    header: ({ column }) => <DataTableColumnHeader column={column} title="Customer" />,
    meta: { label: "Customer" },
  }),
  column.accessor("email", {
    header: "Email",
    cell: ({ getValue }) => <span className="text-sub-600">{getValue()}</span>,
    enableSorting: false,
  }),
  column.accessor("status", {
    header: ({ column }) => <DataTableColumnHeader column={column} title="Status" />,
    cell: ({ getValue }) => (
      <Badge color={statusColor[getValue()]} emphasis="lighter">
        {getValue()}
      </Badge>
    ),
    meta: { label: "Status" },
  }),
  column.accessor("amount", {
    header: ({ column }) => (
      <div className="flex justify-end">
        <DataTableColumnHeader column={column} title="Amount" className="ml-0 -mr-2.5" />
      </div>
    ),
    cell: ({ getValue }) => <span className="block text-right tabular-nums">{currency.format(getValue())}</span>,
    meta: { label: "Amount", headerClassName: "text-right" },
  }),
])

// Without a selection column the footer shows a row count instead of a selection count.
const readOnlyColumns = column.columns([
  column.accessor("id", {
    header: ({ column }) => <DataTableColumnHeader column={column} title="Order" />,
    cell: ({ getValue }) => <span className="font-mono">{getValue()}</span>,
    meta: { label: "Order" },
  }),
  column.accessor("customer", {
    header: ({ column }) => <DataTableColumnHeader column={column} title="Customer" />,
    meta: { label: "Customer" },
  }),
  column.accessor("amount", {
    header: ({ column }) => (
      <div className="flex justify-end">
        <DataTableColumnHeader column={column} title="Amount" className="ml-0 -mr-2.5" />
      </div>
    ),
    cell: ({ getValue }) => <span className="block text-right tabular-nums">{currency.format(getValue())}</span>,
    meta: { label: "Amount", headerClassName: "text-right" },
  }),
])

const noOrders: Order[] = []

export function DataTableLoadError() {
  return (
    <div className="flex w-full max-w-4xl flex-col gap-12">
      <div className="w-full max-w-2xl">
        <DataTable
          caption="Archived orders"
          columns={readOnlyColumns}
          data={noOrders}
          showFilter={false}
          showViewOptions={false}
          emptyState={
            <Empty className="border-0 py-10">
              <EmptyMedia>
                <CircleAlertIcon aria-hidden="true" />
              </EmptyMedia>
              <EmptyTitle>Archived orders could not be loaded</EmptyTitle>
              <EmptyDescription>
                The connection timed out. Your orders are safe; try again in a moment.
              </EmptyDescription>
              <EmptyContent>
                <Button type="button" size="sm" intent="neutral" emphasis="stroke">
                  Try again
                </Button>
              </EmptyContent>
            </Empty>
          }
        />
      </div>
    </div>
  )
}

Narrow viewport

Keep a wide table usable in a narrow container. A less important column starts hidden, the toolbar wraps and the table scrolls sideways inside its own region. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import { Badge } from "@/components/ui/badge"

import {
  createDataTableColumnHelper,
  createDataTableSelectColumn,
  DataTable,
  DataTableColumnHeader,
} from "@/components/ui/data-table"

type Order = {
  id: string
  customer: string
  email: string
  status: "Paid" | "Pending" | "Refunded"
  amount: number
}

const orders: Order[] = [
  { id: "ORD-1001", customer: "Ada Lovelace", email: "ada@example.com", status: "Paid", amount: 320 },
  { id: "ORD-1002", customer: "Grace Hopper", email: "grace@example.com", status: "Pending", amount: 145.5 },
  { id: "ORD-1003", customer: "Alan Turing", email: "alan@example.com", status: "Paid", amount: 89 },
  { id: "ORD-1004", customer: "Katherine Johnson", email: "katherine@example.com", status: "Refunded", amount: 410 },
  { id: "ORD-1005", customer: "Linus Torvalds", email: "linus@example.com", status: "Paid", amount: 56.25 },
  { id: "ORD-1006", customer: "Margaret Hamilton", email: "margaret@example.com", status: "Pending", amount: 272 },
  { id: "ORD-1007", customer: "Tim Berners-Lee", email: "tim@example.com", status: "Paid", amount: 199.99 },
  { id: "ORD-1008", customer: "Barbara Liskov", email: "barbara@example.com", status: "Paid", amount: 130 },
  { id: "ORD-1009", customer: "Dennis Ritchie", email: "dennis@example.com", status: "Refunded", amount: 64 },
  { id: "ORD-1010", customer: "Radia Perlman", email: "radia@example.com", status: "Pending", amount: 380 },
  { id: "ORD-1011", customer: "Ken Thompson", email: "ken@example.com", status: "Paid", amount: 225 },
  { id: "ORD-1012", customer: "Frances Allen", email: "frances@example.com", status: "Paid", amount: 98.4 },
]

const statusColor = { Paid: "green", Pending: "orange", Refunded: "gray" } as const
const currency = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" })

const column = createDataTableColumnHelper<Order>()
const columns = column.columns([
  createDataTableSelectColumn<Order>({ getRowLabel: (order) => `order ${order.id}` }),
  column.accessor("id", {
    header: ({ column }) => <DataTableColumnHeader column={column} title="Order" />,
    cell: ({ getValue }) => <span className="font-mono">{getValue()}</span>,
    meta: { label: "Order" },
  }),
  column.accessor("customer", {
    header: ({ column }) => <DataTableColumnHeader column={column} title="Customer" />,
    meta: { label: "Customer" },
  }),
  column.accessor("email", {
    header: "Email",
    cell: ({ getValue }) => <span className="text-sub-600">{getValue()}</span>,
    enableSorting: false,
  }),
  column.accessor("status", {
    header: ({ column }) => <DataTableColumnHeader column={column} title="Status" />,
    cell: ({ getValue }) => (
      <Badge color={statusColor[getValue()]} emphasis="lighter">
        {getValue()}
      </Badge>
    ),
    meta: { label: "Status" },
  }),
  column.accessor("amount", {
    header: ({ column }) => (
      <div className="flex justify-end">
        <DataTableColumnHeader column={column} title="Amount" className="ml-0 -mr-2.5" />
      </div>
    ),
    cell: ({ getValue }) => <span className="block text-right tabular-nums">{currency.format(getValue())}</span>,
    meta: { label: "Amount", headerClassName: "text-right" },
  }),
])

export function DataTableNarrowViewport() {
  return (
    <div className="flex w-full max-w-4xl flex-col gap-12">
      <div className="w-full max-w-xs">
        <DataTable
          caption="Orders on a narrow screen"
          columns={columns}
          data={orders}
          getRowId={(order) => order.id}
          filterLabel="Filter orders on a narrow screen"
          filterPlaceholder="Filter orders…"
          pageSizeOptions={[5, 10]}
          initialState={{
            pagination: { pageIndex: 0, pageSize: 5 },
            columnVisibility: { email: false },
          }}
        />
      </div>
    </div>
  )
}

API reference

Generated from the component's TypeScript signatures. Inherited DOM and primitive props are listed separately.

DataTable

Sort, filter, paginate and select rows of client-side data, built on TanStack Table v9 and the TDPXL Table, Input, Checkbox, DropdownMenu, Select, Pagination, Empty and Skeleton parts. Columns come from createDataTableColumnHelper; sortable headers use DataTableColumnHeader.

DataTable props
PropTypeDefaultDescription
initialStatePartial<import("@tanstack/table-core").TableState_ColumnFiltering & import("@tanstack/table-core").TableState_ColumnVisibility & import("@tanstack/table-core").TableState_GlobalFiltering & import("@tanstack/table-core").TableState_RowPagination & import("@tanstack/table-core").TableState_RowSelection & import("@tanstack/table-core").TableState_RowSorting> | undefined—Initial uncontrolled state, such as a default sort, page size or hidden columns. Read once on mount.
sortingSortingState | undefined—Controlled sort order. Pair with onSortingChange.
globalFilterstring | undefined—Controlled text of the toolbar filter when filtering across all columns.
columnFiltersColumnFiltersState | undefined—Controlled per-column filters.
paginationPaginationState | undefined—Controlled page index and size.
rowSelectionRowSelectionState | undefined—Controlled selection keyed by row id. Supply getRowId so ids survive sorting and paging.
columnVisibilityColumnVisibilityState | undefined—Controlled column visibility; only an explicit false hides a column.
columns(required)readonly DataTableColumnDef<TData, TValue>[]—Column definitions. Keep them stable (module scope or useMemo) so row models are not rebuilt on every render.
data(required)TData[]—Rows to display. Keep the array identity stable between renders.
getRowId((row: TData, index: number) => string) | undefined—Returns a stable id for each row. Required for selection that survives sorting, paging and data refreshes.
enableRowSelectionboolean | ((row: DataTableRow<TData>) => boolean) | undefined—Allow selection for every row (true) or only rows the callback approves. Defaults to true.
caption(required)React.ReactNode—Names the table for assistive technology. Rendered as a native caption below the table unless hideCaption is set.
hideCaptionboolean | undefinedfalseKeep the caption available to assistive technology but visually hidden, when a nearby heading already names the table.
filterColumnstring | undefined—Column id the toolbar input filters. When omitted, the input searches every text and number column.
filterLabelstring | undefined"Filter rows"Accessible name for the toolbar filter input.
filterPlaceholderstring | undefined"Filter…"Hint text inside the filter input. It is not its accessible name.
showFilterboolean | undefinedtrueShow the toolbar filter input. Defaults to true.
showViewOptionsboolean | undefinedtrueShow the column visibility menu. Defaults to true.
toolbarReact.ReactNode—Extra toolbar content, such as bulk actions, rendered before the column menu.
pageSizeOptionsnumber[] | undefined—Choices for rows per page.
loadingboolean | undefinedfalseShow skeleton rows and mark the table busy while data loads.
loadingRowsnumber | undefined—Number of skeleton rows while loading. Defaults to the page size, up to five.
emptyStateReact.ReactNode—Replaces the default empty state shown when no rows match.
classNamestring | undefined—Additional classes for the outer wrapper.

DataTableColumnHeader

A header label that sorts its column. Renders plain text when the column cannot sort. DataTable places aria-sort on the surrounding header cell, so the button itself only needs the visible title as its name.

DataTableColumnHeader props
PropTypeDefaultDescription
column(required)DataTableColumn<TData, TValue>—The column this header belongs to, from the header renderer context.
title(required)string—Visible column name. It is also the sort button's accessible name.
classNamestring | undefined—Additional classes for this part. Preserve focus visibility, contrast and content reflow when adapting styles.

DataTablePagination

Footer controls for a DataTable: selection count, rows per page and page navigation. Page changes are announced politely; buttons at a boundary use native disabled so they leave the Tab order.

DataTablePagination props
PropTypeDefaultDescription
table(required)DataTableInstance<TData>—The table instance from DataTable or useDataTable.
pageSizeOptionsnumber[] | undefined[10, 20, 50]Choices for the rows-per-page menu. Include the current page size.
showSelectionboolean | undefinedtrueShow the selected-row count. Hide it when the table has no selection column.
labelstring | undefined"Table pagination"Accessible name of the page navigation landmark. Make it unique when a page shows several tables. Defaults to "Table pagination".
classNamestring | undefined—Additional classes for this part. Preserve focus visibility, contrast and content reflow when adapting styles.

DataTableViewOptions

A menu that shows or hides columns. Only columns that can hide are listed; the menu stays open between toggles so several columns can change at once.

DataTableViewOptions props
PropTypeDefaultDescription
table(required)DataTableInstance<TData>—The table instance from DataTable or useDataTable.
labelstring | undefined"Columns"Visible trigger text and menu heading context.
classNamestring | undefined—Additional classes for the trigger. Preserve focus visibility, contrast and content reflow when adapting styles.

Accessibility & usage

From registry/ui/data-table.meta.json (authored guidance; human review may still be pending). Behavior is checked by Storybook tests, not certified here.

When to use

  • A collection of records with shared fields that people need to sort, search or page through: orders, users, invoices, tickets.
  • Selecting several rows to run a bulk action (export, archive, assign), with a stable getRowId so the selection survives sorting and paging.
  • Data that is already in the browser; the built-in row models filter, sort and paginate only the rows passed in data.
  • A custom layout of the same features: useDataTable creates the instance and DataTablePagination, DataTableViewOptions and DataTableColumnHeader compose around your own markup.

When not to use

  • A short static list or a comparison matrix that nobody sorts or filters; a plain table is simpler and lighter.
  • Large server-side datasets where sorting, filtering and paging must happen on the server; this component does not fetch or switch TanStack to manual modes for you.
  • Spreadsheet-style editing with cell-by-cell arrow-key navigation; that is an ARIA grid, not this table.
  • Rich objects with images and long text per entry, especially on mobile; cards or an item list read better.

Anatomy

DataTable
The default arrangement: toolbar (filter input, custom toolbar content, column menu), the Table with caption, and the pagination footer. Props cover caption, filter, loading, empty state and controlled state slices.
Toolbar filter
A search input named by filterLabel. Filters across text and number columns, or one column when filterColumn is set; a hidden status reports the result count.
DataTableColumnHeader
A header label that is a sort button when the column can sort and plain text otherwise; DataTable sets aria-sort on the surrounding header cell.
createDataTableSelectColumn
A leading checkbox column: a select-all checkbox for the current page and one named checkbox per row (getRowLabel).
DataTableViewOptions
A column menu that shows or hides columns that can hide; it stays open between toggles.
DataTablePagination
Footer with the selected or total row count (status), rows per page (Select), page position (polite live text) and first/previous/next/last buttons in a named navigation landmark.
Loading and empty rows
loading renders skeleton rows hidden from assistive technology, marks the table busy and adds a status message; with no rows an Empty message (or emptyState) spans every visible column.
useDataTable and createDataTableColumnHelper
The table instance and a typed column helper for building custom layouts with the same feature set.

Keyboard & interaction

  • Sort buttons are native buttons in the header: Enter or Space cycles the sort; the header cell exposes aria-sort ascending or descending, and the arrow icon is decorative.
  • Typing in the filter updates rows immediately and announces the number of results through a status message; the empty state says whether a filter caused it.
  • Checkboxes toggle with Space. The header checkbox selects the current page and shows a mixed state when only some rows are selected; the footer reports 'N of M rows selected'.
  • Pagination buttons at a boundary are natively disabled and leave the Tab order; the page position updates politely (aria-live) after each change.
  • The column menu is a dropdown of checkbox items operated with arrow keys, Space or Enter, and Escape to close; focus returns to its trigger.
  • Every state slice (sorting, globalFilter, columnFilters, pagination, rowSelection, columnVisibility) can be controlled with its on…Change callback; initialState sets defaults once on mount.

Responsive behavior

  • The table scrolls horizontally inside its focusable region; the toolbar and pagination wrap onto several lines so the page reflows at 320 CSS pixels (WCAG 1.4.10).
  • On narrow screens, hide low-priority columns by default with initialState.columnVisibility and let people restore them from the column menu.
  • The filter input takes the full width on small screens and a fixed maximum width from the sm breakpoint.
  • Keep the identifying column first and short so each row is recognizable while scrolling sideways.
  • Pagination buttons are 36x36 CSS pixels; keep at least the 24x24 minimum (WCAG 2.5.8) if you compact them.

Content guidelines

  • Caption: name the data and its scope ('Recent orders'). Use hideCaption only when a visible heading right above says the same thing.
  • filterLabel names what is searched ('Filter by customer name'); the placeholder is a hint, never the label.
  • Column titles are short nouns; put units in the header and align numbers to the end with tabular figures.
  • Row labels for selection identify the record ('order ORD-1001'), not its position.
  • Empty states differ: 'No rows yet' when there is no data, 'No matching rows' with a way to clear the filter when filtering emptied the table, and an explanation plus retry when loading failed.
  • Bulk actions name the count and the outcome ('Export 3 selected').

Alternatives

  • Table — Static tabular data without sorting, filtering, paging or selection.
  • Item — A scannable list of objects where columns would not be compared.
  • Card — A handful of heterogeneous objects, each with its own actions.
  • Pagination — Page navigation for content that is not a table, such as search results.

Resources