Examples › Searching and filtering

Mantine DataTable doesn’t filter your data; to implement searching and filtering, adjust the array of records you’re passing to the component according to your own logic.

To help you build column-based filtering UIs, Mantine DataTable supports the following column properties:

  • filter
    The content of a popover that opens when the user clicks the filter button added to the column header. It can be a ReactNode or a function receiving an object with a close method (so you can close the popover programmatically, e.g. after resetting a filter) and returning a ReactNode.
  • filtering: boolean
    When true, the filter button is highlighted to indicate that a filter is in effect.
  • filterPopoverProps
    Additional Mantine Popover props to customize the filter popover (position, width, shadow, etc.).
  • filterPopoverDisableClickOutside: boolean
    When true, disables the click-outside handler Mantine DataTable attaches to the filter popover dropdown. You can combine it with filterPopoverProps={{ closeOnClickOutside: false }} if you want the popover to stay open until the user closes it explicitly.

Here’s a possible (rather naive and rough around the edges) implementation:

Name
Department name
Company
Birth date
Age
Jerald HowellIndustrialRunte IncMay 21 195076
Kathleen RueckerComputersShanahan, Robel and BeierDec 19 194382
Erica VolkmanToysGoyette IncJan 29 195571
Clifford OberbrunnerAutomotiveRau - O'HaraMar 06 197947
Alison KlingJeweleryGoyette IncJan 27 199729
Sue ZiemeBooksCummerata - KuhlmanSep 12 196066
Felicia GleasonShoesDoyle, Hodkiewicz and O'ConnellMar 09 197749
Alfredo ZemlakGamesRunte IncNov 12 198838
Emily BartolettiAutomotiveRau - O'HaraJan 05 196858
Delores ReynoldsIndustrialRunte IncJun 04 200422
Louis SchambergerToolsCummerata - KuhlmanSep 07 199432
Beverly HellerBeautyRunte IncNov 29 200124
Eugene FeestKidsGoyette IncFeb 20 195472
Martin BahringerBeautyRau - O'HaraMay 26 194581
Ellis MillerElectronicsConnelly, Feest and HodkiewiczMay 24 195076
Gloria ColeHomeDoyle, Hodkiewicz and O'ConnellDec 09 199629
Linda WittingBabyDoyle, Hodkiewicz and O'ConnellMay 16 196561
Gregg KutchMoviesShanahan, Robel and BeierNov 28 198639
Mamie RaynorGroceryCummerata - KuhlmanNov 05 199234
Erick BruenElectronicsGoyette IncSep 26 195274
Faith LangworthClothingRunte IncNov 27 198342
Alicia LeannonSportsFeest, Bogan and HerzogDec 27 200520
Boyd MohrJeweleryGoyette IncJun 18 195175
Lindsey HeidenreichGamesShanahan, Robel and BeierJul 13 199729
Elsa MarvinBooksTillman - JacobiOct 07 198244
Debbie HagenesClothingRunte IncJan 24 196858
Lionel McCulloughKidsGoldner, Rohan and LehnerFeb 23 198640
Kim LebsackJeweleryCummerata - KuhlmanJun 11 197056
Rolando WeissnatHomeRau - O'HaraApr 25 195175
Jacqueline LeschGamesRunte IncJul 22 197848
Felix StokesKidsGoyette IncMar 21 198442
Renee TillmanIndustrialGoldner, Rohan and LehnerNov 24 195373
Richard WatsicaOutdoorsRunte IncJun 10 197155
Nathan WolfGamesGoldner, Rohan and LehnerJul 02 199828
Jonathan Keebler-CronaMusicGoyette IncOct 22 200224
Kathleen SpinkaJeweleryGoldner, Rohan and LehnerFeb 28 194779
Bernice SchinnerGardenTillman - JacobiDec 23 200520
Adam PagacBooksDoyle, Hodkiewicz and O'ConnellDec 04 196164
Earl RyanBeautyRau - O'HaraJan 08 196165
Greg BaileyHomeRau - O'HaraJan 02 196759
Anne PowlowskiMusicConnelly, Feest and HodkiewiczJan 26 195769
Abraham DooleyJeweleryTillman - JacobiOct 05 195670
Myron LemkeShoesFeest, Bogan and HerzogSep 27 197848
Dianna Gislason-LeschGamesCummerata - KuhlmanMay 29 196660
Reginald HagenesIndustrialRunte IncMay 19 195076
Shelia TurcotteMusicGoyette IncJul 22 195967
Carlton JenkinsSportsRunte IncJun 07 194383
Lance WiegandGroceryCummerata - KuhlmanJul 12 196660
Ruby GrahamSportsFeest, Bogan and HerzogMay 30 197056
Hattie CollierOutdoorsGoyette IncApr 14 195472
Viola RathMoviesRunte IncMar 24 197056
Roland HuelJeweleryDoyle, Hodkiewicz and O'ConnellApr 20 197353
Leticia WiegandKidsGoyette IncJan 13 195868
Jacqueline KulasBeautyFeest, Bogan and HerzogAug 30 196066
Cristina JaskolskiSportsFeest, Bogan and HerzogJan 25 196660
Felipe DaughertyMusicConnelly, Feest and HodkiewiczAug 12 196957
Timothy HeaneyKidsDoyle, Hodkiewicz and O'ConnellDec 05 200520
Alonzo KutchSportsTillman - JacobiMay 02 198244
Keith KlingSportsGoyette IncFeb 16 196165
Janice GoyetteSportsRunte IncMar 14 196363
Helen Kunze-MacGyverJeweleryDoyle, Hodkiewicz and O'ConnellFeb 11 200224
Barry JastGroceryRunte IncMay 09 197848
Ramiro CummingsSportsGoyette IncApr 09 196660
Antonio Little-BahringerGamesShanahan, Robel and BeierNov 21 198838
Samuel ZemlakElectronicsGoyette IncApr 28 196462
Doris EmardGamesRunte IncMay 04 200323
Olivia AbernathyOutdoorsRunte IncApr 30 196957
Justin KohlerHealthGoyette IncJan 11 198046
Scott OberbrunnerSportsGoyette IncJan 22 199729
Yolanda SpinkaMusicConnelly, Feest and HodkiewiczJan 19 198145
Brad Ullrich-OrnGamesFeest, Bogan and HerzogAug 08 195274
Gloria FisherHealthGoyette IncNov 20 199531
Sergio CristBooksGoldner, Rohan and LehnerMay 31 199531
Theresa SporerSportsGoyette IncMay 15 200323
Theodore WiegandGamesCummerata - KuhlmanMay 18 200026
Rudy RoweGroceryTillman - JacobiOct 01 196264
Kurt RaynorToysGoyette IncAug 20 195769
Ruth MedhurstHomeDoyle, Hodkiewicz and O'ConnellJan 09 194779
Tim AbernathyShoesDoyle, Hodkiewicz and O'ConnellOct 23 198442
Rebecca RunolfsdottirGroceryRunte IncJul 07 196462
Angel AufderharHealthRunte IncJun 22 195967
Javier BergstromMoviesCummerata - KuhlmanOct 20 199333
Leigh KlockoMusicRau - O'HaraJun 14 195571
Taylor RiceBeautyGoldner, Rohan and LehnerSep 10 194878
Cindy GoldnerShoesDoyle, Hodkiewicz and O'ConnellNov 30 197847
Shannon CristOutdoorsConnelly, Feest and HodkiewiczNov 12 199432
Sarah MaggioBooksGoyette IncNov 09 200026
Carlton Langosh-GloverBabyRunte IncDec 06 196659
Felicia RoobGamesDoyle, Hodkiewicz and O'ConnellDec 21 196857
Simon KuhicKidsGoldner, Rohan and LehnerMay 07 195868
Sharon DanielMusicRunte IncJul 05 199927
Erin BayerBooksTillman - JacobiJul 04 198244
Erika Powlowski-CorwinSportsConnelly, Feest and HodkiewiczOct 10 194977
Vicky PollichBooksCummerata - KuhlmanMay 22 198640
Felicia ZiemannIndustrialRunte IncDec 15 199134
Colleen CrooksAutomotiveRau - O'HaraJun 06 199828
Jimmy FisherKidsGoyette IncDec 10 194580
Lula ReichertGamesCummerata - KuhlmanMar 17 199531
Bethany SchinnerMoviesCummerata - KuhlmanNov 12 195175
Allen HackettHealthTillman - JacobiMay 07 198442
No records

