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