Skip to content

Commit

Permalink
docs: add column visibility docs (#5362)
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVandy committed Feb 18, 2024
1 parent c42e95a commit e808abf
Show file tree
Hide file tree
Showing 44 changed files with 164 additions and 44 deletions.
2 changes: 1 addition & 1 deletion docs/api/core/cell.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Cell
title: Cell APIs
---

These are **core** options and API properties for all cells. More options and API properties are available for other [table features](../../guide/features).
Expand Down
2 changes: 1 addition & 1 deletion docs/api/core/column-def.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: ColumnDef
title: ColumnDef APIs
---

Column definitions are plain objects with the following options:
Expand Down
2 changes: 1 addition & 1 deletion docs/api/core/column.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Column
title: Column APIs
---

These are **core** options and API properties for all columns. More options and API properties are available for other [table features](../../guide/features).
Expand Down
2 changes: 1 addition & 1 deletion docs/api/core/header-group.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: HeaderGroup
title: HeaderGroup APIs
---

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).
Expand Down
2 changes: 1 addition & 1 deletion docs/api/core/header.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Header
title: Header APIs
---

These are **core** options and API properties for all headers. More options and API properties may be available for other [table features](../../guide/features).
Expand Down
2 changes: 1 addition & 1 deletion docs/api/core/row.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Row
title: Row APIs
---

These are **core** options and API properties for all rows. More options and API properties are available for other [table features](../../guide/features).
Expand Down
2 changes: 1 addition & 1 deletion docs/api/core/table.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Table
title: Table APIs
---

## `useReactTable` / `createSolidTable` / `useVueTable` / `createSvelteTable`
Expand Down
2 changes: 1 addition & 1 deletion docs/api/features/column-ordering.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Column Ordering
title: Column Ordering APIs
id: column-ordering
---

Expand Down
2 changes: 1 addition & 1 deletion docs/api/features/column-sizing.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Column Sizing
title: Column Sizing APIs
id: column-sizing
---

Expand Down
2 changes: 1 addition & 1 deletion docs/api/features/column-visibility.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Column Visibility
title: Column Visibility APIs
id: column-visibility
---

Expand Down
2 changes: 1 addition & 1 deletion docs/api/features/expanding.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Expanding
title: Expanding APIs
id: expanding
---

Expand Down
2 changes: 1 addition & 1 deletion docs/api/features/filters.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Filters
title: Filter APIs
id: filters
---

Expand Down
2 changes: 1 addition & 1 deletion docs/api/features/grouping.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Grouping
title: Grouping APIs
id: grouping
---

Expand Down
2 changes: 1 addition & 1 deletion docs/api/features/pagination.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Pagination
title: Pagination APIs
id: pagination
---

Expand Down
2 changes: 1 addition & 1 deletion docs/api/features/pinning.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Pinning
title: Pinning APIs
id: pinning
---

Expand Down
2 changes: 1 addition & 1 deletion docs/api/features/row-selection.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Row Selection
title: Row Selection APIs
id: row-selection
---

Expand Down
2 changes: 1 addition & 1 deletion docs/api/features/sorting.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Sorting
title: Sorting APIs
id: sorting
---

Expand Down
2 changes: 1 addition & 1 deletion docs/guide/cells.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Cells
title: Cells Guide
---

## API
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/column-defs.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Columns
title: Columns Guide
---

## API
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/column-filtering.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Column Filtering
title: Column Filtering Guide
---

## Examples
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/column-ordering.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Column Ordering
title: Column Ordering Guide
---

## Examples
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/column-pinning.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Column Pinning
title: Column Pinning Guide
---

## Examples
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/column-sizing.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Column Sizing
title: Column Sizing Guide
---

## Examples
Expand Down
119 changes: 117 additions & 2 deletions docs/guide/column-visibility.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Column Visibility
title: Column Visibility Guide
---

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