The code for this example is as follows:

'use client';

import { DataTable } from 'mantine-datatable';
import { ActionIcon, Button, Checkbox, MultiSelect, Stack, TextInput } from '@mantine/core';
import { DatePicker, type DatesRangeValue } from '@mantine/dates';
import { useDebouncedValue } from '@mantine/hooks';
import { IconSearch, IconX } from '@tabler/icons-react';
import dayjs from 'dayjs';
import { useEffect, useMemo, useState } from 'react';
import { employees } from '~/data';

const initialRecords = employees.slice(0, 100);

export function SearchingAndFilteringExample() {
  const [records, setRecords] = useState(initialRecords);

  const departments = useMemo(() => {
    const departments = new Set(employees.map((e) => e.department.name));
    return [...departments];
  }, []);

  const [query, setQuery] = useState('');
  const [selectedDepartments, setSelectedDepartments] = useState<string[]>([]);
  const [birthdaySearchRange, setBirthdaySearchRange] = useState<DatesRangeValue>();
  const [seniors, setSeniors] = useState(false);
  const [debouncedQuery] = useDebouncedValue(query, 200);

  useEffect(() => {
    setRecords(
      initialRecords.filter(({ firstName, lastName, department, birthDate }) => {
        if (
          debouncedQuery !== '' &&
          !`${firstName} ${lastName}`.toLowerCase().includes(debouncedQuery.trim().toLowerCase())
        )
          return false;

        if (
          birthdaySearchRange?.[0] &&
          birthdaySearchRange[1] &&
          (dayjs(birthdaySearchRange[0]).isAfter(birthDate, 'day') ||
            dayjs(birthdaySearchRange[1]).isBefore(birthDate, 'day'))
        )
          return false;

        if (selectedDepartments.length && !selectedDepartments.some((d) => d === department.name)) return false;

        if (seniors && dayjs().diff(birthDate, 'y') < 70) return false;

        return true;
      })
    );
  }, [debouncedQuery, birthdaySearchRange, selectedDepartments, seniors]);

  return (
    <DataTable
      height={300}
      withTableBorder
      withColumnBorders
      records={records}
      columns={[
        {
          accessor: 'name',
          render: ({ firstName, lastName }) => `${firstName} ${lastName}`,
          filter: (
            <TextInput
              label="Employees"
              description="Show employees whose names include the specified text"
              placeholder="Search employees..."
              leftSection={<IconSearch size={16} />}
              rightSection={
                <ActionIcon size="sm" variant="transparent" c="dimmed" onClick={() => setQuery('')}>
                  <IconX size={14} />
                </ActionIcon>
              }
              value={query}
              onChange={(e) => setQuery(e.currentTarget.value)}
            />
          ),
          filtering: query !== '',
        },
        {
          accessor: 'department.name',
          filter: (
            <MultiSelect
              label="Departments"
              description="Show all employees working at the selected departments"
              data={departments}
              value={selectedDepartments}
              placeholder="Search departments…"
              onChange={setSelectedDepartments}
              leftSection={<IconSearch size={16} />}
              comboboxProps={{ withinPortal: false }}
              clearable
              searchable
            />
          ),
          filtering: selectedDepartments.length > 0,
        },
        { accessor: 'department.company.name', title: 'Company' },
        {
          accessor: 'birthDate',
          textAlign: 'right',
          render: ({ birthDate }) => dayjs(birthDate).format('MMM DD YYYY'),
          filter: ({ close }) => (
            <Stack>
              <DatePicker
                maxDate={new Date()}
                type="range"
                value={birthdaySearchRange}
                onChange={setBirthdaySearchRange}
              />
              <Button
                disabled={!birthdaySearchRange}
                variant="light"
                onClick={() => {
                  setBirthdaySearchRange(undefined);
                  close();
                }}
              >
                Clear
              </Button>
            </Stack>
          ),
          filtering: Boolean(birthdaySearchRange),
        },
        {
          accessor: 'age',
          textAlign: 'right',
          render: ({ birthDate }) => dayjs().diff(birthDate, 'y'),
          filter: () => (
            <Checkbox
              label="Seniors"
              description="Show employees who are older than 70 years"
              checked={seniors}
              onChange={() => {
                setSeniors((current) => !current);
              }}
            />
          ),
          filtering: seniors,
        },
      ]}
    />
  );
}

Head over to the next example to discover more features.

Mantine DataTable is used and trusted by