Skip to main content

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.ts will contain our column definitions.
  • data-table.svelte will contain the <Table /> component and the complete <DataTable /> component.
  • data-table-actions.svelte will contain the actions menu for each row.
  • data-table-checkbox.svelte will contain the checkbox for each row.
  • data-table-email-button.svelte will contain the sortable email header button.
  • +page.svelte is 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 header and cell definitions for amount as follows:
routes/payments/columns.ts
We’re using the 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 <DataTableActions /> component, let’s update our actions column definition to use it.
routes/payments/columns.ts
You can access the row data using 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
This will automatically paginate your rows into pages of 10. See the pagination docs for more information on customizing page size and implementing manual pagination.
2

Adding pagination controls

We can add pagination controls to our table using the <Button /> component and the table.previousPage(), table.nextPage() API methods.
routes/payments/data-table.svelte
See Reusable Components section for a more advanced pagination component.

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 email header cell to add sorting controls.
src/routes/payments/columns.ts
This will automatically sort the table (asc and desc) when the user toggles on the header cell.

Filtering

Let’s add a search input to filter emails in our table.
1

Update DataTable

routes/payments/data-table.svelte
Filtering is now enabled for the 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
This adds a dropdown menu that you can use to toggle column visibility.

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
This adds a checkbox to each row and a checkbox in the header to select all rows.
4

Show selected rows

You can show the number of selected rows using the table.getFilteredSelectedRowModel() API.

Reusable Components

Check out the Tasks example to learn about creating reusable components for your data tables.