You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Column Faceting is a feature that allows you to generate lists of values for a given column from that column's data. For example, a list of unique values in a column can be generated from all rows in that column to be used as search suggestions in an autocomplete filter component. Or, a tuple of minimum and maximum values can be generated from a column of numbers to be used as a range for a range slider filter component.
18
+
19
+
### Column Faceting Row Models
20
+
21
+
In order to use any of the column faceting features, you must include the appropriate row models in your table options.
22
+
23
+
```ts
24
+
//only import the row models you need
25
+
import {
26
+
getCoreRowModel,
27
+
getFacetedRowModel,
28
+
getFacetedMinMaxValues, //depends on getFacetedRowModel
29
+
getFacetedUniqueValues, //depends on getFacetedRowModel
30
+
}
31
+
//...
32
+
consttable = useReactTable({
33
+
columns,
34
+
data,
35
+
getCoreRowModel: getCoreRowModel(),
36
+
getFacetedRowModel: getFacetedRowModel(), //if you need a list of values for a column (other faceted row models depend on this one)
37
+
getFacetedMinMaxValues: getFacetedMinMaxValues(), //if you need min/max values
38
+
getFacetedUniqueValues: getFacetedUniqueValues(), //if you need a list of unique values
39
+
//...
40
+
})
41
+
```
42
+
43
+
First, you must include the `getFacetedRowModel` row model. This row model will generate a list of values for a given column. If you need a list of unique values, include the `getFacetedUniqueValues` row model. If you need a tuple of minimum and maximum values, include the `getFacetedMinMaxValues` row model.
44
+
45
+
### Use Faceted Row Models
46
+
47
+
Once you have included the appropriate row models in your table options, you will be able to use the faceting column instance APIs to access the lists of values generated by the faceted row models.
If instead of using the built-in client-side faceting features, you can implement your own faceting logic on the server-side and pass the faceted values to the client-side. You can use the `getFacetedUniqueValues` and `getFacetedMinMaxValues` table options to resolve the faceted values from the server-side.
65
+
66
+
```ts
67
+
const facetingQuery =useQuery(
68
+
//...
69
+
)
70
+
71
+
const table =useReactTable({
72
+
columns,
73
+
data,
74
+
getCoreRowModel: getCoreRowModel(),
75
+
getFacetedRowModel: getFacetedRowModel(),
76
+
getFacetedUniqueValues: (table, columnId) => {
77
+
const uniqueValueMap =newMap<string, number>();
78
+
//...
79
+
returnuniqueValueMap;
80
+
},
81
+
getFacetedMinMaxValues: (table, columnId) => {
82
+
//...
83
+
return [min, max];
84
+
},
85
+
//...
86
+
})
87
+
```
88
+
89
+
Alternatively, you don't have to put any of your faceting logic through the TanStack Table APIs at all. Just fetch your lists and pass them to your filter components directly.
0 commit comments