DocsGuides

Guides

Column Filtering Guide

How to use, customize, or disable filter features, client-side or server side, in Shadcn Table

Filtering is one of the most powerful features of Shadcn Table and is enabled by default. By default, column filters show as chips in the toolbar, each opening the column's filter input in an editor; see the Filter Chips Guide. There is a lot of flexibility and customization available here. Whether you want to customize the powerful client-side filtering already built in or implement your own server-side filtering, Shadcn Table has got you covered.

Example

Storybook

Relevant Table Options

Prop NameTypeDescription
columnFilterDisplayMode'chips' | 'custom' | 'popover' | 'subheader'Where column filters show. 'chips' (the default) puts one chip per filter in the toolbar; 'subheader' puts an input under each header; 'popover' opens the input from the header filter icon; 'custom' renders nothing so you can place the filters yourself.
columnFilterModeOptionsArray<ST_FilterOption | string> | nullSpecify which filter modes are available for every column. Optionally specify this per column as a column option.
enableColumnFilterModesbooleanShows the filter mode picker on every column filter. Set false to hide it; a column can turn it off with its own enableColumnFilterModes: false.
enableColumnFiltersboolean
enableFacetedValuesbooleanEnable or disable the calculation of faceted values. Facet values are a list of all unique values in a column. These are useful for advanced filtering components with selection, autocomplete, etc.
enableFilterMatchHighlightingbooleanEnable or disable highlighting text that matches the filter in the table cells.
enableFiltersbooleanEnable or disable both the global and column filters.
filterFnsRecord<string, FilterFn>This option allows you to define custom filter functions that can be referenced in a column's filterFn option, or the globalFilterFn option, by their key. TanStack Table core resolves the names: a filterFn or globalFilterFn string must be a built-in name or a key registered here, and an unknown name filters nothing.
filterFromLeafRowsboolean
manualFilteringbooleanDisables 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.
maxLeafRowFilterDepthnumberSet a maximum number leaf row depth that will be filtered. Preserve child rows of filtered rows by setting this to "0"
onColumnFilterFnsChangeOnChangeFn<{ [key: string]: ST_FilterOption }>Receives a function updater, as the TanStack Table on*Change handlers do. Resolve it with functionalUpdate, or pass a React state setter.
onColumnFiltersChangeOnChangeFn<ColumnFiltersState>If provided, this function will be called with an updaterFn when state.columnFilters 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.
onShowColumnFiltersChangeOnChangeFn<boolean>Receives a function updater, as the TanStack Table on*Change handlers do. Resolve it with functionalUpdate, or pass a React state setter.
renderColumnFilterModeMenuItems({ column, internalFilterOptions, onSelectFilterMode, table }) => ReactNode[]
stFilterAutocompletePropsAutocompleteProps | ({ column, rangeFilterIndex, table }) => AutocompleteProps
stFilterCheckboxPropsCheckboxProps | ({ column, table}) => CheckboxProps
stFilterDatePickerPropsDatePickerProps | ({ column, rangeFilterIndex, table }) => DatePickerProps
stFilterDateTimePickerPropsDateTimePickerProps | ({ column, rangeFilterIndex, table }) => DateTimePickerProps
stFilterSliderPropsSliderProps | ({ column, table}) => SliderProps
stFilterTextFieldPropsTextFieldProps | ({ table, column, rangeFilterIndex}) => TextFieldProps
stFilterTimePickerPropsTimePickerProps | ({ column, rangeFilterIndex, table }) => TimePickerProps

Relevant Column Options

