Want to skip to the implementation? Check out these examples:
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 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.
Here's an example of a custom fuzzy filter function:
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
}
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.
To use fuzzy filtering with global filtering, you can specify the fuzzy filter function in the globalFilterFn option of the table instance:
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.
})
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.
})
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:
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...
];
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.
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:
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
}
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:
{
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
}
{
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
}
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.