Skip to content

Commit e808abf

Browse files
authored
docs: add column visibility docs (#5362)
1 parent c42e95a commit e808abf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+164
-44
lines changed

docs/api/core/cell.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Cell
2+
title: Cell APIs
33
---
44

55
These are **core** options and API properties for all cells. More options and API properties are available for other [table features](../../guide/features).

docs/api/core/column-def.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: ColumnDef
2+
title: ColumnDef APIs
33
---
44

55
Column definitions are plain objects with the following options:

docs/api/core/column.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Column
2+
title: Column APIs
33
---
44

55
These are **core** options and API properties for all columns. More options and API properties are available for other [table features](../../guide/features).

docs/api/core/header-group.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: HeaderGroup
2+
title: HeaderGroup APIs
33
---
44

55
These are **core** options and API properties for all header groups. More options and API properties may be available for other [table features](../../guide/features).

docs/api/core/header.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Header
2+
title: Header APIs
33
---
44

55
These are **core** options and API properties for all headers. More options and API properties may be available for other [table features](../../guide/features).

docs/api/core/row.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Row
2+
title: Row APIs
33
---
44

55
These are **core** options and API properties for all rows. More options and API properties are available for other [table features](../../guide/features).

docs/api/core/table.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Table
2+
title: Table APIs
33
---
44

55
## `useReactTable` / `createSolidTable` / `useVueTable` / `createSvelteTable`

docs/api/features/column-ordering.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Column Ordering
2+
title: Column Ordering APIs
33
id: column-ordering
44
---
55

docs/api/features/column-sizing.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Column Sizing
2+
title: Column Sizing APIs
33
id: column-sizing
44
---
55

docs/api/features/column-visibility.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Column Visibility
2+
title: Column Visibility APIs
33
id: column-visibility
44
---
55

docs/api/features/expanding.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Expanding
2+
title: Expanding APIs
33
id: expanding
44
---
55

docs/api/features/filters.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Filters
2+
title: Filter APIs
33
id: filters
44
---
55

docs/api/features/grouping.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Grouping
2+
title: Grouping APIs
33
id: grouping
44
---
55

docs/api/features/pagination.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Pagination
2+
title: Pagination APIs
33
id: pagination
44
---
55

docs/api/features/pinning.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Pinning
2+
title: Pinning APIs
33
id: pinning
44
---
55

docs/api/features/row-selection.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Row Selection
2+
title: Row Selection APIs
33
id: row-selection
44
---
55

docs/api/features/sorting.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Sorting
2+
title: Sorting APIs
33
id: sorting
44
---
55

docs/guide/cells.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Cells
2+
title: Cells Guide
33
---
44

55
## API

docs/guide/column-defs.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Columns
2+
title: Columns Guide
33
---
44

55
## API

docs/guide/column-filtering.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Column Filtering
2+
title: Column Filtering Guide
33
---
44

55
## Examples

docs/guide/column-ordering.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Column Ordering
2+
title: Column Ordering Guide
33
---
44

55
## Examples

docs/guide/column-pinning.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Column Pinning
2+
title: Column Pinning Guide
33
---
44

55
## Examples

docs/guide/column-sizing.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Column Sizing
2+
title: Column Sizing Guide
33
---
44

55
## Examples

docs/guide/column-visibility.md

+117-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Column Visibility
2+
title: Column Visibility Guide
33
---
44

55
## Examples
@@ -8,9 +8,124 @@ Want to skip to the implementation? Check out these examples:
88

99
- [column-visibility](../framework/react/examples/column-visibility)
1010
- [column-ordering](../framework/react/examples/column-ordering)
11+
- [sticky-column-pinning](../framework/react/examples/column-pinning-sticky)
12+
13+
### Other Examples
14+
15+
- [SolidJS column-visibility](../framework/solid/examples/column-visibility)
16+
- [Svelte column-visibility](../framework/svelte/examples/column-visibility)
1117

1218
## API
1319

1420
[Column Visibility API](../api/features/column-visibility)
1521

16-
## Column Visibility Guide
22+
## Column Visibility Guide
23+
24+
The column visibility feature allows table columns to be hidden or shown dynamically. In previous versions of react-table, this feature was a static property on a column, but in v8, there is a dedicated `columnVisibility` state and APIs for managing column visibility dynamically.
25+
26+
### Column Visibility State
27+
28+
The `columnVisibility` state is a map of column IDs to boolean values. A column will be hidden if its ID is present in the map and the value is `false`. If the column ID is not present in the map, or the value is `true`, the column will be shown.
29+
30+
```jsx
31+
const [columnVisibility, setColumnVisibility] = useState({
32+
columnId1: true,
33+
columnId2: false, //hide this column by default
34+
columnId3: true,
35+
});
36+
37+
const table = useReactTable({
38+
//...
39+
state: {
40+
columnVisibility,
41+
//...
42+
},
43+
onColumnVisibilityChange: setColumnVisibility,
44+
});
45+
```
46+
47+
Alternatively, if you don't need to manage the column visibility state outside of the table, you can still set the initial default column visibility state using the `initialState` option.
48+
49+
> **Note**: If `columnVisibility` is provided to both `initialState` and `state`, the `state` initialization will take precedence and `initialState` will be ignored. Do not provide `columnVisibility` to both `initialState` and `state`, only one or the other.
50+
51+
```jsx
52+
const table = useReactTable({
53+
//...
54+
initialState: {
55+
columnVisibility: {
56+
columnId1: true,
57+
columnId2: false, //hide this column by default
58+
columnId3: true,
59+
},
60+
//...
61+
},
62+
});
63+
```
64+
65+
### Disable Hiding Columns
66+
67+
By default, all columns can be hidden or shown. If you want to prevent certain columns from being hidden, you set the `enableHiding` column option to `false` for those columns.
68+
69+
```jsx
70+
const columns = [
71+
{
72+
header: 'ID',
73+
accessorKey: 'id',
74+
enableHiding: false, // disable hiding for this column
75+
},
76+
{
77+
header: 'Name',
78+
accessor: 'name', // can be hidden
79+
},
80+
];
81+
```
82+
83+
### Column Visibility Toggle APIs
84+
85+
There are several column API methods that are useful for rendering column visibility toggles in the UI.
86+
87+
- `column.getCanHide` - Useful for disabling the visibility toggle for a column that has `enableHiding` set to `false`.
88+
- `column.getIsVisible` - Useful for setting the initial state of the visibility toggle.
89+
- `column.toggleVisibility` - Useful for toggling the visibility of a column.
90+
- `column.getToggleVisibilityHandler` - Shortcut for hooking up the `column.toggleVisibility` method to a UI event handler.
91+
92+
```jsx
93+
{table.getAllColumns().map((column) => (
94+
<label key={column.id}>
95+
<input
96+
checked={column.getIsVisible()}
97+
disabled={!column.getCanHide()}
98+
onChange={column.getToggleVisibilityHandler()}
99+
type="checkbox"
100+
/>
101+
{column.columnDef.header}
102+
</label>
103+
))}
104+
```
105+
106+
### Column Visibility Aware Table APIs
107+
108+
When you render your header, body, and footer cells, there are a lot of API options available. You may see APIs like `table.getAllLeafColumns` and `row.getAllCells`, but if you use these APIs, they will not take column visibility into account. Instead, you need to use the "visible" variants of these APIs, such as `table.getVisibleLeafColumns` and `row.getVisibleCells`.
109+
110+
```jsx
111+
<table>
112+
<thead>
113+
<tr>
114+
{table.getVisibleLeafColumns().map((column) => ( // takes column visibility into account
115+
//
116+
))}
117+
</tr>
118+
</thead>
119+
<tbody>
120+
{table.getRowModel().rows.map((row) => (
121+
<tr key={row.id}>
122+
{row.getVisibleCells().map((cell) => ( // takes column visibility into account
123+
//
124+
))}
125+
</tr>
126+
))}
127+
</tbody>
128+
</table>
129+
```
130+
131+
If you are using the Header Group APIs, they will already take column visibility into account.

docs/guide/expanding.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Expanding
2+
title: Expanding Guide
33
---
44

55
## Examples

docs/guide/faceted-values.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Faceted Values
2+
title: Faceted Values Guide
33
---
44

55
## Examples

docs/guide/features.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Features
2+
title: Features Guide
33
---
44

55
TanStack Table comes with many features, each with their own associated options and API:

docs/guide/filters.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Filters
2+
title: Filters Guide
33
---
44

55
<!-- Deprecated -->

docs/guide/fuzzy-filtering.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Fuzzy Filtering
2+
title: Fuzzy Filtering Guide
33
---
44

55
## Examples

docs/guide/global-filtering.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Global Filtering
2+
title: Global Filtering Guide
33
---
44

55
## Examples

docs/guide/grouping.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Grouping
2+
title: Grouping Guide
33
---
44

55
## Examples

docs/guide/headers.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Headers
2+
title: Headers Guide
33
---
44

55
## API

docs/guide/migrating.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Migrating to V8
2+
title: Migrating to V8 Guide
33
---
44

55
## Migrating to V8

docs/guide/pagination.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Pagination
2+
title: Pagination Guide
33
---
44

55
## Examples

docs/guide/pinning.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Pinning
2+
title: Pinning Guide
33
---
44

55
<!-- Deprecated -->

docs/guide/row-models.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Row Models
2+
title: Row Models Guide
33
---
44

55
## Row Models Guide

docs/guide/row-pinning.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Row Pinning
2+
title: Row Pinning Guide
33
---
44

55
## Examples

docs/guide/row-selection.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Row Selection
2+
title: Row Selection Guide
33
---
44

55
## Examples

docs/guide/rows.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Rows
2+
title: Rows Guide
33
---
44

55
## API

0 commit comments

Comments
 (0)