跳转到内容

🎉 v5 is out! Head to the documentation to get started.

Data Grid - Filtering

Filtering helps to view a subset of the records based on a criteria.

Basic filter

Column filters can be set using the column menu and clicking the Filter menu item. Alternatively, if the grid has the toolbar displayed, you just need to click on the Filters button.

The filter applied to a column can be pre-configured using the filterModel prop:

<DataGrid
  filterModel={{
    items: [{ columnField: 'commodity', operatorValue: 'contains', value: 'rice' }],
  }}
/>
No rows
Desk
Commodity
Trader Name
Trader Email
Quantity

每页行数:

100

0-0 的 0

Predefined filters

A filter is composed of three parts: the column to filter, the value to look for, and an operator (e.g. contains, is before, is after, etc.). The operator determines if a candidate value should be considered as a result. The candidate value used by the operator is the one corresponding to the field attribute or the valueGetter of the GridColDef. As part of the predefined column types, a set of operators is available. You can find the supported column types in the columns section.

Note: The valueFormatter is only used for rendering purposes.

The following demo allows to explore the different operators available:

Name
Rating
Created on
Is admin?
Adelaide Hill
3
11/11/2024
true
Sarah Vargas
3
8/9/2024
true
Bernice Malone
2
2/20/2025
false
Mollie Bowers
2
10/7/2024
false
Katie Chavez
3
9/26/2024
true
Todd Chapman
4
6/10/2025
true
Johanna Coleman
4
2/14/2025
true
Lillie Lopez
3
11/17/2024
false
Miguel Swanson
5
5/20/2025
true
Rosetta Wade
2
3/20/2025
false

每页行数:

100

1-10 的 10

Disable filtering

Globally

Filters are enabled by default, but you can easily disable this feature by setting the disableColumnFilter prop.

<DataGrid disableColumnFilter />

Per column

You can disable the filter on a column by setting the filterable property of the GridColDef to false;

const columns = [{ field: 'image', filterable: false }];
Desk
Commodity
Trader Name
Trader Email
Quantity
D-6054
Rough Rice
Lucinda Clarke
52,763
D-4215
Corn
Ann Bell
39,703
D-4357
Rapeseed
Virgie Hudson
45,672
D-3818
Soybeans
Bess Henderson
13,381
D-182
Rough Rice
Jeremy Brooks
27,312
D-8222
Soybeans
Katherine Ray
77,413
D-9865
Coffee C
Nannie Allison
89,698
D-4781
Coffee C
Mamie Barber
87,912
D-3392
Corn
Zachary Flores
38,941
D-1901
Coffee C
Nora Castro
98,742

每页行数:

100

1-10 的 10

<DataGrid
  {...data}
  columns={data.columns.map((column) => ({
    ...column,
    filterable: false,
  }))}
/>

Customize the filters

The grid provides different ways to customize the filter panel. This section provides examples on how to make the most common modifications.

Change the input component

The value used by the operator to look for has to be entered by the user. On most column types, a text field is used. However, a custom component can be rendered instead.

In this demo, the Rating column reuses the numeric filter and the same rating component is used to the enter the value of the filter.

Avatar
Name
Website
Rating
Email
Phone
Username
City
Country
C
Carlos Luna
5
(942) 528-4731
@fi
Nosekreb
🇨🇩Congo, Democratic Republic of the
J
Jerry Rowe
4
(846) 580-7087
@ondemopi
Woivdo
🇵🇼Palau
S
Sylvia Colon
5
(245) 808-1950
@imakon
Vosdaklot
🇵🇫French Polynesia
L
Lilly Francis
4
(527) 578-6940
@ga
Onepurha
🇧🇳Brunei Darussalam
R
Raymond Zimmerman
5
(967) 775-6840
@uhoal
Lawudso
🇰🇼Kuwait
R
Rena Martin
4
(521) 749-6310
@poahurul
Gusedano
🇹🇯Tajikistan
W
Willie Walsh
4
(576) 952-4675
@zizozam
Uzimataz
🇵🇭Philippines
J
Jackson Blake
5
(768) 611-8238
@va
Otvukafo
🇲🇬Madagascar
T
Theresa Goodwin
4
(622) 221-1029
@reldis
Wawgolwum
🇧🇲Bermuda
H
Hilda Craig
4
(905) 343-9125
@fa
Kasfujef
🇮🇶Iraq

每页行数:

100

1-42 的 42

Extend filter operators

When defining a custom column type, the added operators are the same from the type that was extended.

In this demo, a price column type (used by Total is USD) is defined extending the number column type. Instead of adding all numeric operators, only the operators < and > are kept. Furthermore, the "$" prefix is added to the input component with the InputComponentProps prop.

Desk
Commodity
Total in USD
D-8800
Adzuki bean
$5,928,850.60
D-6826
Cotton No.2
$5,370,750.52
D-3484
Rapeseed
$4,545,003.56
D-8355
Soybeans
$3,501,955.01
D-1312
Adzuki bean
$6,166,624.95
D-1935
Sugar No.14
$6,230,887.50
D-2654
Soybeans
$3,868,613.45
D-299
Sugar No.14
$3,778,599.28
D-6344
Wheat
$6,358,983.01
D-6096
Frozen Concentrated Orange Juice
$4,686,683.91

每页行数:

100

1-46 的 46

Create a custom operator

If reusing the native filter operators is not enough, creating a custom operator is an option. A custom operator is defined creating a GridFilterOperator object. This object has to be added to the filterOperators attribute of the GridColDef.

The most important part of an operator is the getApplyFilterFn function. It's called with the GridFilterItem object and the GridColDef object. This function must return another function that is called on every value of the column. The returned function determines if the cell value satisfies the condition of the operator.

{
  label: 'From',
  value: 'from',
  getApplyFilterFn: (filterItem: GridFilterItem, column: GridColDef) => {
    if (!filterItem.columnField || !filterItem.value || !filterItem.operatorValue) {
      return null;
    }
    return (params: GridCellParams): boolean => {
      return Number(params.value) >= Number(filterItem.value);
    };
  },
  InputComponent: RatingInputValue,
  InputComponentProps: { type: 'number' },
}

Note: If the column has a valueGetter, then params.value will be the resolved value.

In this demo, you can see how to create a completely new operator for the Rating column.

No rows
Avatar
Name
Website
Rating
Email
Phone
Username
City
Country

每页行数:

100

0-0 的 0

Server-side filter

Filtering can be run server-side by setting the filterMode prop to server, and implementing the onFilterModelChange handler.

<DataGrid
  rows={rows}
  columns={columns}
  filterMode="server"
  onFilterModelChange={handleFilterModelChange}
  loading={loading}
/>

Below is a very simple demo on how you could achieve server-side filtering.

commodity
rice
soybeans
milk
wheat
oats

每页行数:

100

1-5 的 5

<DataGrid
  rows={rows}
  columns={columns}
  filterMode="server"
  onFilterModelChange={onFilterChange}
  loading={loading}
/>

Multi-column filtering

DataGridPro supports filtering by multiple columns. The default operator that will be applied between filters is an And.

No rows
Desk
Commodity
Trader Name
Trader Email
Quantity
<DataGridPro
  {...data}
  filterModel={filterModel}
  onFilterModelChange={(model) => setFilterModel(model)}
/>

To change the default operator, you should set the 'linkOperator' property of the filterModel like below.

const filterModel: GridFilterModel = {
  items: [
    { columnField: 'commodity', operatorValue: 'contains', value: 'rice' },
    { columnField: 'commodity', operatorValue: 'startsWith', value: 'Soy' },
  ],
  linkOperator: GridLinkOperator.Or,
};
No rows
Desk
Commodity
2
Trader Name
Trader Email
Quantity
<DataGridPro {...data} filterModel={filterModel} />

Quick filter

The grid does not natively include quick filtering. However, it can be implemented as in the demo below.

No rows
Desk
Commodity
Trader Name
Trader Email
Quantity

每页行数:

100

0-0 的 0

⚠️ This feature isn't natively implemented in the grid package. It's coming.

👍 Upvote issue #202 if you want to see it land faster.

apiRef

Signature:
applyFilter: (item: GridFilterItem, linkOperator?: GridLinkOperator) => void
Signature:
applyFilterLinkOperator: (operator: GridLinkOperator) => void
Signature:
applyFilters: () => void
Signature:
deleteFilter: (item: GridFilterItem) => void
Signature:
getVisibleRowModels: () => Map<GridRowId, GridRowData>
Signature:
hideFilterPanel: () => void
Signature:
setFilterModel: (model: GridFilterModel) => void
Signature:
showFilterPanel: (targetColumnField?: string) => void
Signature:
upsertFilter: (item: GridFilterItem) => void