Column OptionTypeDescription
Filter({ column, header, table }) => ReactNodeDefine a custom filter component in a column.
columnFilterModeOptionsArray<string>
enableColumnFilterbooleanEnable or disable column filtering for this column. Filter will not be shown if disabled.
enableColumnFilterModesbooleanFilter modes are on by default; set false to hide the filter mode picker for this column.
enableFilterMatchHighlightingbooleanEnable or disable filter match highlighting for this column.
filterFnST_FilterFnA built-in filter name, a name registered through the filterFns table option, or a filter function. TanStack Table core resolves the name, and an unknown name filters nothing. After the table prepares its columns, columnDef.filterFn holds the current filter mode name; read it with getColumnFilterFnName(columnDef).
filterSelectOptionsArray<string | { label: string; value: string }>
filterVariant'text' | 'select' | 'multi-select' | 'range' | 'range-slider' | 'checkbox' | 'autocomplete' | 'date' | 'date-range' | 'datetime' | 'datetime-range' | 'time' | 'time-range'Specify whether the filter should be a text input or a select input, or other type of pre-built input.
renderColumnFilterModeMenuItems
stFilterAutocompletePropsAutocompleteProps | ({ column, rangeFilterIndex, table }) => AutocompleteProps
stFilterCheckboxPropsCheckboxProps | ({ column, table }) => CheckboxProps
stFilterDatePickerPropsDatePickerProps | ({ column, rangeFilterIndex, table }) => DatePickerProps
stFilterDateTimePickerPropsDateTimePickerProps | ({ column, rangeFilterIndex, table }) => DateTimePickerProps
stFilterSliderPropsSliderProps | ({ column, table}) => SliderProps
stFilterTextFieldPropsTextFieldProps | ({ column, rangeFilterIndex, table }) => TextFieldProps
stFilterTimePickerPropsTimePickerProps | ({ column, rangeFilterIndex, table }) => TimePickerProps

Relevant State Options

State OptionTypeDescription
columnFilterFns{ [key: string]: ST_FilterFn }
columnFiltersArray<{id: string, value: unknown}>an array of objects with id and value properties, representing the current filter values for each column
filterChips{ columnIds: string[]; openAnchor: 'chip' | 'header' | 'toolbar' | null; openColumnId: string | null }The filter chips shown in the toolbar, in the order they were added, and the column whose filter editor is open with where it is anchored. A chip exists before it has a value.
showColumnFiltersboolean

Disable Filtering Features

Various subsets of filtering features can be disabled. If you want to disable filtering completely, you can set the enableColumnFilters table option to false to remove all filters from each column. Alternatively, enableColumnFilter can be set to false for individual columns.

enableFilters can be set to false to disable both column filters and the global search filter.

Filter Variants

Shadcn Table has several built-in filter variants for advanced filtering. These can be specified on a per-column basis using the filterVariant option. The following variants are available:

  • 'text' - shows the default text field
  • 'autocomplete' - shows an autocomplete text field with the options from faceted values or specified in filterSelectOptions array.
  • 'select' - shows a select dropdown with the options from faceted values or specified in filterSelectOptions array.
  • 'multi-select' - shows a select dropdown with the options from faceted values or specified in filterSelectOptions and allows multiple selections with checkboxes
  • 'range' - shows min and max text fields for filtering a range of values
  • 'range-slider' - shows a slider for filtering a range of values
  • 'date' - shows a date picker for filtering by date values
  • 'datetime' - shows a date and time picker for filtering by date and time values
  • 'date-range' - shows one date range picker, its min and max ends in one popup, for filtering by date ranges
  • 'datetime-range' - shows one date and time range picker, its min and max ends in one popup, for filtering by date and time ranges
  • 'time' - shows a time picker for filtering by time values
  • 'time-range' - shows one time range picker with a min and a max time box, for filtering by time ranges
  • 'checkbox' - shows a checkbox for filtering by 'true' or 'false' values

The 'date', 'datetime' and 'time' variants compare values as dates: by local day, to the minute, or by time of day, also when the accessor returns a Date.

The date and time variants render DatePicker from shadcn-date-picker, which installs with the table: a box to type in, with a calendar or a clock in a popup. A picked value applies at once; a typed value applies on Enter or when focus leaves the box. The filter value stays a string: YYYY-MM-DD, YYYY-MM-DDTHH:mm or HH:mm.

stFilterDatePickerProps, stFilterDateTimePickerProps and stFilterTimePickerProps pass these on to the picker: min, max, step (date and time filters, and time filters), disabled, id, className, style, aria-* and data-* attributes, and the onFocus, onBlur and onKeyDown handlers. The other input props have no effect.

Storybook

Faceted Values for Filter Variants

Faceted values are a list of unique values for a column that gets generated under the hood from table data when the enableFacetedValues table option is set to true. These values can be used to populate the select dropdowns for the 'select' and 'multi-select' filter variants, or the min and max values for the 'range-slider' variant. This means that you no longer need to manually specify the filterSelectOptions table option for these variants manually, especially if you are using client-side filtering.

Storybook

Custom Faceted Values

