import { Meta, Story, Source } from "@storybook/addon-docs";
# Table
## Overview
All tables should have a visible horizontal header and label for each column.
The below code is the absolute minimum required to create a table. However we stronly advice you to
use the `dataSource` input to provide a data source for your table. This allows you to easily sort
data.
```html
Header 1
Header 2
Header 3
Cell 1
Cell 2
Cell 3
```
## Data Source
Bitwarden provides a data source for tables that can be used in place of a traditional data array.
The `TableDataSource` implements sorting and will in the future also support filtering. This allows
the `bitTable` component to focus on rendering while offloading the data management to the data
source.
```ts
// External data source
const data: T[];
const dataSource = new TableDataSource();
dataSource.data = data;
```
We use the `dataSource` as an input to the `bit-table` component, and access the rows to render
within the `ng-template`which provides access to the rows using `let-rows$`.
### Sortable
We provide a simple component for displaying sortable column headers. The `bitSortable` component
wires up to the `TableDataSource` and will automatically sort the data when clicked and display
an indicator for which column is currently sorted. The dafault sorting can be specified by setting
the `default`.
```html
Id
Name
```
It's also possible to define a custom sorting function by setting the `fn` input.
```ts
const sortFn = (a: T, b: T) => (a.id > b.id ? 1 : -1);
```
### Virtual Scrolling
It's heavily adviced to use virtual scrolling if you expect the table to have any significant amount
of data. This is easily done by wrapping the table in the `cdk-virtual-scroll-viewport` component,
specify a `itemSize`, set `scrollWindow` to `true` and replace `*ngFor` with `*cdkVirtualFor`.
```html
Id
Name
Other
{{ r.id }}
{{ r.name }}
{{ r.other }}
```
## Accessibility
- Always incude a row or column header with your table; this allows screen readers to better contextualize the data
- Avoid spanning data across cells