Skip to content
2k
On This Page

Table

A responsive table component.

A list of your recent invoices.
InvoiceStatusMethodAmount
INV001PaidCredit Card$250.00
INV002PendingPayPal$150.00
INV003UnpaidBank Transfer$350.00
INV004PaidCredit Card$450.00
INV005PaidPayPal$550.00
INV006PendingBank Transfer$200.00
INV007UnpaidCredit Card$300.00
Total$2,500.00
import { For } from "solid-js"

import {
  Table,
  TableBody,
  TableCaption,
  TableCell,
  TableFooter,
  TableHead,
  TableHeader,
  TableRow
} from "~/components/ui/table"

const invoices = [
  {
    invoice: "INV001",
    paymentStatus: "Paid",
    totalAmount: "$250.00",
    paymentMethod: "Credit Card"
  },
  {
    invoice: "INV002",
    paymentStatus: "Pending",
    totalAmount: "$150.00",
    paymentMethod: "PayPal"
  },
  {
    invoice: "INV003",
    paymentStatus: "Unpaid",
    totalAmount: "$350.00",
    paymentMethod: "Bank Transfer"
  },
  {
    invoice: "INV004",
    paymentStatus: "Paid",
    totalAmount: "$450.00",
    paymentMethod: "Credit Card"
  },
  {
    invoice: "INV005",
    paymentStatus: "Paid",
    totalAmount: "$550.00",
    paymentMethod: "PayPal"
  },
  {
    invoice: "INV006",
    paymentStatus: "Pending",
    totalAmount: "$200.00",
    paymentMethod: "Bank Transfer"
  },
  {
    invoice: "INV007",
    paymentStatus: "Unpaid",
    totalAmount: "$300.00",
    paymentMethod: "Credit Card"
  }
]

export default function TableDemo() {
  return (
    <Table>
      <TableCaption>A list of your recent invoices.</TableCaption>
      <TableHeader>
        <TableRow>
          <TableHead class="w-[100px]">Invoice</TableHead>
          <TableHead>Status</TableHead>
          <TableHead>Method</TableHead>
          <TableHead class="text-right">Amount</TableHead>
        </TableRow>
      </TableHeader>
      <TableBody>
        <For each={invoices}>
          {(invoice) => (
            <TableRow>
              <TableCell class="font-medium">{invoice.invoice}</TableCell>
              <TableCell>{invoice.paymentStatus}</TableCell>
              <TableCell>{invoice.paymentMethod}</TableCell>
              <TableCell class="text-right">{invoice.totalAmount}</TableCell>
            </TableRow>
          )}
        </For>
      </TableBody>
      <TableFooter>
        <TableRow>
          <TableCell colspan={3}>Total</TableCell>
          <TableCell class="text-right">$2,500.00</TableCell>
        </TableRow>
      </TableFooter>
    </Table>
  )
}

Installation

pnpm dlx solidui-cli@latest add table

Copy and paste the following code into your project.

components/ui/table.tsx
import { type ComponentProps } from "@solidjs/web"

import { cn } from "~/lib/utils"

type TableProps = ComponentProps<"table">

const Table = (props: TableProps) => (
  <div class="relative w-full overflow-x-auto" data-slot="table-container">
    <table data-slot="table" {...props} class={cn("w-full caption-bottom text-sm", props.class)} />
  </div>
)

const TableHeader = (props: ComponentProps<"thead">) => (
  <thead
    data-slot="table-header"
    {...props}
    class={cn("[&_tr]:border-b", props.class)}
  />
)

const TableBody = (props: ComponentProps<"tbody">) => (
  <tbody
    data-slot="table-body"
    {...props}
    class={cn("[&_tr:last-child]:border-0", props.class)}
  />
)

const TableFooter = (props: ComponentProps<"tfoot">) => (
  <tfoot
    data-slot="table-footer"
    {...props}
    class={cn("border-t bg-muted/50 font-medium [&>tr]:last:border-b-0", props.class)}
  />
)

