Framework
Version
Menu
Getting Started
Core Guides
Feature Guides
Core APIs
Feature APIs
Enterprise
Examples

Global Faceting Guide

Examples

Want to skip to the implementation? Check out these examples:

API

Global Faceting API

Global Faceting Guide

Global Faceting allows you to generate lists of values for all columns from the table's data. For example, a list of unique values in a table can be generated from all rows in all columns to be used as search suggestions in an autocomplete filter component. Or, a tuple of minimum and maximum values can be generated from a table of numbers to be used as a range for a range slider filter component.

Global Faceting Row Models

In order to use any of the global faceting features, you must include the appropriate row models in your table options.

ts
//only import the row models you need
import {
getCoreRowModel,
getFacetedRowModel,
getFacetedMinMaxValues, //depends on getFacetedRowModel
getFacetedUniqueValues, //depends on getFacetedRowModel
} from '@tanstack/react-table'
//...
const table = useReactTable({
// other options...
getCoreRowModel: getCoreRowModel(),
getFacetedRowModel: getFacetedRowModel(), //Faceting model for client-side faceting (other faceting methods depend on this model)
getFacetedMinMaxValues: getFacetedMinMaxValues(), //if you need min/max values
getFacetedUniqueValues: getFacetedUniqueValues(), //if you need a list of unique values
//...
})

Use Global Faceted Row Models

Once you have included the appropriate row models in your table options, you will be able to use the faceting table instance APIs to access the lists of values generated by the faceted row models.

ts
// list of unique values for autocomplete filter
const autoCompleteSuggestions =
Array.from(table.getGlobalFacetedUniqueValues().keys())
.sort()
.slice(0, 5000);
ts
// tuple of min and max values for range filter
const [min, max] = table.getGlobalFacetedMinMaxValues() ?? [0, 1];

Custom Global (Server-Side) Faceting

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 getGlobalFacetedUniqueValues and getGlobalFacetedMinMaxValues table options to resolve the faceted values from the server-side.

ts
const facetingQuery = useQuery(
'faceting',
async () => {
const response = await fetch('/api/faceting');
return response.json();
},
{
onSuccess: (data) => {
table.getGlobalFacetedUniqueValues = () => data.uniqueValues;
table.getGlobalFacetedMinMaxValues = () => data.minMaxValues;
},
}
);

In this example, we use the useQuery hook from react-query to fetch faceting data from the server. Once the data is fetched, we set the getGlobalFacetedUniqueValues and getGlobalFacetedMinMaxValues table options to return the faceted values from the server response. This will allow the table to use the server-side faceting data for generating autocomplete suggestions and range filters.

Subscribe to Bytes

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.

Bytes

No spam. Unsubscribe at any time.

scarf analytics