If you are using server-side pagination and filtering, you can still customize the faceted values output with the getFacetedUniqueValues and getFacetedMinMaxValues props.

const table = useShadcnTable({
  columns,
  data,
  enableFacetedValues: true,
  //if using server-side pagination and filtering
  getFacetedMinMaxValues: (table) => {
    //fetch min and max values from server
    return [minValue, maxValue];
  },
  //if using server-side filtering
  getFacetedUniqueValues: (table) => {
    const uniqueValueMap = new Map<string, number>();
    //fetch unique values from server, ideally including the count of each unique value
    return uniqueValueMap;
  },
})

Column Filter Display Modes

By default (columnFilterDisplayMode: 'chips'), filters live as chips in the toolbar: "+ Filter" or the toolbar filter button picks a column, the column's filter input opens in an editor, and a chip shows the column, the filter mode and the value. The Filter Chips Guide covers the chips, the column picker and the editor. The filter variants below render the same inputs in every mode; the examples on this page set columnFilterDisplayMode: 'subheader' so their inputs show under the headers.

To show the filter inputs below the column headers instead, set columnFilterDisplayMode to 'subheader'. The toolbar then shows a filter toggle in place of the filter button, and showColumnFilters shows or hides the inputs.

const table = useShadcnTable({
  columns,
  data,
  columnFilterDisplayMode: 'subheader', //filter inputs below the headers
  initialState: { showColumnFilters: true }, //show them on load
});

You can switch to a more "excel-like" UI by setting the columnFilterDisplayMode table option to 'popover'. This will show a filter icon in the column header that can be clicked to open a popover with the filter input.

const table = useShadcnTable({
  columns,
  data,
  columnFilterDisplayMode: 'popover', //filter inputs will show in a popover (like excel)
});

Alternatively, if you want to render your own column filter UI in a separate sidebar, but still want to use the built-in filtering functionality, you can set the columnFilterDisplayMode table option to 'custom'.

const table = useShadcnTable({
  columns,
  data,
  columnFilterDisplayMode: 'custom', //render your own column filter UI (e.g. in a sidebar)
});
Storybook

Custom Filter Functions

You can specify either a pre-built filterFn that comes with Shadcn Table or pass in your own custom filter functions.

Custom Filter Functions Per Column

By default, Shadcn Table uses a fuzzy filtering algorithm based on the popular match-sorter library from Kent C. Dodds for text columns, while 'date', 'datetime' and 'time' columns default to equals. However, Shadcn Table also comes with numerous other filter functions that you can specify per column in the filterFn column options.

Pre-built ST Filter Functions

Pre-built filter functions from Shadcn Table include between, betweenInclusive, contains, empty, endsWith, equals, fuzzy, greaterThan, greaterThanOrEqualTo, lessThan, lessThanOrEqualTo, notEmpty, notEquals, and startsWith. View these algorithms here

Pre-built TanStack Table Filter Functions

Pre-built filter functions from TanStack Table include includesString, includesStringSensitive, equalsString, equalsStringSensitive, arrIncludes, arrIncludesAll, arrIncludesSome, weakEquals, and inNumberRange. View more information about these algorithms in the TanStack Table Filter docs.

You can specify either a pre-built filter function, from Shadcn Table or TanStack Table, or you can even specify your own custom filter function in the filterFn column option.

const columns = [
  {
    accessorKey: 'firstName',
    header: 'First Name',
    // using a prebuilt filter function from Shadcn Table
    filterFn: 'startsWith',
  },
  {
    accessorKey: 'middleName',
    header: 'Middle Name',
    // using a prebuilt filter function from TanStack Table
    filterFn: 'includesStringSensitive',
  },
  {
    accessorKey: 'lastName',
    header: 'Last Name',
    // custom filter function
    filterFn: (row, id, filterValue) =>
      row.getValue(id).startsWith(filterValue),
  },
];

If you provide a custom filter function, it must have the following signature:

(row: Row<TData>, id: string, filterValue: string | number) => boolean;

This function will be used to filter 1 row at a time and should return a boolean indicating whether or not that row passes the filter.

Add Custom Filter Functions

You can add custom filter functions to the filterFns table option. These will be available to all columns to use. The filterFn table option on a column will override any filter function with the same name in the filterFns table option.