const TableRow = (props: ComponentProps<"tr">) => (
  <tr
    data-slot="table-row"
    {...props}
    class={cn("border-b transition-colors hover:bg-muted/50 data-[state=selected]:bg-muted", props.class)}
  />
)

const TableHead = (props: ComponentProps<"th">) => (
  <th
    data-slot="table-head"
    {...props}
    class={cn("h-10 px-2 text-left align-middle font-medium whitespace-nowrap text-foreground [&:has([role=checkbox])]:pr-0", props.class)}
  />
)

const TableCell = (props: ComponentProps<"td">) => (
  <td
    data-slot="table-cell"
    {...props}
    class={cn("p-2 align-middle whitespace-nowrap [&:has([role=checkbox])]:pr-0", props.class)}
  />
)

const TableCaption = (props: ComponentProps<"caption">) => (
  <caption
    data-slot="table-caption"
    {...props}
    class={cn("mt-4 text-sm text-muted-foreground", props.class)}
  />
)

export {
  Table,
  TableBody,
  TableCaption,
  TableCell,
  TableFooter,
  TableHead,
  TableHeader,
  TableRow,
  type TableProps
}

Update the import paths to match your project setup.

Usage

import {
  Table,
  TableBody,
  TableCaption,
  TableCell,
  TableHead,
  TableHeader,
  TableRow
} from "~/components/ui/table"
<Table>
  <TableCaption>A list of your recent invoices.</TableCaption>
  <TableHeader>
    <TableRow>
      <TableHead class="w-[120px]">Invoice</TableHead>
      <TableHead>Status</TableHead>
      <TableHead>Method</TableHead>
      <TableHead class="text-right">Amount</TableHead>
    </TableRow>
  </TableHeader>
  <TableBody>
    <TableRow>
      <TableCell class="font-medium">INV001</TableCell>
      <TableCell>Paid</TableCell>
      <TableCell>Credit Card</TableCell>
      <TableCell class="text-right">$250.00</TableCell>
    </TableRow>
  </TableBody>
</Table>

Composition

Use the following composition to build a Table:

Table
โ”œโ”€โ”€ TableCaption
โ”œโ”€โ”€ TableHeader
โ”‚   โ””โ”€โ”€ TableRow
โ”‚       โ”œโ”€โ”€ TableHead
โ”‚       โ”œโ”€โ”€ TableHead
โ”‚       โ”œโ”€โ”€ TableHead
โ”‚       โ””โ”€โ”€ TableHead
โ”œโ”€โ”€ TableBody
โ”‚   โ”œโ”€โ”€ TableRow
โ”‚   โ”‚   โ”œโ”€โ”€ TableCell
โ”‚   โ”‚   โ”œโ”€โ”€ TableCell
โ”‚   โ”‚   โ”œโ”€โ”€ TableCell
โ”‚   โ”‚   โ””โ”€โ”€ TableCell
โ”‚   โ””โ”€โ”€ TableRow
โ”‚       โ”œโ”€โ”€ TableCell
โ”‚       โ”œโ”€โ”€ TableCell
โ”‚       โ”œโ”€โ”€ TableCell
โ”‚       โ””โ”€โ”€ TableCell
โ””โ”€โ”€ TableFooter

Use the <TableFooter /> component to add a footer to the table.

A list of your recent invoices.
InvoiceStatusMethodAmount
INV001PaidCredit Card$250.00
INV002PendingPayPal$150.00
INV003UnpaidBank Transfer$350.00
Total$750.00
import { For } from "solid-js"

import {
  Table,
  TableBody,
  TableCaption,
  TableCell,
  TableFooter,
  TableHead,
  TableHeader,
  TableRow
} from "~/components/ui/table"

const invoices = [
  {
    id: "INV001",
    status: "Paid",
    method: "Credit Card",
    amount: "$250.00"
  },
  {
    id: "INV002",
    status: "Pending",
    method: "PayPal",
    amount: "$150.00"
  },
  {
    id: "INV003",
    status: "Unpaid",
    method: "Bank Transfer",
    amount: "$350.00"
  }
]

