Table Components
Default table
Use the following example of a responsive table component to show multiple rows and columns of text data.
<Table>
<TableHead>
<TableHeadCell>Product name</TableHeadCell>
<TableHeadCell>Color</TableHeadCell>
<TableHeadCell>Category</TableHeadCell>
<TableHeadCell>Price</TableHeadCell>
<TableHeadCell>
<span class="sr-only"> Edit </span>
</TableHeadCell>
</TableHead>
<TableBody class="divide-y">
<TableBodyRow>
<TableBodyCell>
Apple MacBook Pro 17"
</TableBodyCell>
<TableBodyCell>Sliver</TableBodyCell>
<TableBodyCell>Laptop</TableBodyCell>
<TableBodyCell>$2999</TableBodyCell>
<TableBodyCell>
<a href="/tables" class="font-medium text-blue-600 hover:underline dark:text-blue-500"> Edit </a>
</TableBodyCell>
</TableBodyRow>
<TableBodyRow>
<TableBodyCell>
Microsoft Surface Pro
</TableBodyCell>
<TableBodyCell>White</TableBodyCell>
<TableBodyCell>Laptop PC</TableBodyCell>
<TableBodyCell>$1999</TableBodyCell>
<TableBodyCell>
<a href="/tables" class="font-medium text-blue-600 hover:underline dark:text-blue-500"> Edit </a>
</TableBodyCell>
</TableBodyRow>
<TableBodyRow>
<TableBodyCell>
Magic Mouse 2
</TableBodyCell>
<TableBodyCell>Black</TableBodyCell>
<TableBodyCell>Accessories</TableBodyCell>
<TableBodyCell>$99</TableBodyCell>
<TableBodyCell>
<a href="/tables" class="font-medium text-blue-600 hover:underline dark:text-blue-500"> Edit </a>
</TableBodyCell>
</TableBodyRow>
</TableBody>
</Table>
Striped rows
Set the `stiped` prop to `true` to alternate background colors of every second tabel row.
<Table striped={true}>
<TableHead>
<TableHeadCell>Product name</TableHeadCell>
<TableHeadCell>Color</TableHeadCell>
<TableHeadCell>Category</TableHeadCell>
<TableHeadCell>Price</TableHeadCell>
<TableHeadCell>
<span class="sr-only"> Edit </span>
</TableHeadCell>
</TableHead>
<TableBody class="divide-y">
<TableBodyRow>
<TableBodyCell>
Apple MacBook Pro 17"
</TableBodyCell>
<TableBodyCell>Sliver</TableBodyCell>
<TableBodyCell>Laptop</TableBodyCell>
<TableBodyCell>$2999</TableBodyCell>
<TableBodyCell>
<a href="/tables" class="font-medium text-blue-600 hover:underline dark:text-blue-500"> Edit </a>
</TableBodyCell>
</TableBodyRow>
<TableBodyRow>
<TableBodyCell>
Microsoft Surface Pro
</TableBodyCell>
<TableBodyCell>White</TableBodyCell>
<TableBodyCell>Laptop PC</TableBodyCell>
<TableBodyCell>$1999</TableBodyCell>
<TableBodyCell>
<a href="/tables" class="font-medium text-blue-600 hover:underline dark:text-blue-500"> Edit </a>
</TableBodyCell>
</TableBodyRow>
<TableBodyRow>
<TableBodyCell>
Magic Mouse 2
</TableBodyCell>
<TableBodyCell>Black</TableBodyCell>
<TableBodyCell>Accessories</TableBodyCell>
<TableBodyCell>$99</TableBodyCell>
<TableBodyCell>
<a href="/tables" class="font-medium text-blue-600 hover:underline dark:text-blue-500"> Edit </a>
</TableBodyCell>
</TableBodyRow>
<TableBodyRow>
<TableBodyCell>
Google Pixel Phone
</TableBodyCell>
<TableBodyCell>Gray</TableBodyCell>
<TableBodyCell>Phone</TableBodyCell>
<TableBodyCell>$799</TableBodyCell>
<TableBodyCell>
<a href="/tables" class="font-medium text-blue-600 hover:underline dark:text-blue-500"> Edit </a>
</TableBodyCell>
</TableBodyRow>
<TableBodyRow>
<TableBodyCell>
Apple Watch 5
</TableBodyCell>
<TableBodyCell>Red</TableBodyCell>
<TableBodyCell>Wearables</TableBodyCell>
<TableBodyCell>$999</TableBodyCell>
<TableBodyCell>
<a href="/tables" class="font-medium text-blue-600 hover:underline dark:text-blue-500"> Edit </a>
</TableBodyCell>
</TableBodyRow>
</TableBody>
</Table>
Hover state
Set `hoverable` to `true` to change the background color of a data row when hovering over the element with the cursor.
<Table hoverable={true}>
<TableHead>
<TableHeadCell>Product name</TableHeadCell>
<TableHeadCell>Color</TableHeadCell>
<TableHeadCell>Category</TableHeadCell>
<TableHeadCell>Price</TableHeadCell>
<TableHeadCell>
<span class="sr-only"> Edit </span>
</TableHeadCell>
</TableHead>
<TableBody class="divide-y">
<TableBodyRow>
<TableBodyCell>
Apple MacBook Pro 17"
</TableBodyCell>
<TableBodyCell>Sliver</TableBodyCell>
<TableBodyCell>Laptop</TableBodyCell>
<TableBodyCell>$2999</TableBodyCell>
<TableBodyCell>
<a href="/tables" class="font-medium text-blue-600 hover:underline dark:text-blue-500"> Edit </a>
</TableBodyCell>
</TableBodyRow>
<TableBodyRow>
<TableBodyCell>
Microsoft Surface Pro
</TableBodyCell>
<TableBodyCell>White</TableBodyCell>
<TableBodyCell>Laptop PC</TableBodyCell>
<TableBodyCell>$1999</TableBodyCell>
<TableBodyCell>
<a href="/tables" class="font-medium text-blue-600 hover:underline dark:text-blue-500"> Edit </a>
</TableBodyCell>
</TableBodyRow>
<TableBodyRow>
<TableBodyCell>
Magic Mouse 2
</TableBodyCell>
<TableBodyCell>Black</TableBodyCell>
<TableBodyCell>Accessories</TableBodyCell>
<TableBodyCell>$99</TableBodyCell>
<TableBodyCell>
<a href="/tables" class="font-medium text-blue-600 hover:underline dark:text-blue-500"> Edit </a>
</TableBodyCell>
</TableBodyRow>
</TableBody>
</Table>
Checkbox
Checkboxes can be used inside table data rows to select multiple data sets and apply a bulk action.
<Table hoverable={true}>
<TableHead>
<TableHeadCell class="!p-4">
<Checkbox />
</TableHeadCell>
<TableHeadCell>Product name</TableHeadCell>
<TableHeadCell>Color</TableHeadCell>
<TableHeadCell>Category</TableHeadCell>
<TableHeadCell>Price</TableHeadCell>
<TableHeadCell>
<span class="sr-only"> Edit </span>
</TableHeadCell>
</TableHead>
<TableBody class="divide-y">
<TableBodyRow>
<TableBodyCell class="!p-4">
<Checkbox />
</TableBodyCell>
<TableBodyCell>
Apple MacBook Pro 17"
</TableBodyCell>
<TableBodyCell>Sliver</TableBodyCell>
<TableBodyCell>Laptop</TableBodyCell>
<TableBodyCell>$2999</TableBodyCell>
<TableBodyCell>
<a href="/tables" class="font-medium text-blue-600 hover:underline dark:text-blue-500"> Edit </a>
</TableBodyCell>
</TableBodyRow>
<TableBodyRow>
<TableBodyCell class="!p-4">
<Checkbox />
</TableBodyCell>
<TableBodyCell>
Microsoft Surface Pro
</TableBodyCell>
<TableBodyCell>White</TableBodyCell>
<TableBodyCell>Laptop PC</TableBodyCell>
<TableBodyCell>$1999</TableBodyCell>
<TableBodyCell>
<a href="/tables" class="font-medium text-blue-600 hover:underline dark:text-blue-500"> Edit </a>
</TableBodyCell>
</TableBodyRow>
<TableBodyRow>
<TableBodyCell class="!p-4">
<Checkbox />
</TableBodyCell>
<TableBodyCell>
Magic Mouse 2
</TableBodyCell>
<TableBodyCell>Black</TableBodyCell>
<TableBodyCell>Accessories</TableBodyCell>
<TableBodyCell>$99</TableBodyCell>
<TableBodyCell>
<a href="/tables" class="font-medium text-blue-600 hover:underline dark:text-blue-500"> Edit </a>
</TableBodyCell>
</TableBodyRow>
</TableBody>
</Table>
Search input
ID | Maker | Type | Make |
---|---|---|---|
1 | Toyota | ABC | 2017 |
2 | Ford | CDE | 2018 |
3 | Volvo | FGH | 2019 |
4 | Saab | IJK | 2020 |
<script>
let searchTerm = '';
let items = [
{ id: 1, maker: 'Toyota', type: 'ABC', make: 2017 },
{ id: 2, maker: 'Ford', type: 'CDE', make: 2018 },
{ id: 3, maker: 'Volvo', type: 'FGH', make: 2019 },
{ id: 4, maker: 'Saab', type: 'IJK', make: 2020 }
];
$: filteredItems = items.filter(
(item) => item.maker.toLowerCase().indexOf(searchTerm.toLowerCase()) !== -1
);
</script>
<TableSearch placeholder="Search by maker name" hoverable={true} bind:inputValue={searchTerm}>
<TableHead>
<TableHeadCell>ID</TableHeadCell>
<TableHeadCell>Maker</TableHeadCell>
<TableHeadCell>Type</TableHeadCell>
<TableHeadCell>Make</TableHeadCell>
</TableHead>
<TableBody class="divide-y">
{#each filteredItems as item}
<TableBodyRow>
<TableBodyCell>{item.id}</TableBodyCell>
<TableBodyCell>{item.maker}</TableBodyCell>
<TableBodyCell>{item.type}</TableBodyCell>
<TableBodyCell>{item.make}</TableBodyCell>
</TableBodyRow>
{/each}
</TableBody>
</TableSearch>
Props
The component has the following props, type, and default values. See types page for type information.
Table
Name | Type | Default |
---|---|---|
divClass | string | 'relative overflow-x-auto shadow-md sm:rounded-lg' |
striped | boolean | false |
hoverable | boolean | false |
TableBodyCell
Name | Type | Default |
---|---|---|
tdClass | string | 'px-6 py-4 whitespace-nowrap font-medium text-gray-900 dark:text-white' |
TableBodyRow
Name | Type | Default |
---|---|---|
trClass | string | 'bg-white dark:border-gray-700 dark:bg-gray-800' |
TableSearch
Name | Type | Default |
---|---|---|
divClass | string | 'relative overflow-x-auto shadow-md sm:rounded-lg' |
inputValue | string | '' |
striped | boolean | false |
hoverable | boolean | false |
placeholder | string | 'Search' |