# Tables

> Learn how to build accessible tables using Cimpress UI components.

Source: https://ui.cimpress.io/dev-guides/tables/

Tables can be used to present users with interactive tabular information. A table can be a simple listing with 3-4 columns, but it can also be a highly interactive widget that allows users to select rows, hide and/or sort columns, and perform custom row actions.
Because of this complexity, Cimpress UI doesn't provide a single all-encompassing table component. Instead, it provides building blocks that allow you to build your own table that's tailored to the needs of your application and your dataset.

This guide will show you how to build your own table from Cimpress UI building blocks, and will walk you through any additional considerations for making your table accessible to more people.

## Data source

Cimpress UI doesn't assume anything about your data source. Our components help you display the data and all necessary controls, but they don't contain any data logic. It's up to you to modify your data based on actions performed by users.

While you can wire up a simple table by yourself using React state, we recommend using a headless data engine for more complex scenarios. Such an engine handles all state and interactions for you.

We recommend using [TanStack Table](https://tanstack.com/table/v8/docs) as your data engine. We also provide the `@cimpress-ui/tanstack-table-react` package, which contains a set of components and hooks that seamlessly connect TanStack Table with Cimpress UI components.

To use this package, install it as a dependency in your application alongside Cimpress UI:

**npm**

```sh
npm install @cimpress-ui/tanstack-table-react
```

**pnpm**

```sh
pnpm add @cimpress-ui/tanstack-table-react
```

**Yarn**

```sh
yarn add @cimpress-ui/tanstack-table-react
```

You do not need to install TanStack Table separately. All exports from `@tanstack/react-table` are re-exported from `@cimpress-ui/tanstack-table-react/base`.

## Prerequisites

We are going to build a table showcasing the highest-rated movies. This is what our data looks like:

```ts title="data.ts"
export interface Movie {
  title: string;
  releaseYear: number;
  rating: number;
}

export const movies: Movie[] = [
  { title: 'The Shawshank Redemption', releaseYear: 1994, rating: 9.3 },
  { title: 'The Godfather', releaseYear: 1972, rating: 9.2 },
  { title: 'The Godfather: Part II', releaseYear: 1974, rating: 9.0 },
  { title: 'The Dark Knight', releaseYear: 2008, rating: 9.0 },
  { title: '12 Angry Men', releaseYear: 1957, rating: 8.9 },
  { title: "Schindler's List", releaseYear: 1993, rating: 8.9 },
  { title: 'The Lord of the Rings: The Return of the King', releaseYear: 2003, rating: 8.9 },
  { title: 'Pulp Fiction', releaseYear: 1994, rating: 8.9 },
  { title: 'The Lord of the Rings: The Fellowship of the Ring', releaseYear: 2001, rating: 8.8 },
  { title: 'Forrest Gump', releaseYear: 1994, rating: 8.8 },
];
```

Additionally, we'll need a React component to render our table:

```ts title="table.tsx"
export function MovieTable() {
  return 'This will be a table';
}
```

## Basic data table

First, let's build a simple table with no interactivity. Choose the "Manual" or "TanStack" tab, depending on how you intend to build your table.

**Manual**

**TanStack**

### Column properties

Let's make our table look a little better. We'll assign specific widths to the columns, and align the content of "Release year" and "Rating" columns to the right.

**Manual**

**TanStack**

### Cell content

Make sure that the information within each cell can be conveyed by a screen reader. If a cell contains decorative elements that shouldn't be read (icons, emojis, etc.), mark them with the `aria-hidden` attribute.

Let's add a star symbol to our rating column. As an example, if a cell contains the text "⭐️ 9.3", a screen reader will read it as "star nine point three". To make it read "nine point three", mark the star and the space next to it with the `aria-hidden` attribute.

**Manual**

**TanStack**

### Final result

This is what our table looks like now. All following chapters will use this table as a starting point.

## Working with partial data

There are situations when your table can present partial data, such as when using pagination, or when not all columns are visible on the page. While this is usually obvious for sighted users based on the presence of additional UI controls, screen reader users may find it hard to orient themselves within such a table. To alleviate this, you should provide some additional information to table components.

### Subset of rows

If your table presents a subset of rows from a larger dataset (such as when using pagination), you should provide the following additional information:

- The number of rows in the full unpaginated dataset, including headers.
- The position of each row within the full unpaginated dataset, including headers.

Let's paginate our table, displaying 3 rows per page:

**Manual**

**TanStack**

Here is how our paginated table looks like. You can verify the presence of additional row-related attributes by inspecting the DOM.

#### Asynchronous loading

The example above assumes that each page's data is available immediately upon switching to that page. But in most scenarios the page switch needs to trigger an API call to fetch the data, creating a delay. In this case we recommend displaying a loading state to your users to make it clear that something is happening behind the scenes.

Building on the previous example, let's add a loading state to the paginated table:

**Manual**

**TanStack**

Here is how the asynchronously paginated table looks like. Try changing the page to see the loading state in action.

### Subset of columns

If your table presents a subset of columns from a larger dataset (such as when the user is allowed to hide columns), you should provide the following additional information:

- The number of columns in the full dataset.
- The position of each column within the full dataset.

Let's hide the "Release year" column from our table:

**Manual**

**TanStack**

Here is how our table looks like. You can verify the presence of additional column-related attributes by inspecting the DOM.

## No data

It's possible for the table to have no rows to display. This can happen when no data is available, or when the table rows are filtered. Let's see how we can display an "empty state" in our table if needed.

Let's assume our table of highest-rated movies has been filtered so that no rows meet the filtering criteria. We're not going to set up the filtering itself in this guide. Instead, in order to keep things simple, we'll assume that the `movies` array can be empty.

**Manual**

**TanStack**

Here is the finished table with no data to display:

## Row selection

Often we want users to be able to select specific rows in a table. Cimpress UI provides building blocks for row selection functionality, but you are in control of the selection state.

**Manual**

**TanStack**

Here is the finished table with selectable rows:

## Column sorting

Cimpress UI's table header cells have sorting capabilities built-in. As with other functionalities, you are in control of the sort state.

**Manual**

**TanStack**

Here is the finished table with sortable columns:
