Filtering

DataGrid column and global filtering

Overview

DataGrid manages the filter state but does not render filter inputs. Build the filter UI with @mantine/core components and connect it to the store:

interface DataGridStore<TData extends RowData = any> {
  /** Active column filters, each with an id and value. */
  columnFilters: ColumnFiltersState;

  /** Set one filter. Pass undefined, null or '' to remove it. */
  setColumnFilter: (columnId: string, value: unknown) => void;
  getColumnFilterValue: (columnId: string) => unknown;

  /** Replace all column filters, or update them from the previous state. */
  setColumnFilters: (updater: Updater<ColumnFiltersState>) => void;
  resetColumnFilters: () => void;

  /** Distinct column values. Requires withFaceting. */
  getFacetedUniqueValues: (columnId: string) => DataGridFacetedValue[];

  /** Numeric column range. Requires withFaceting. */
  getFacetedMinMaxValues: (columnId: string) => [number, number] | undefined;

  /** Global search value and setter. */
  globalFilter: any;
  setGlobalFilter: (value: any) => void;
}

Column filtering is enabled automatically when any column has enableColumnFilter: true or filterFn. Set withFiltering: true to enable it for all columns.

Rows are filtered with filterFn of the column. It is the name of a built-in TanStack Table filter function, for example includesString or inNumberRange, or your own function.

Global search

Pass withGlobalFilter to useDataGrid to enable search across all columns. DataGrid.GlobalFilter renders the search input, DataGrid.Toolbar without children includes it. To use your own input, connect it with store.getGlobalFilter() and store.setGlobalFilter():

First Name
Last Name
Email
Department
Salary
AliceJohnsonalice.johnson@example.comEngineering95000
BobSmithbob.smith@example.comMarketing72000
CarolWilliamscarol.williams@example.comSales68000
DavidBrowndavid.brown@example.comHR65000
EvaDaviseva.davis@example.comFinance88000
FrankMillerfrank.miller@example.comEngineering102000
GraceWilsongrace.wilson@example.comMarketing76000
HenryTaylorhenry.taylor@example.comSales71000
IreneAndersonirene.anderson@example.comEngineering97000
JamesThomasjames.thomas@example.comFinance91000
KarenJacksonkaren.jackson@example.comHR63000
LeoWhiteleo.white@example.comEngineering110000
MiaHarrismia.harris@example.comSales74000
NathanMartinnathan.martin@example.comMarketing69000
OliviaGarciaolivia.garcia@example.comFinance85000
PatrickMartinezpatrick.martinez@example.comEngineering99000
QuinnRobinsonquinn.robinson@example.comHR67000
RachelClarkrachel.clark@example.comSales73000
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { TextInput } from '@mantine/core';
import { employees } from './data';

function Demo() {
  const store = useDataGrid({
    data: employees,
    columns: [
      { accessorKey: 'firstName', header: 'First Name' },
      { accessorKey: 'lastName', header: 'Last Name' },
      { accessorKey: 'email', header: 'Email', size: 220 },
      { accessorKey: 'department', header: 'Department' },
      { accessorKey: 'salary', header: 'Salary' },
    ],
    withGlobalFilter: true,
  });

  return (
    <>
      <TextInput
        placeholder="Search..."
        value={store.getGlobalFilter() ?? ''}
        onChange={(event) => store.setGlobalFilter(event.currentTarget.value)}
        mb="md"
      />
      <DataGrid store={store} />
    </>
  );
}

Filters popover

A Filters button in the toolbar opens a popover with one control per filterable column. Controls read values with getColumnFilterValue and write them with setColumnFilter:

First Name
Last Name
Email
Department
Status
AliceJohnsonalice.johnson@example.comEngineeringactive
BobSmithbob.smith@example.comMarketingactive
CarolWilliamscarol.williams@example.comSalesactive
DavidBrowndavid.brown@example.comHRinactive
EvaDaviseva.davis@example.comFinanceactive
FrankMillerfrank.miller@example.comEngineeringactive
GraceWilsongrace.wilson@example.comMarketingactive
HenryTaylorhenry.taylor@example.comSalesinactive
IreneAndersonirene.anderson@example.comEngineeringactive
JamesThomasjames.thomas@example.comFinanceactive
KarenJacksonkaren.jackson@example.comHRactive
LeoWhiteleo.white@example.comEngineeringactive
MiaHarrismia.harris@example.comSalesactive
NathanMartinnathan.martin@example.comMarketinginactive
OliviaGarciaolivia.garcia@example.comFinanceactive
PatrickMartinezpatrick.martinez@example.comEngineeringactive
QuinnRobinsonquinn.robinson@example.comHRactive
RachelClarkrachel.clark@example.comSalesactive
import { Badge, Button, Group, Popover, Select, Stack, TextInput } from '@mantine/core';
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';

function Demo() {
  const store = useDataGrid({
    data: employees,
    columns: [
      { accessorKey: 'firstName', header: 'First Name', enableColumnFilter: true },
      { accessorKey: 'lastName', header: 'Last Name' },
      { accessorKey: 'email', header: 'Email', size: 220 },
      { accessorKey: 'department', header: 'Department', filterFn: 'equals' },
      { accessorKey: 'status', header: 'Status', filterFn: 'equals' },
    ],
    withFaceting: true,
  });

  const activeCount = store.getColumnFilters().length;

  return (
    <DataGrid store={store}>
      <DataGrid.Toolbar>
        <Popover position="bottom-start" shadow="md">
          <Popover.Target>
            <Button
              size={store.getControlSize()}
              variant="default"
              rightSection={activeCount > 0 ? <Badge size="xs" circle>{activeCount}</Badge> : null}
            >
              Filters
            </Button>
          </Popover.Target>
          <Popover.Dropdown>
            <Stack gap="sm" w={240}>
              <TextInput
                label="First name"
                placeholder="Contains"
                value={(store.getColumnFilterValue('firstName') as string) ?? ''}
                onChange={(event) => store.setColumnFilter('firstName', event.currentTarget.value)}
              />
              <Select
                label="Department"
                placeholder="Any"
                clearable
                data={store.getFacetedUniqueValues('department')}
                value={(store.getColumnFilterValue('department') as string) ?? null}
                onChange={(value) => store.setColumnFilter('department', value)}
              />
              <Select
                label="Status"
                placeholder="Any"
                clearable
                data={store.getFacetedUniqueValues('status')}
                value={(store.getColumnFilterValue('status') as string) ?? null}
                onChange={(value) => store.setColumnFilter('status', value)}
              />
              <Group justify="flex-end">
                <Button
                  size="xs"
                  variant="default"
                  disabled={activeCount === 0}
                  onClick={() => store.resetColumnFilters()}
                >
                  Clear all
                </Button>
              </Group>
            </Stack>
          </Popover.Dropdown>
        </Popover>
      </DataGrid.Toolbar>
    </DataGrid>
  );
}

Faceted filters

Pass withFaceting: true to useDataGrid to get unique values and numeric ranges of columns from the data:

  • getFacetedUniqueValues(columnId) returns { value, label }[] that can be passed as data to Select or MultiSelect
  • getFacetedMinMaxValues(columnId) returns [min, max] for RangeSlider or number inputs

Use the matching filterFn: equals for a single value, arrIncludesSome for multiple values and inNumberRange for a range:

Salary

First Name
Last Name
Department
Status
Salary
AliceJohnsonEngineeringactive95000
BobSmithMarketingactive72000
CarolWilliamsSalesactive68000
DavidBrownHRinactive65000
EvaDavisFinanceactive88000
FrankMillerEngineeringactive102000
GraceWilsonMarketingactive76000
HenryTaylorSalesinactive71000
IreneAndersonEngineeringactive97000
JamesThomasFinanceactive91000
KarenJacksonHRactive63000
LeoWhiteEngineeringactive110000
MiaHarrisSalesactive74000
NathanMartinMarketinginactive69000
OliviaGarciaFinanceactive85000
PatrickMartinezEngineeringactive99000
QuinnRobinsonHRactive67000
RachelClarkSalesactive73000
import { Button, Group, RangeSlider, Select, Text } from '@mantine/core';
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';

function Demo() {
  const store = useDataGrid({
    data: employees,
    columns: [
      { accessorKey: 'firstName', header: 'First Name' },
      { accessorKey: 'lastName', header: 'Last Name' },
      { accessorKey: 'department', header: 'Department', filterFn: 'equals' },
      { accessorKey: 'status', header: 'Status', filterFn: 'equals' },
      {
        accessorKey: 'salary',
        header: 'Salary',
        filterFn: 'inNumberRange',
        meta: { align: 'end' },
      },
    ],
    withFaceting: true,
  });

  const [min, max] = store.getFacetedMinMaxValues('salary') ?? [0, 0];
  const range = (store.getColumnFilterValue('salary') as [number, number] | undefined) ?? [min, max];

  return (
    <DataGrid store={store}>
      <DataGrid.Toolbar>
        <Select
          size={store.getControlSize()}
          placeholder="Department"
          clearable
          data={store.getFacetedUniqueValues('department')}
          value={(store.getColumnFilterValue('department') as string) ?? null}
          onChange={(value) => store.setColumnFilter('department', value)}
        />
        <Select
          size={store.getControlSize()}
          placeholder="Status"
          clearable
          data={store.getFacetedUniqueValues('status')}
          value={(store.getColumnFilterValue('status') as string) ?? null}
          onChange={(value) => store.setColumnFilter('status', value)}
        />
        <Group gap="xs" wrap="nowrap" style={{ flex: 1, minWidth: 220 }}>
          <Text size="xs" c="dimmed">
            Salary
          </Text>
          <RangeSlider
            size="sm"
            style={{ flex: 1 }}
            min={min}
            max={max}
            step={1000}
            minRange={1000}
            value={range}
            label={(value) => `$${value.toLocaleString()}`}
            onChange={(value) => store.setColumnFilter('salary', value)}
          />
        </Group>
        <Button
          size={store.getControlSize()}
          variant="default"
          disabled={store.getColumnFilters().length === 0}
          onClick={() => store.resetColumnFilters()}
        >
          Reset
        </Button>
      </DataGrid.Toolbar>
    </DataGrid>
  );
}

Operator filters

stringOperators, numberOperators and booleanOperators are sets of filter operators: contains, equals, starts with, is empty, greater than, etc. Set filterFn: createOperatorFilterFn(stringOperators) on the column and use an { operator, value } object as its filter value. To define your own operators, pass an array of FilterOperator objects to createOperatorFilterFn:

First Name
Last Name
Department
Salary
AliceJohnsonEngineering95000
BobSmithMarketing72000
CarolWilliamsSales68000
DavidBrownHR65000
EvaDavisFinance88000
FrankMillerEngineering102000
GraceWilsonMarketing76000
HenryTaylorSales71000
IreneAndersonEngineering97000
JamesThomasFinance91000
KarenJacksonHR63000
LeoWhiteEngineering110000
MiaHarrisSales74000
NathanMartinMarketing69000
OliviaGarciaFinance85000
PatrickMartinezEngineering99000
QuinnRobinsonHR67000
RachelClarkSales73000
import { useState } from 'react';
import { Group, NumberInput, Select, TextInput } from '@mantine/core';
import {
  createOperatorFilterFn,
  DataGrid,
  numberOperators,
  OperatorFilterValue,
  stringOperators,
  useDataGrid,
} from '@mantine/data-grid';
import { employees } from './data';

const stringOperatorOptions = stringOperators.map(({ value, label }) => ({ value, label }));
const numberOperatorOptions = numberOperators.map(({ value, label }) => ({ value, label }));

