Guides
Global Filtering/Search Guide
How to use, customize, or disable the global filter and search features in Shadcn Table
Shadcn Table has a powerful built-in global filtering (search) feature that uses a fuzzy matching algorithm and ranks/sorts the results based on how closely rows match the search query. In this guide, we'll cover how to use, customize, or disable the global filter and search features to fit your needs.
Example
Relevant Table Options
| Prop Name | Type | Description |
|---|---|---|
| enableFilterMatchHighlighting | boolean | Enable or disable highlighting text that matches the filter in the table cells. |
| enableGlobalFilter | boolean | |
| enableGlobalFilterModes | boolean | |
| enableGlobalFilterRankedResults | boolean | |
| getColumnCanGlobalFilter | (column: Column<TData, unknown>) => boolean | |
| globalFilterFn | ST_FilterOption | The filter function to use for global filtering. |
| globalFilterModeOptions | Array<ST_FilterOption | string> | null | |
| manualFiltering | boolean | Disables the getFilteredRowModel from being used to filter data. This may be useful if your table needs to dynamically support both client-side and server-side filtering. |
| onGlobalFilterChange | OnChangeFn<GlobalFilterState> | If provided, this function will be called with an updaterFn when state.globalFilter changes. This overrides the default internal state management, so you will need to persist the state change either fully or partially outside of the table. |
| onGlobalFilterFnChange | OnChangeFn<GlobalFilterState> | Receives a function updater, as the TanStack Table on*Change handlers do. Resolve it with functionalUpdate, or pass a React state setter. |
| onShowGlobalFilterChange | OnChangeFn<boolean> | Receives a function updater, as the TanStack Table on*Change handlers do. Resolve it with functionalUpdate, or pass a React state setter. |
| positionGlobalFilter | 'left' | 'right' | |
| renderGlobalFilterModeMenuItems | ({ internalFilterOptions, onSelectFilterMode, table }) => ReactNode[] | |
| showClearSearchButton | boolean | Renders the inline clear button inside the global search field. The search toggle already clears and closes the field, so turn this on when the field shows without the toggle. |
| stSearchTextFieldProps | TextFieldProps | ({ table }) => TextFieldProps |
Relevant Column Options
| Column Option | Type | Description |
|---|---|---|
| enableFilterMatchHighlighting | boolean | Enable or disable filter match highlighting for this column. |
| enableGlobalFilter | boolean |
Relevant State Options
| State Option | Type | Description |
|---|---|---|
| globalFilter | any | |
| globalFilterFn | ST_FilterFn | |
| showGlobalFilter | boolean |
Disable Global Filtering
You can either disable the global filter feature entirely or disable it for specific columns.
Disable Global Filtering per Column
If you simply want to not include a column as one of the columns that the global filter scans through during filtering, you can set the enableGlobalFilter option to false for that column.
const columns = [
{
accessorKey: 'id',
header: 'Id',
enableGlobalFilter: false, // do not scan this column during global filtering
},
{
accessorKey: 'name',
header: 'Name',
},
];Disable Global Filter Feature
You can disable the global filtering feature and hide the search icon by setting the enableGlobalFilter table option to false.
const table = useShadcnTable({
columns,
data,
enableGlobalFilter: false, //disable search feature
});
return <ShadcnTable table={table} />;Client-Side Global Filtering
Client-side filtering (and global filtering) is enabled by default. This means that the search box will scan through all columns and try to find matches for the search term.
Global Filter Function
You can use any of the built-in filterFns or any of the custom filter functions that you have defined in the filterFns prop, just like you would with the column filters.
const table = useShadcnTable({
columns,
data,
globalFilterFn: 'contains', //turn off fuzzy matching and use simple contains filter function
});Or a custom filter function:
const table = useShadcnTable({
columns,
data,
filterFns: {
myCustomFilterFn: (row, id, filterValue) =>
row.getValue(id).startsWith(filterValue),
},
globalFilterFn: 'myCustomFilterFn', //set the global filter function to myCustomFilterFn
});The default global filter function is set to fuzzy, which is a filtering algorithm based on the popular match-sorter library from Kent C. Dodds, though you can change the global filter function by setting the globalFilterFn table option.
Ranked Results
If you keep the default fuzzy filterFn option as the global filter function, you get an extra ranked results feature enabled by default. This means that when a user searches with the search box, the results will be sorted by the closest match first instead of the order the data was defined in.
The ranked results feature will disable itself automatically if a sort direction is applied to a column, if any sub-rows are expanded, or if any of the manual props are set to true.
If you do not want ranked results to be enabled at all, but you still want fuzzy matching, you can set the enableGlobalFilterRankedResults table option to false.
const table = useShadcnTable({
columns,
data,
enableGlobalFilterRankedResults: false, //preserve the order of the data when fuzzy match searching
});Global Filter Modes
Similar to the column filter modes, you can enable the user to be able to choose between multiple different filter modes for the global filter with the enableGlobalFilterModes table option. You can then customize which filter modes are available in the drop-down by setting the globalFilterModeOptions table option or by rendering your own custom menu items with the renderGlobalFilterModeMenuItems table option.
const table = useShadcnTable({
columns,
data,
enableGlobalFilterModes: true, //enable the user to choose between multiple search filter modes
globalFilterModeOptions: ['fuzzy', 'startsWith'], //only allow the user to choose between fuzzy and startsWith filter modes
});Search Toggle and Clear Control
The search toggle is the first button of the toolbar group. Closed, it shows a magnifier, and a click opens the search field to its left and focuses it. Open, it shows a crossed-out magnifier, and one click clears the search text and closes the field. The toggle is never disabled. Pressing Escape in the field clears the text first, then closes the empty field.
The field itself renders no inline clear button by default, because the toggle already clears it. If you show the search field without the toggle, set showClearSearchButton: true to bring the inline X back. Both use the clearSearch localization key.
const table = useShadcnTable({
columns,
data,
initialState: { showGlobalFilter: true }, //the field is always shown, so there is no toggle
showClearSearchButton: true, //render the inline clear button inside the field
});Show Search Field by Default
Additionally, if you want to show the search text box by default and not hide it behind the search icon, you can set the showGlobalFilter state to true in the initialState. The toggle button is then not rendered.
Manual Server-Side Global Filtering
A very common use case when you have a lot of data is to filter the data on the server, instead of client-side. In this case, you will want to set the manualFiltering table option to true and manage the globalFilter state yourself like in the example below (can work in conjunction with manual column filtering).
// You can manage and have control over the columnFilters state yourself
const [globalFilter, setGlobalFilter] = useState('');
const [data, setData] = useState([]); //data will get updated after re-fetching
useEffect(() => {
const fetchData = async () => {
// send api requests when columnFilters state changes
const filteredData = await fetch();
setData([...filteredData]);
};
}, [globalFilter]);
const table = useShadcnTable({
columns,
data, // this will already be filtered on the server
manualFiltering: true, //turn off client-side filtering
onGlobalFilterChange: setGlobalFilter, //hoist internal global state to your state
state: { globalFilter }, //pass in your own managed globalFilter state
});
return <ShadcnTable table={table} />;Specifying
manualFilteringturns off all client-side filtering and assumes that thedatayou pass to<ShadcnTable />is already filtered.
Here is the full Remote Data example showing off server-side filtering, pagination, and sorting.
Customize Global Filter Position
You can customize the position of the global filter (search box) in the top toolbar by setting the positionGlobalFilter table option to left or right. It is shown on the right by default, just left of the toolbar button group.
const table = useShadcnTable({
columns,
data,
positionGlobalFilter: 'left', //show the global filter on the left side of the top toolbar
initialState: {
showGlobalFilter: true, //show the global filter by default
},
});Customize the Search Text Field
You can customize the search text field by passing in props to the stSearchTextFieldProps table option. This is useful if you want to customize the placeholder text, add styles, or any other text field props.
const table = useShadcnTable({
columns,
data,
stSearchTextFieldProps: {
placeholder: 'Search all users',
style: { minWidth: '300px' },
},
});
return <ShadcnTable table={table} />;Filter Match Highlighting
Filter match highlighting is a new featured enabled by default that will highlight text in the table body cells that matches the current search query with a shade of the theme.palette.warning.main color.
If you are using a custom Cell render override for a column, you will need to use the renderedCellValue table option instead of cell.getValue() to preserve the filter match highlighting.
const columns = [
{
accessorKey: 'name',
header: 'Name',
Cell: ({ renderedCellValue }) => <span>{renderedCellValue}</span>, // use renderedCellValue instead of cell.getValue()
},
];Disable Filter Match Highlighting
Filter match highlighting can be disabled by setting the enableFilterMatchHighlighting table option to false.
const table = useShadcnTable({
columns,
data,
enableFilterMatchHighlighting: false, //disable filter match highlighting
});View Extra Storybook Examples