export default function TableFooterDemo() {
  return (
    <Table>
      <TableCaption>A list of your recent invoices.</TableCaption>
      <TableHeader>
        <TableRow>
          <TableHead class="w-[120px]">Invoice</TableHead>
          <TableHead>Status</TableHead>
          <TableHead>Method</TableHead>
          <TableHead class="text-right">Amount</TableHead>
        </TableRow>
      </TableHeader>
      <TableBody>
        <For each={invoices}>
          {(invoice) => (
            <TableRow>
              <TableCell class="font-medium">{invoice.id}</TableCell>
              <TableCell>{invoice.status}</TableCell>
              <TableCell>{invoice.method}</TableCell>
              <TableCell class="text-right">{invoice.amount}</TableCell>
            </TableRow>
          )}
        </For>
      </TableBody>
      <TableFooter>
        <TableRow>
          <TableCell class="font-medium" colspan={3}>
            Total
          </TableCell>
          <TableCell class="text-right">$750.00</TableCell>
        </TableRow>
      </TableFooter>
    </Table>
  )
}

Actions

A table showing actions for each row using a <DropdownMenu /> component.

ProductPriceActions
Wireless Mouse$29.99
Mechanical Keyboard$129.99
USB-C Hub$49.99
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.
import { For } from "solid-js"

import { Button } from "~/components/ui/button"
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuSeparator,
  DropdownMenuTrigger
} from "~/components/ui/dropdown-menu"
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow
} from "~/components/ui/table"

const products = [
  {
    id: "mouse",
    name: "Wireless Mouse",
    price: "$29.99"
  },
  {
    id: "keyboard",
    name: "Mechanical Keyboard",
    price: "$129.99"
  },
  {
    id: "hub",
    name: "USB-C Hub",
    price: "$49.99"
  }
]

export default function TableActions() {
  return (
    <Table>
      <TableHeader>
        <TableRow>
          <TableHead>Product</TableHead>
          <TableHead>Price</TableHead>
          <TableHead class="text-right">Actions</TableHead>
        </TableRow>
      </TableHeader>
      <TableBody>
        <For each={products}>
          {(product) => (
            <TableRow>
              <TableCell class="font-medium">{product.name}</TableCell>
              <TableCell>{product.price}</TableCell>
              <TableCell class="text-right">
                <DropdownMenu>
                  <DropdownMenuTrigger
                    render={(partProps) => (
                      <Button {...partProps} size="icon" variant="ghost" />
                    )}
                    class="size-8"
                  >
                    <svg
                      width="24"
                      height="24"
                      viewBox="0 0 24 24"
                      fill="none"
                      stroke="currentColor"
                      stroke-width="2"
                      stroke-linecap="round"
                      stroke-linejoin="round"
                      aria-hidden="true"
                    >
                      <circle cx="12" cy="12" r="1" />
                      <circle cx="19" cy="12" r="1" />
                      <circle cx="5" cy="12" r="1" />
                    </svg>
                    <span class="sr-only">Open menu</span>
                  </DropdownMenuTrigger>
                  <DropdownMenuContent side="bottom" align="end">
                    <DropdownMenuItem>Edit</DropdownMenuItem>
                    <DropdownMenuItem>Duplicate</DropdownMenuItem>
                    <DropdownMenuSeparator />
                    <DropdownMenuItem variant="destructive">
                      Delete
                    </DropdownMenuItem>
                  </DropdownMenuContent>
                </DropdownMenu>
              </TableCell>
            </TableRow>
          )}
        </For>
      </TableBody>
    </Table>
  )
}

Data Table

You can use the <Table /> component to build more complex data tables. It renders plain table markup, so it composes with any headless table library for sorting, filtering, and pagination. A Data Table guide will follow once a Solid 2-compatible TanStack Table adapter is available.

You can also see an example of a data table in the Tasks demo.