Skip to content

Commit

Permalink
Merge branch 'main' into alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVandy committed Sep 14, 2024
2 parents 7f40d64 + 6b4d616 commit 3338718
Show file tree
Hide file tree
Showing 6 changed files with 694 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
"to": "guide/tables"
},
{
"label": "Rows Models",
"label": "Row Models",
"to": "guide/row-models"
},
{
Expand Down
215 changes: 214 additions & 1 deletion docs/guide/expanding.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,222 @@ Want to skip to the implementation? Check out these examples:

- [expanding](../../framework/react/examples/expanding)
- [grouping](../../framework/react/examples/grouping)
- [sub-components](../../framework/react/examples/sub-components)

## API

[Expanding API](../../api/features/expanding)

## Expanding Feature Guide
## Expanding Feature Guide

Expanding is a feature that allows you to show and hide additional rows of data related to a specific row. This can be useful in cases where you have hierarchical data and you want to allow users to drill down into the data from a higher level. Or it can be useful for showing additional information related to a row.

### Different use cases for Expanding Features

There are multiple use cases for expanding features in TanStack Table that will be discussed below.

1. Expanding sub-rows (child rows, aggregate rows, etc.)
2. Expanding custom UI (detail panels, sub-tables, etc.)

### Enable Client-Side Expanding

To use the client-side expanding features, you need to define the getExpandedRowModel function in your table options. This function is responsible for returning the expanded row model.

```ts
const table = useReactTable({
// other options...
getExpandedRowModel: getExpandedRowModel(),
})
```

Expanded data can either contain table rows or any other data you want to display. We will discuss how to handle both cases in this guide.

### Table rows as expanded data

Expanded rows are essentially child rows that inherit the same column structure as their parent rows. If your data object already includes these expanded rows data, you can utilize the `getSubRows` function to specify these child rows. However, if your data object does not contain the expanded rows data, they can be treated as custom expanded data, which is discussed in next section.

For example, if you have a data object like this:

```ts
type Person = {
id: number
name: string
age: number
children: Person[]
}

const data: Person = [
{ id: 1,
name: 'John',
age: 30,
children: [
{ id: 2, name: 'Jane', age: 5 },
{ id: 5, name: 'Jim', age: 10 }
]
},
{ id: 3,
name: 'Doe',
age: 40,
children: [
{ id: 4, name: 'Alice', age: 10 }
]
},
]
```

Then you can use the getSubRows function to return the children array in each row as expanded rows. The table instance will now understand where to look for the sub rows on each row.

```ts
const table = useReactTable({
// other options...
getSubRows: (row) => row.children, // return the children array as sub-rows
getCoreRowModel: getCoreRowModel(),
getExpandedRowModel: getExpandedRowModel(),
})
```

> **Note:** You can have a complicated `getSubRows` function, but keep in mind that it will run for every row and every sub-row. This can be expensive if the function is not optimized. Async functions are not supported.
### Custom Expanding UI

In some cases, you may wish to show extra details or information, which may or may not be part of your table data object, such as expanded data for rows. This kind of expanding row UI has gone by many names over the years including "expandable rows", "detail panels", "sub-components", etc.

By default, the `row.getCanExpand()` row instance API will return false unless it finds `subRows` on a row. This can be overridden by implementing your own `getRowCanExpand` function in the table instance options.

```ts
//...
const table = useReactTable({
// other options...
getRowCanExpand: (row) => true, // Add your logic to determine if a row can be expanded. True means all rows include expanded data
getCoreRowModel: getCoreRowModel(),
getExpandedRowModel: getExpandedRowModel(),
})
//...
<tbody>
{table.getRowModel().rows.map((row) => (
<React.Fragment key={row.id}>
{/* Normal row UI */}
<tr>
{row.getVisibleCells().map((cell) => (
<td key={cell.id}>
<FlexRender
render={cell.column.columnDef.cell}
props={cell.getContext()}
/>
</td>
))}
</tr>
{/* If the row is expanded, render the expanded UI as a separate row with a single cell that spans the width of the table */}
{row.getIsExpanded() && (
<tr>
<td colSpan={row.getAllCells().length}> // The number of columns you wish to span for the expanded data if it is not a row that shares the same columns as the parent row
// Your custom UI goes here
</td>
</tr>
)}
</React.Fragment>
))}
</tbody>
//...
```

### Expanded rows state

If you need to control the expanded state of the rows in your table, you can do so by using the expanded state and the `onExpandedChange` option. This allows you to manage the expanded state according to your requirements.

```ts
const [expanded, setExpanded] = useState<ExpandedState>({})

const table = useReactTable({
// other options...
state: {
expanded: expanded, // must pass expanded state back to the table
},
onExpandedChange: setExpanded
})
```

The ExpandedState type is defined as follows:

```ts
type ExpandedState = true | Record<string, boolean>
```
If the ExpandedState is true, it means all rows are expanded. If it's a record, only the rows with IDs present as keys in the record and have their value set to true are expanded. For example, if the expanded state is { row1: true, row2: false }, it means the row with ID row1 is expanded and the row with ID row2 is not expanded. This state is used by the table to determine which rows are expanded and should display their subRows, if any.
### UI toggling handler for expanded rows
TanStack table will not add a toggling handler UI for expanded data to your table. You should manually add it within each row's UI to allow users to expand and collapse the row. For example, you can add a button UI within the columns definition.
```ts
const columns = [
{
accessorKey: 'name',
header: 'Name',
},
{
accessorKey: 'age',
header: 'Age',
},
{
header: 'Children',
cell: ({ row }) => {
return row.getCanExpand() ?
<button
onClick={row.getToggleExpandedHandler()}
style={{ cursor: 'pointer' }}
>
{row.getIsExpanded() ? '👇' : '👉'}
</button>
: '';
},
},
]
```

### Filtering Expanded Rows

By default, the filtering process starts from the parent rows and moves downwards. This means if a parent row is excluded by the filter, all its child rows will also be excluded. However, you can change this behavior by using the `filterFromLeafRows` option. When this option is enabled, the filtering process starts from the leaf (child) rows and moves upwards. This ensures that a parent row will be included in the filtered results as long as at least one of its child or grandchild rows meets the filter criteria. Additionally, you can control how deep into the child hierarchy the filter process goes by using the `maxLeafRowFilterDepth` option. This option allows you to specify the maximum depth of child rows that the filter should consider.

```ts
//...
const table = useReactTable({
// other options...
getSubRows: row => row.subRows,
getCoreRowModel: getCoreRowModel(),
getFilteredRowModel: getFilteredRowModel(),
getExpandedRowModel: getExpandedRowModel(),
filterFromLeafRows: true, // search through the expanded rows
maxLeafRowFilterDepth: 1, // limit the depth of the expanded rows that are searched
})
```

### Paginating Expanded Rows

By default, expanded rows are paginated along with the rest of the table (which means expanded rows may span multiple pages). If you want to disable this behavior (which means expanded rows will always render on their parents page. This also means more rows will be rendered than the set page size) you can use the `paginateExpandedRows` option.

```ts
const table = useReactTable({
// other options...
paginateExpandedRows: false,
})
```

### Pinning Expanded Rows

Pinning expanded rows works the same way as pinning regular rows. You can pin expanded rows to the top or bottom of the table. Please refer to the [Pinning Guide](./pinning.md) for more information on row pinning.

### Sorting Expanded Rows

By default, expanded rows are sorted along with the rest of the table.

### Manual Expanding (server-side)

If you are doing server-side expansion, you can enable manual row expansion by setting the manualExpanding option to true. This means that the `getExpandedRowModel` will not be used to expand rows and you would be expected to perform the expansion in your own data model.

```ts
const table = useReactTable({
// other options...
manualExpanding: true,
})
```
108 changes: 108 additions & 0 deletions docs/guide/fuzzy-filtering.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,111 @@ Want to skip to the implementation? Check out these examples:

## Fuzzy Filtering Guide

Fuzzy filtering is a technique that allows you to filter data based on approximate matches. This can be useful when you want to search for data that is similar to a given value, rather than an exact match.

You can implement a client side fuzzy filtering by defining a custom filter function. This function should take in the row, columnId, and filter value, and return a boolean indicating whether the row should be included in the filtered data.

Fuzzy filtering is mostly used with global filtering, but you can also apply it to individual columns. We will discuss how to implement fuzzy filtering for both cases.

> **Note:** You will need to install the `@tanstack/match-sorter-utils` library to use fuzzy filtering.
> TanStack Match Sorter Utils is a fork of [match-sorter](https://github.com/kentcdodds/match-sorter) by Kent C. Dodds. It was forked in order to work better with TanStack Table's row by row filtering approach.
Using the match-sorter libraries is optional, but the TanStack Match Sorter Utils library provides a great way to both fuzzy filter and sort by the rank information it returns, so that rows can be sorted by their closest matches to the search query.

### Defining a Custom Fuzzy Filter Function

Here's an example of a custom fuzzy filter function:

```typescript
import { rankItem } from '@tanstack/match-sorter-utils';
import { FilterFn } from '@tanstack/table';

const fuzzyFilter: FilterFn<any> = (row, columnId, value, addMeta) => {
// Rank the item
const itemRank = rankItem(row.getValue(columnId), value)

// Store the itemRank info
addMeta({ itemRank })

// Return if the item should be filtered in/out
return itemRank.passed
}
```

In this function, we're using the rankItem function from the @tanstack/match-sorter-utils library to rank the item. We then store the ranking information in the meta data of the row, and return whether the item passed the ranking criteria.

### Using Fuzzy Filtering with Global Filtering

To use fuzzy filtering with global filtering, you can specify the fuzzy filter function in the globalFilterFn option of the table instance:

```typescript
const table = useReactTable({ // or your framework's equivalent function
columns,
data,
filterFns: {
fuzzy: fuzzyFilter, //define as a filter function that can be used in column definitions
},
globalFilterFn: 'fuzzy', //apply fuzzy filter to the global filter (most common use case for fuzzy filter)
getCoreRowModel: getCoreRowModel(),
getFilteredRowModel: getFilteredRowModel(), //client side filtering
getSortedRowModel: getSortedRowModel(), //client side sorting needed if you want to use sorting too.
})
```

### Using Fuzzy Filtering with Column Filtering

To use fuzzy filtering with column filtering, you should first define the fuzzy filter function in the filterFns option of the table instance. You can then specify the fuzzy filter function in the filterFn option of the column definition:

```typescript
const column = [
{
accessorFn: row => `${row.firstName} ${row.lastName}`,
id: 'fullName',
header: 'Full Name',
cell: info => info.getValue(),
filterFn: 'fuzzy', //using our custom fuzzy filter function
},
// other columns...
];
```

In this example, we're applying the fuzzy filter to a column that combines the firstName and lastName fields of the data.

#### Sorting with Fuzzy Filtering

When using fuzzy filtering with column filtering, you might also want to sort the data based on the ranking information. You can do this by defining a custom sorting function:

```typescript
import { compareItems } from '@tanstack/match-sorter-utils'
import { sortingFns } from '@tanstack/table'

const fuzzySort: SortingFn<any> = (rowA, rowB, columnId) => {
let dir = 0

// Only sort by rank if the column has ranking information
if (rowA.columnFiltersMeta[columnId]) {
dir = compareItems(
rowA.columnFiltersMeta[columnId]?.itemRank!,
rowB.columnFiltersMeta[columnId]?.itemRank!
)
}

// Provide an alphanumeric fallback for when the item ranks are equal
return dir === 0 ? sortingFns.alphanumeric(rowA, rowB, columnId) : dir
}
```

In this function, we're comparing the ranking information of the two rows. If the ranks are equal, we fall back to alphanumeric sorting.

You can then specify this sorting function in the sortFn option of the column definition:

```typescript
{
accessorFn: row => `${row.firstName} ${row.lastName}`,
id: 'fullName',
header: 'Full Name',
cell: info => info.getValue(),
filterFn: 'fuzzy', //using our custom fuzzy filter function
sortFn: 'fuzzySort', //using our custom fuzzy sort function
}
```
Loading

0 comments on commit 3338718

Please sign in to comment.