Skip to content

Commit 102ffd2

Browse files
authoredMay 8, 2024··
docs: header and column docs (#5531)
* docs: header and column docs * undo example log
1 parent 64ef24c commit 102ffd2

File tree

5 files changed

+145
-3
lines changed

5 files changed

+145
-3
lines changed
 

‎docs/guide/cells.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ title: Cells Guide
88

99
## Cells Guide
1010

11-
This quick guide will discuss the different ways you can retrieve and interact with cell objects in TanStack Table.
11+
This quick guide will discuss the different ways you can retrieve and interact with `cell` objects in TanStack Table.
1212

1313
### Where to Get Cells From
1414

‎docs/guide/column-defs.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ title: Columns Guide
88

99
## Column Definitions Guide
1010

11+
> Note: This guide is about setting up column definitions for your table and NOT about the actual [`column`](../columns) objects that are generated within the table instance.
12+
1113
Column defs are the single most important part of building a table. They are responsible for:
1214

1315
- Building the underlying data model that will be used for everything including sorting, filtering, grouping, etc.

‎docs/guide/columns.md

+64-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,67 @@ title: Columns Guide
66

77
[Header API](../../api/core/column)
88

9-
## Columns Guide
9+
## Columns Guide
10+
11+
> Note: This guide is about the actual `column` objects that are generated within the table instance and NOT about setting up the [column definitions](../column-defs) for your table.
12+
13+
This quick guide will discuss the different ways you can retrieve and interact with `column` objects in TanStack Table.
14+
15+
### Where to Get Columns From
16+
17+
You can find the `column` objects in many places. They are often attached
18+
19+
#### Header and Cell Objects
20+
21+
Before you reach for one of the `table` instance APIs, consider if you actually need to retrieve either [`headers`](../headers) or [`cells`](../cells) instead of `columns`. If you are rending out the markup for your table, you will most likely want to reach for the APIs that return headers or cells instead of columns. The column objects themselves are not really meant to render out the headers or cells, but the `header` and `cell` objects will contain references to these `column` objects from which they can derive the necessary information to render their UI.
22+
23+
```js
24+
const column = cell.column; // get column from cell
25+
const column = header.column; // get column from header
26+
```
27+
28+
#### Column Table Instance APIs
29+
30+
There are dozens of `table` instance APIs you can use to retrieve columns from the table instance. Which APIs you will use will depend entirely on which features you are using in your table and your use-case.
31+
32+
##### Get Column
33+
34+
If you need to just get a single column by its ID, you can use the `table.getColumn` API.
35+
36+
```js
37+
const column = table.getColumn('firstName');
38+
```
39+
40+
##### Get Columns
41+
42+
The simplest column API is `table.getAllColumns`, which will return a list of all columns in the table. There are dozens of other column APIs that are affected by other features and the state of the table that come alongside this API though. `table.getAllFlatColumns`, `table.getAllLeafColumns`, `getCenterLeafColumns`, `table.getLeftVisibleLeafColumns` are just some examples of other column APIs that you might use in tandem with the column visibility or column pinning features.
43+
44+
### Column Objects
45+
46+
Column objects are not actually meant to be used to render out the table UI directly, so they are not associated 1-to-1 with any `<th>` or `<td>` elements in your table, but they contain a lot of useful properties and methods that you can use to interact with the table state.
47+
48+
#### Column IDs
49+
50+
Every column must have a unique `id` defined in their associated [Column Definition](../column-defs). Usually, you define this `id` yourself, or it is derived from the `accessorKey` or `header` properties in the column definition.
51+
52+
#### ColumnDef
53+
54+
A reference to the original `columnDef` object that was used to created the column is always available on the column object.
55+
56+
#### Nested Grouped Columns Properties
57+
58+
There are a few properties on `column` objects that are only useful if the column is part of a nested or grouped column structure. These properties include:
59+
60+
- `columns`: An array of child columns that belong to a group column.
61+
- `depth`: The header group "row index" that the column group belongs to.
62+
- `parent`: The parent column of the column. If the column is a top-level column, this will be `undefined`.
63+
64+
### More Column APIs
65+
66+
There are dozens of Column APIs that you can use to interact with the table state and extract cell values from the table based on the state of the table. See each features column API documentation for more information.
67+
68+
### Column Rendering
69+
70+
Don't necessarily use `column` objects to render `headers` or `cells` directly. Instead, use the [`header](../headers) and [`cell`](../cells) objects, as discussed above.
71+
72+
But if you are just rendering a list of columns somewhere else in your UI for something like a column visibility menu or something similar, you can just map over a columns array and render out the UI as you normally would.

‎docs/guide/headers.md

+76-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,79 @@ title: Headers Guide
66

77
[Header API](../../api/core/header)
88

9-
## Headers Guide
9+
## Headers Guide
10+
11+
This quick guide will discuss the different ways you can retrieve and interact with `header` objects in TanStack Table.
12+
13+
Headers are the equivalent of cells, but meant for the `<thead>` section of the table instead of the `<tbody>` section.
14+
15+
### Where to Get Headers From
16+
17+
Headers come from [Header Groups](../header-groups), which are the equivalent of rows, but meant for the `<thead>` section of the table instead of the `<tbody>` section.
18+
19+
#### HeaderGroup Headers
20+
21+
If you are in a header group, the headers are stored as an array in the `headerGroup.headers` property. Usually you will just map over this array to render your headers.
22+
23+
```jsx
24+
<thead>
25+
{table.getHeaderGroups().map(headerGroup => {
26+
return (
27+
<tr key={headerGroup.id}>
28+
{headerGroup.headers.map(header => ( // map over the headerGroup headers array
29+
<th key={header.id} colSpan={header.colSpan}>
30+
{/* */}
31+
</th>
32+
))}
33+
</tr>
34+
)
35+
})}
36+
</thead>
37+
```
38+
39+
#### Header Table Instance APIs
40+
41+
There are multiple `table` instance APIs that you can use to retrieve a list of headers depending on the features that you are using. The most common API you might use is `table.getFlatHeaders`, which will return a flat list of all headers in the table, but there are at least a dozen other headers that are useful in tandem with the column visibility and column pinning features. APIs like `table.getLeftLeafHeaders` or `table.getRightFlatHeaders` could be useful depending on your use case.
42+
43+
### Header Objects
44+
45+
Header objects are similar to [Cell](../cells) objects, but meant for the `<thead>` section of the table instead of the `<tbody>` section. Every header object can be associated with a `<th>` or similar cell element in your UI. There are a few properties and methods on `header` objects that you can use to interact with the table state and extract cell values from the table based on the state of the table.
46+
47+
#### Header IDs
48+
49+
Every header object has an `id` property that makes it unique within the table instance. Usually you only need this `id` as a unique identifier for React keys or if you are following the [performant column resizing example](../../framework/react/examples/column-resizing-performant).
50+
51+
For simple headers with no advanced nested or grouped headers logic, the `header.id` will be the same as it's parent `column.id`. However, if the header is part group column or a placeholder cell, it will have a more complicated id that is constructed from the header family, depth/header row index, column id, and header group id.
52+
53+
#### Nested Grouped Headers Properties
54+
55+
There are a few properties on `header` objects that are only useful if the header is part of a nested or grouped header structure. These properties include:
56+
57+
- `colspan`: The number of columns that the header should span. This is useful for rendering the `colSpan` attribute on the `<th>` element.
58+
- `rowSpan`: The number of rows that the header should span. This is useful for rendering the `rowSpan` attribute on the `<th>` element. (Currently not implemented in default TanStack Table)
59+
- `depth`: The header group "row index" that the header group belongs to.
60+
- `isPlaceholder`: A boolean flag that is true if the header is a placeholder header. Placeholder headers are used to fill in the gaps when a column is hidden or when a column is part of a group column.
61+
- `placeholderId`: The unique identifier for the placeholder header.
62+
- `subHeaders`: The array of sub/child headers that belong to this header. Will be empty if the header is a leaf header.
63+
64+
> Note: `header.index` refers to its index within the header group (row of headers), i.e. its position from left to right. It is not the same as `header.depth`, which refers to the header group "row index".
65+
66+
#### Header Parent Objects
67+
68+
Every header stores a reference to its parent [column](../columns) object and its parent [header group](../header-groups) object.
69+
70+
### More Header APIs
71+
72+
Headers have a few more useful APIs attached to them that are useful for interacting with the table state. Most of them relate to the Column sizing and resizing features. See the [Column Resizing Guide](../column-resizing) for more information.
73+
74+
### Header Rendering
75+
76+
Since the `header` column option you defined can be either a string, jsx, or a function returning either of those, the best way to render the headers is to use the `flexRender` utility from your adapter, which will handle all of those cases for you.
77+
78+
```jsx
79+
{headerGroup.headers.map(header => (
80+
<th key={header.id} colSpan={header.colSpan}>
81+
{/* Handles all possible header column def scenarios for `header` */}
82+
{flexRender(header.column.columnDef.header, header.getContext())}
83+
</th>
84+
))}

‎docs/guide/tables.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ title: Table Instance Guide
1010

1111
TanStack Table is a headless UI library. When we talk about the `table` or "table instance", we're not talking about a literal `<table>` element. Instead, we're referring to the core table object that contains the table state and APIs. The `table` instance is created by calling your adapter's `createTable` function (e.g. `useReactTable`, `createSolidTable`, `createSvelteTable`, `useQwikTable`, `useVueTable`).
1212

13+
The `table` instance that is returned from the `createTable` function (from the framework adapter) is the main object that you will interact with to read and mutate the table state. It is the one place where everything happens in TanStack Table.
14+
1315
### Creating a Table Instance
1416

1517
To create a table instance, 2 `options` are required: `columns` and `data`. There are dozens of other table options to configure features and behavior, but these 2 are required.

0 commit comments

Comments
 (0)
Please sign in to comment.