const columns = [
  {
    accessorKey: 'name',
    header: 'Name',
    filterFn: 'customFilterFn',
  },
];

const table = useShadcnTable({
  columns,
  data,
  filterFns: {
    customFilterFn: (row, id, filterValue) => {
      return row.original.customField === filterValue;
    },
  },
});

Filter Modes

Enable Column Filter Modes (Filter Switching)

Filter modes are on by default: the user switches between the column's filter modes from a drop-down menu. The filter icon in the filter text field opens the menu in the subheader and popover display modes. In the default chips mode, the mode picker sits on the filter editor's top line instead ("Name contains"), see the Filter Chips Guide. Text, autocomplete, select, date, datetime and time filters offer every mode, range filters offer between and between inclusive, and a multi-select filter adds Includes any, Includes all and Excludes. Set enableColumnFilterModes: false on the table, or on a column, to hide the picker.

const table = useShadcnTable({
  columns,
  data,
  enableColumnFilterModes: false, //no filter mode picker on any column
});

Customize Filter Modes

You can narrow down the available filter mode options by setting the columnFilterModeOptions table option or a column specific columnFilterModeOptions option.

const columns = [
  {
    accessorKey: 'firstName',
    header: 'First Name',
    columnFilterModeOptions: ['fuzzy', 'contains', 'startsWith'],
  },
  {
    accessorKey: 'age',
    header: 'Age',
    columnFilterModeOptions: ['between', 'lessThan', 'greaterThan'],
  },
];

Render Custom Filter Mode Menu

You can also render custom menu items in the filter mode drop-down menu by setting the renderColumnFilterModeMenuItems table option or column option. This option is a function that takes in the column and returns an array of MenuItem components. This is useful if you want to add custom filter modes that are not included in Shadcn Table, or if you just want to render the menu in your own custom way

const columns = [
  {
    accessorKey: 'firstName',
    header: 'First Name',
    renderColumnFilterModeMenuItems: ({ column, onSelectFilterMode }) => [
      <MenuItem
        key="startsWith"
        onClick={() => onSelectFilterMode('startsWith')}
      >
        Start With
      </MenuItem>,
      <MenuItem
        key="endsWith"
        onClick={() => onSelectFilterMode('yourCustomFilterFn')}
      >
        Your Custom Filter Fn
      </MenuItem>,
    ],
  },
];

const table = useShadcnTable({
  columns,
  data,
  // renderColumnFilterModeMenuItems could go here if you want to apply to all columns
});

return <ShadcnTable table={table} />;
Storybook

Expanded Leaf Row Filtering Options

If you are using the filtering features along-side either the grouping or expanding features, then there are a few behaviors and customizations you should be aware of.

Check out the Expanded Leaf Row Filtering Behavior docs to learn more about the filterFromLeafRows and maxLeafRowFilterDepth props.

Manual Server-Side Column 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 columnFilters state yourself like so (can work in conjunction with manual global filtering).

// You can manage and have control over the columnFilters state yourself
const [columnFilters, setColumnFilters] = 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]);
  };
}, [columnFilters]);

const table = useShadcnTable({
  columns,
  data, // this will already be filtered on the server
  manualFiltering: true, //turn off client-side filtering
  onColumnFiltersChange: setColumnFilters, //hoist internal columnFilters state to your state
  state: { columnFilters }, //pass in your own managed columnFilters state
});

return <ShadcnTable table={table} />;

Specifying manualFiltering turns off all client-side filtering and assumes that the data you pass to <ShadcnTable /> is already filtered.

See either the React Query or useEffect fetching examples to see fully server-side filtering, pagination, and sorting in action.

Customize shadcn/ui Filter components

You can customize the shadcn/ui filter components by setting the stFilterTextFieldProps table option or column option.

You can also turn a filter textfield into a select dropdown by setting the filterSelectOptions table option or column option.

Storybook

Custom Filter Components

If you need custom filter components that are much more complex than text-boxes and dropdowns, you can create and pass in your own filter components using the Filter column option.

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 filter query with a shade of the theme.palette.warning.main color.

Filter Match Highlighting will only work on columns with the default text filter variant. Also, 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() in order 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

This feature can be disabled by setting the enableFilterMatchHighlighting table option to false.

const table = useShadcnTable({
  columns,
  data,
  enableFilterMatchHighlighting: false,
});

View Extra Storybook Examples

On this page