- [column-visibility](../framework/react/examples/column-visibility)
- [column-ordering](../framework/react/examples/column-ordering)
- [sticky-column-pinning](../framework/react/examples/column-pinning-sticky)

### Other Examples

- [SolidJS column-visibility](../framework/solid/examples/column-visibility)
- [Svelte column-visibility](../framework/svelte/examples/column-visibility)

## API

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

## Column Visibility Guide
## Column Visibility Guide

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.

### Column Visibility State

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.

```jsx
const [columnVisibility, setColumnVisibility] = useState({
columnId1: true,
columnId2: false, //hide this column by default
columnId3: true,
});

const table = useReactTable({
//...
state: {
columnVisibility,
//...
},
onColumnVisibilityChange: setColumnVisibility,
});
```

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.

> **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.
```jsx
const table = useReactTable({
//...
initialState: {
columnVisibility: {
columnId1: true,
columnId2: false, //hide this column by default
columnId3: true,
},
//...
},
});
```

### Disable Hiding Columns

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.

```jsx
const columns = [
{
header: 'ID',
accessorKey: 'id',
enableHiding: false, // disable hiding for this column
},
{
header: 'Name',
accessor: 'name', // can be hidden
},
];
```

### Column Visibility Toggle APIs

There are several column API methods that are useful for rendering column visibility toggles in the UI.

- `column.getCanHide` - Useful for disabling the visibility toggle for a column that has `enableHiding` set to `false`.
- `column.getIsVisible` - Useful for setting the initial state of the visibility toggle.
- `column.toggleVisibility` - Useful for toggling the visibility of a column.
- `column.getToggleVisibilityHandler` - Shortcut for hooking up the `column.toggleVisibility` method to a UI event handler.

```jsx
{table.getAllColumns().map((column) => (
<label key={column.id}>
<input
checked={column.getIsVisible()}
disabled={!column.getCanHide()}
onChange={column.getToggleVisibilityHandler()}
type="checkbox"
/>
{column.columnDef.header}
</label>
))}
```

### Column Visibility Aware Table APIs

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`.

```jsx
<table>
<thead>
<tr>
{table.getVisibleLeafColumns().map((column) => ( // takes column visibility into account
//
))}
</tr>
</thead>
<tbody>
{table.getRowModel().rows.map((row) => (
<tr key={row.id}>
{row.getVisibleCells().map((cell) => ( // takes column visibility into account
//
))}
</tr>
))}
</tbody>
</table>
```

If you are using the Header Group APIs, they will already take column visibility into account.
2 changes: 1 addition & 1 deletion docs/guide/expanding.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Expanding
title: Expanding Guide
---

## Examples
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/faceted-values.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Faceted Values
title: Faceted Values Guide
---

## Examples
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/features.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Features
title: Features Guide
---

TanStack Table comes with many features, each with their own associated options and API:
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/filters.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Filters
title: Filters Guide
---

<!-- Deprecated -->
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/fuzzy-filtering.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Fuzzy Filtering
title: Fuzzy Filtering Guide
---

## Examples
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/global-filtering.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Global Filtering
title: Global Filtering Guide
---

## Examples
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/grouping.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Grouping
title: Grouping Guide
---

## Examples
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/headers.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Headers
title: Headers Guide
---

## API
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/migrating.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Migrating to V8
title: Migrating to V8 Guide
---

## Migrating to V8
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/pagination.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Pagination
title: Pagination Guide
---

## Examples
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/pinning.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Pinning
title: Pinning Guide
---

<!-- Deprecated -->
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/row-models.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Row Models
title: Row Models Guide
---

## Row Models Guide
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/row-pinning.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Row Pinning
title: Row Pinning Guide
---

## Examples
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/row-selection.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Row Selection
title: Row Selection Guide
---

## Examples
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/rows.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Rows
title: Rows Guide
---

## API
Expand Down
Loading

0 comments on commit e808abf

Please sign in to comment.