Introduction
Data tables are difficult to componentize because of the wide variety of features they support, and the uniqueness of every data set. So instead of trying to create a one-size-fits-all solution, we’ve created a guide to help you build your own data tables. We’ll start with the basic<Table /> component, and work our way up to a fully-featured data table.
Tip: If you find yourself using the same table in multiple places, you can always extract it into a reusable component.
Table of Contents
This guide will show you how to use TanStack Table and the<Table /> component to build your own custom data table. We’ll cover the following topics:
Installation
1
Add the Table component
Add the
<Table /> component to your project along with the data-table helpers. These helpers enable TanStack Table v8 to work with Svelte 5 Snippets, Components, etc.2
Install TanStack Table
Add
@tanstack/table-core as a dependency:Prerequisites
We’re going to build a table to show recent payments. Here’s what our data looks like:Project Structure
Start by creating a route where your data table will live (we’ll call ours payments), along with the following files:columns.tswill contain our column definitions.data-table.sveltewill contain the<Table />component and the complete<DataTable />component.data-table-actions.sveltewill contain the actions menu for each row.data-table-checkbox.sveltewill contain the checkbox for each row.data-table-email-button.sveltewill contain the sortable email header button.+page.svelteis where we’ll render and access<DataTable />component.
Basic Table
Let’s start by building a basic table.1
Column Definitions
First, we’ll define our columns.
routes/payments/columns.ts
Note: Columns are where you define the core of what your table will look like. They define the data that will be displayed, how it will be formatted, sorted and filtered.
2
DataTable Component
Next, we’ll create a
<DataTable /> component to render our table.routes/payments/data-table.svelte
Tip: If you find yourself using
<DataTable /> in multiple places, this is the component you could make reusable by extracting it to components/ui/data-table.svelte.<DataTable columns={columns} data={data} />3
Render the table
Finally, we’ll render our table in our page component.
routes/payments/+page.server.ts
routes/payments/+page.svelte
Cell Formatting
Let’s format the amount cell to display the dollar amount. We’ll also align the cell to the right.1
Update columns definition
Update the We’re using the
header and cell definitions for amount as follows:routes/payments/columns.ts
createRawSnippet function to create a Svelte Snippet for rendering simple HTML elements that don’t require full lifecycle and state capabilities like a component. We then use the renderSnippet helper function to render the snippet.You can use the same approach to format other cells and headers.Row Actions
Let’s add row actions to our table. We’ll use the<DropdownMenu /> and the <Button /> components for this, so you have install them if not done already:
1
Create actions component
We’ll start by defining the actions menu in our
data-table-actions.svelte component.routes/payments/data-table-actions.svelte
2
Update columns definition
Now that we’ve defined the You can access the row data using
<DataTableActions /> component, let’s update our actions column definition to use it.routes/payments/columns.ts
row.original in the cell function. Use this to handle actions for your row eg. use the id to make a DELETE call to your API.Pagination
Next, we’ll add pagination to our table.1
Update DataTable
routes/payments/data-table.svelte
2
Adding pagination controls
We can add pagination controls to our table using the See Reusable Components section for a more advanced pagination component.
<Button /> component and the table.previousPage(), table.nextPage() API methods.routes/payments/data-table.svelte
Sorting
Let’s make the email column sortable.1
Define DataTableEmailButton component
We’ll start by creating a component to render a sortable email header button.
routes/payments/data-table-email-button.svelte
2
Update DataTable
routes/payments/data-table.svelte
3
Make header cell sortable
We can now update the This will automatically sort the table (asc and desc) when the user toggles on the header cell.
email header cell to add sorting controls.src/routes/payments/columns.ts
Filtering
Let’s add a search input to filter emails in our table.1
Update DataTable
routes/payments/data-table.svelte
email column. You can add filters to other columns as well. See the filtering docs for more information on customizing filters.Visibility
Adding column visibility is fairly simple using@tanstack/table-core visibility API.
1
Update DataTable
routes/payments/data-table.svelte
Row Selection
Next, we’re going to add row selection to our table.1
Define DataTableCheckbox component
We’ll start by defining the checkbox component in our
data-table-checkbox.svelte component.routes/payments/data-table-checkbox.svelte
2
Update columns definition
Now that we have a new component, we can add a
select column definition to render a checkbox.routes/payments/columns.ts
3
Update DataTable
routes/payments/data-table.svelte
4
Show selected rows
You can show the number of selected rows using the
table.getFilteredSelectedRowModel() API.