function Demo() {
  const [nameOperator, setNameOperator] = useState('contains');
  const [salaryOperator, setSalaryOperator] = useState('gte');

  const store = useDataGrid({
    data: employees,
    columns: [
      {
        accessorKey: 'firstName',
        header: 'First Name',
        filterFn: createOperatorFilterFn(stringOperators),
      },
      { accessorKey: 'lastName', header: 'Last Name' },
      { accessorKey: 'department', header: 'Department' },
      {
        accessorKey: 'salary',
        header: 'Salary',
        filterFn: createOperatorFilterFn(numberOperators),
        meta: { align: 'end' },
      },
    ],
  });

  const nameValue = (store.getColumnFilterValue('firstName') as OperatorFilterValue | undefined)
    ?.value;
  const salaryValue = (store.getColumnFilterValue('salary') as OperatorFilterValue | undefined)
    ?.value;
  const nameValueless = stringOperators.find((op) => op.value === nameOperator)?.valueless;

  const setNameFilter = (operator: string, value: unknown) => {
    const valueless = stringOperators.find((op) => op.value === operator)?.valueless;
    store.setColumnFilter('firstName', valueless || value ? { operator, value } : undefined);
  };

  const setSalaryFilter = (operator: string, value: unknown) => {
    store.setColumnFilter('salary', value === '' ? undefined : { operator, value });
  };

  return (
    <DataGrid store={store}>
      <DataGrid.Toolbar>
        <Group gap={4} wrap="nowrap">
          <Select
            size={store.getControlSize()}
            w={130}
            allowDeselect={false}
            data={stringOperatorOptions}
            value={nameOperator}
            onChange={(operator) => {
              if (operator) {
                setNameOperator(operator);
                setNameFilter(operator, nameValue);
              }
            }}
          />
          <TextInput
            size={store.getControlSize()}
            placeholder="First name"
            disabled={nameValueless}
            value={typeof nameValue === 'string' ? nameValue : ''}
            onChange={(event) => setNameFilter(nameOperator, event.currentTarget.value)}
          />
        </Group>
        <Group gap={4} wrap="nowrap">
          <Select
            size={store.getControlSize()}
            w={72}
            allowDeselect={false}
            data={numberOperatorOptions}
            value={salaryOperator}
            onChange={(operator) => {
              if (operator) {
                setSalaryOperator(operator);
                setSalaryFilter(operator, salaryValue ?? '');
              }
            }}
          />
          <NumberInput
            size={store.getControlSize()}
            placeholder="Salary"
            prefix="$"
            thousandSeparator
            value={typeof salaryValue === 'number' ? salaryValue : ''}
            onChange={(value) => setSalaryFilter(salaryOperator, value)}
          />
        </Group>
      </DataGrid.Toolbar>
    </DataGrid>
  );
}

Filter chips

Render store.getColumnFilters() as removable pills to show active filters. Use the getColumnLabel function exported from @mantine/data-grid to get the column label:

First Name
Last Name
Department
Status
Salary
AliceJohnsonEngineeringactive95000
BobSmithMarketingactive72000
CarolWilliamsSalesactive68000
DavidBrownHRinactive65000
EvaDavisFinanceactive88000
FrankMillerEngineeringactive102000
GraceWilsonMarketingactive76000
HenryTaylorSalesinactive71000
IreneAndersonEngineeringactive97000
JamesThomasFinanceactive91000
KarenJacksonHRactive63000
LeoWhiteEngineeringactive110000
MiaHarrisSalesactive74000
NathanMartinMarketinginactive69000
OliviaGarciaFinanceactive85000
PatrickMartinezEngineeringactive99000
QuinnRobinsonHRactive67000
RachelClarkSalesactive73000
import { Button, Pill, Popover, Select, Stack, TextInput } from '@mantine/core';
import { DataGrid, getColumnLabel, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';

function Demo() {
  const store = useDataGrid({
    data: employees,
    columns: [
      { accessorKey: 'firstName', header: 'First Name', enableColumnFilter: true },
      { accessorKey: 'lastName', header: 'Last Name' },
      { accessorKey: 'department', header: 'Department', filterFn: 'equals' },
      { accessorKey: 'status', header: 'Status', filterFn: 'equals' },
      { accessorKey: 'salary', header: 'Salary', meta: { align: 'end' } },
    ],
    withFaceting: true,
  });

  return (
    <DataGrid store={store}>
      <DataGrid.Toolbar>
        <Popover position="bottom-start" shadow="md">
          <Popover.Target>
            <Button size={store.getControlSize()} variant="default">
              Add filter
            </Button>
          </Popover.Target>
          <Popover.Dropdown>
            <Stack gap="sm" w={220}>
              <TextInput
                label="First name"
                placeholder="Contains"
                value={(store.getColumnFilterValue('firstName') as string) ?? ''}
                onChange={(event) => store.setColumnFilter('firstName', event.currentTarget.value)}
              />
              <Select
                label="Department"
                placeholder="Any"
                clearable
                data={store.getFacetedUniqueValues('department')}
                value={(store.getColumnFilterValue('department') as string) ?? null}
                onChange={(value) => store.setColumnFilter('department', value)}
              />
              <Select
                label="Status"
                placeholder="Any"
                clearable
                data={store.getFacetedUniqueValues('status')}
                value={(store.getColumnFilterValue('status') as string) ?? null}
                onChange={(value) => store.setColumnFilter('status', value)}
              />
            </Stack>
          </Popover.Dropdown>
        </Popover>
        {store.getColumnFilters().map((filter) => {
          const column = store.table.getColumn(filter.id);
          return column ? (
            <Pill
              key={filter.id}
              size={store.getControlSize()}
              withRemoveButton
              onRemove={() => store.setColumnFilter(filter.id, undefined)}
            >
              {getColumnLabel(column)}: {String(filter.value)}
            </Pill>
          ) : null;
        })}
        {store.getColumnFilters().length > 0 && (
          <Button size={store.getControlSize()} variant="default" onClick={() => store.resetColumnFilters()}>
            Clear all
          </Button>
        )}
      </DataGrid.Toolbar>
    </DataGrid>
  );
}

Controlled filters

Pass columnFilters and onColumnFiltersChange to useDataGrid to control filter state. The state is an array of { id, value } objects:

Active filters: []

First Name
Last Name
Department
Salary
Status
AliceJohnsonEngineering95000active
BobSmithMarketing72000active
CarolWilliamsSales68000active
DavidBrownHR65000inactive
EvaDavisFinance88000active
FrankMillerEngineering102000active
GraceWilsonMarketing76000active
HenryTaylorSales71000inactive
IreneAndersonEngineering97000active
JamesThomasFinance91000active
KarenJacksonHR63000active
LeoWhiteEngineering110000active
MiaHarrisSales74000active
NathanMartinMarketing69000inactive
OliviaGarciaFinance85000active
PatrickMartinezEngineering99000active
QuinnRobinsonHR67000active
RachelClarkSales73000active
import { useState } from 'react';
import { ColumnFiltersState } from '@tanstack/react-table';
import { Code, Text, TextInput } from '@mantine/core';
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';

function Demo() {
  const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([]);

  const store = useDataGrid({
    data: employees,
    columns: [
      { accessorKey: 'firstName', header: 'First Name', enableColumnFilter: true },
      { accessorKey: 'lastName', header: 'Last Name', enableColumnFilter: true },
      { accessorKey: 'department', header: 'Department' },
      { accessorKey: 'salary', header: 'Salary' },
      { accessorKey: 'status', header: 'Status' },
    ],
    columnFilters,
    onColumnFiltersChange: setColumnFilters,
  });

  return (
    <>
      <TextInput
        placeholder="Filter by first name"
        value={(store.getColumnFilterValue('firstName') as string) ?? ''}
        onChange={(event) => store.setColumnFilter('firstName', event.currentTarget.value)}
        mb="md"
      />
      <Text mb="md">
        Active filters: <Code>{JSON.stringify(columnFilters)}</Code>
      </Text>
      <DataGrid store={store} />
    </>
  );
}

Custom filter functions

Set filterFn on a column definition to a function that receives the row, column id and filter value and returns true if the row should be included:

First Name
Last Name
Department (exact match)
Salary
AliceJohnsonEngineering95000
BobSmithMarketing72000
CarolWilliamsSales68000
DavidBrownHR65000
EvaDavisFinance88000
FrankMillerEngineering102000
GraceWilsonMarketing76000
HenryTaylorSales71000
IreneAndersonEngineering97000
JamesThomasFinance91000
KarenJacksonHR63000
LeoWhiteEngineering110000
MiaHarrisSales74000
NathanMartinMarketing69000
OliviaGarciaFinance85000
PatrickMartinezEngineering99000
QuinnRobinsonHR67000
RachelClarkSales73000
import { TextInput } from '@mantine/core';
import { DataGrid, DataGridFilterFn, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';

const exactMatchFilter: DataGridFilterFn = (row, columnId, filterValue) => {
  if (!filterValue) {
    return true;
  }
  const cellValue = String(row.getValue(columnId));
  return cellValue === filterValue;
};

function Demo() {
  const store = useDataGrid({
    data: employees,
    columns: [
      { accessorKey: 'firstName', header: 'First Name' },
      { accessorKey: 'lastName', header: 'Last Name' },
      { accessorKey: 'department', header: 'Department (exact match)', filterFn: exactMatchFilter },
      { accessorKey: 'salary', header: 'Salary' },
    ],
  });

  return (
    <>
      <TextInput
        placeholder="Department, exact match (e.g. Engineering)"
        value={(store.getColumnFilterValue('department') as string) ?? ''}
        onChange={(event) => store.setColumnFilter('department', event.currentTarget.value)}
        mb="md"
      />
      <DataGrid store={store} />
    </>
  );
}

Disable filtering per column

Set enableColumnFilter: false on a column definition to disable filtering of that column:

const store = useDataGrid({
  data,
  withFiltering: true,
  columns: [
    { accessorKey: 'id', header: 'ID', enableColumnFilter: false },
    { accessorKey: 'name', header: 'Name' },
    { accessorKey: 'email', header: 'Email' },
  ],
});

Global filter column exclusion

Set enableGlobalFilter: false on a column definition to exclude the column from the global search:

First Name
Last Name
Email (excluded)
Department
Salary (excluded)
AliceJohnsonalice.johnson@example.comEngineering95000
BobSmithbob.smith@example.comMarketing72000
CarolWilliamscarol.williams@example.comSales68000
DavidBrowndavid.brown@example.comHR65000
EvaDaviseva.davis@example.comFinance88000
FrankMillerfrank.miller@example.comEngineering102000
GraceWilsongrace.wilson@example.comMarketing76000
HenryTaylorhenry.taylor@example.comSales71000
IreneAndersonirene.anderson@example.comEngineering97000
JamesThomasjames.thomas@example.comFinance91000
KarenJacksonkaren.jackson@example.comHR63000
LeoWhiteleo.white@example.comEngineering110000
MiaHarrismia.harris@example.comSales74000
NathanMartinnathan.martin@example.comMarketing69000
OliviaGarciaolivia.garcia@example.comFinance85000
PatrickMartinezpatrick.martinez@example.comEngineering99000
QuinnRobinsonquinn.robinson@example.comHR67000
RachelClarkrachel.clark@example.comSales73000
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { TextInput } from '@mantine/core';
import { employees } from './data';

function Demo() {
  const store = useDataGrid({
    data: employees,
    columns: [
      { accessorKey: 'firstName', header: 'First Name' },
      { accessorKey: 'lastName', header: 'Last Name' },
      {
        accessorKey: 'email',
        header: 'Email (excluded)',
        size: 220,
        enableGlobalFilter: false,
      },
      { accessorKey: 'department', header: 'Department' },
      {
        accessorKey: 'salary',
        header: 'Salary (excluded)',
        enableGlobalFilter: false,
      },
    ],
    withGlobalFilter: true,
  });

  return (
    <>
      <TextInput
        placeholder="Search..."
        value={store.getGlobalFilter() ?? ''}
        onChange={(event) => store.setGlobalFilter(event.currentTarget.value)}
        mb="md"
      />
      <DataGrid store={store} />
    </>
  );
}

Fuzzy search

Pass fuzzyFilter exported from @mantine/data-grid as globalFilterFn to useDataGrid for typo-tolerant global search. Use fuzzySort to sort rows by their match rank:

First Name
Last Name
Email
Department
Salary
AliceJohnsonalice.johnson@example.comEngineering95000
BobSmithbob.smith@example.comMarketing72000
CarolWilliamscarol.williams@example.comSales68000
DavidBrowndavid.brown@example.comHR65000
EvaDaviseva.davis@example.comFinance88000
FrankMillerfrank.miller@example.comEngineering102000
GraceWilsongrace.wilson@example.comMarketing76000
HenryTaylorhenry.taylor@example.comSales71000
IreneAndersonirene.anderson@example.comEngineering97000
JamesThomasjames.thomas@example.comFinance91000
KarenJacksonkaren.jackson@example.comHR63000
LeoWhiteleo.white@example.comEngineering110000
MiaHarrismia.harris@example.comSales74000
NathanMartinnathan.martin@example.comMarketing69000
OliviaGarciaolivia.garcia@example.comFinance85000
PatrickMartinezpatrick.martinez@example.comEngineering99000
QuinnRobinsonquinn.robinson@example.comHR67000
RachelClarkrachel.clark@example.comSales73000
import { DataGrid, fuzzyFilter, useDataGrid } from '@mantine/data-grid';
import { TextInput } from '@mantine/core';
import { employees } from './data';

function Demo() {
  const store = useDataGrid({
    data: employees,
    columns: [
      { accessorKey: 'firstName', header: 'First Name' },
      { accessorKey: 'lastName', header: 'Last Name' },
      { accessorKey: 'email', header: 'Email', size: 220 },
      { accessorKey: 'department', header: 'Department' },
      { accessorKey: 'salary', header: 'Salary' },
    ],
    withGlobalFilter: true,
    globalFilterFn: fuzzyFilter,
  });

  return (
    <>
      <TextInput
        placeholder="Search..."
        value={store.getGlobalFilter() ?? ''}
        onChange={(event) => store.setGlobalFilter(event.currentTarget.value)}
        mb="md"
      />
      <DataGrid store={store} />
    </>
  );
}

Debounced and server-side filtering

For server-side filtering, set manualFiltering: true, keep the filter state controlled and debounce it with useDebouncedValue before sending a request:

import { useState } from 'react';
import { ColumnFiltersState } from '@tanstack/react-table';
import { TextInput } from '@mantine/core';
import { useDebouncedValue } from '@mantine/hooks';
import { DataGrid, useDataGrid } from '@mantine/data-grid';

function Demo() {
  const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([]);
  const [debouncedFilters] = useDebouncedValue(columnFilters, 300);

  // Use debouncedFilters in your data fetching logic
  const data = useQuery(['employees', debouncedFilters], () =>
    fetchEmployees(debouncedFilters)
  );

  const store = useDataGrid({
    data: data ?? [],
    columns: [
      { accessorKey: 'firstName', header: 'First Name' },
      { accessorKey: 'lastName', header: 'Last Name' },
      { accessorKey: 'department', header: 'Department' },
    ],
    withFiltering: true,
    manualFiltering: true,
    columnFilters,
    onColumnFiltersChange: setColumnFilters,
  });

  return (
    <DataGrid store={store}>
      <DataGrid.Toolbar>
        <TextInput
          size={store.getControlSize()}
          placeholder="First name"
          value={(store.getColumnFilterValue('firstName') as string) ?? ''}
          onChange={(event) => store.setColumnFilter('firstName', event.currentTarget.value)}
        />
      </DataGrid.Toolbar>
    </DataGrid>
  );
}

See the Server-side page for a complete example.

Empty results

When all rows are filtered out, emptyState is displayed. Use isFiltered from its payload to offer a "Clear filters" action, see States.