Data Grid - Filtering
Easily filter your rows based on one or several criteria.
The filters can be modified through the data grid interface in several ways:
- By opening the column menu and clicking the Filter menu item.
- By clicking the Filters button in the data grid toolbar (if enabled).
Each column type has its own filter operators. The demo below lets you explore all the operators for each built-in column type.
See the dedicated section to learn how to create your own custom filter operator.
Single and multi-filters
Pass filters to the Data Grid
Structure of the model
The full typing details can be found on the GridFilterModel API page.
The filter model is composed of a list of items
and a logicOperator
:
The items
A filter item represents a filtering rule and is composed of several elements:
filterItem.field
: the field on which the rule applies.filterItem.value
: the value to look for.filterItem.operator
: name of the operator method to use (e.g. contains), matches thevalue
key of the operator object.filterItem.id
(): required when multiple filter items are used.
The logicOperator
The logicOperator
tells the data grid if a row should satisfy all (AND
) filter items or at least one (OR
) in order to be considered valid.
// Example 1: get rows with rating > 4 OR isAdmin = true
const filterModel: GridFilterModel = {
items: [
{ id: 1, field: 'rating', operator: '>', value: '4' },
{ id: 2, field: 'isAdmin', operator: 'is', value: 'true' },
],
logicOperator: GridLogicOperator.Or,
};
// Example 2: get rows with rating > 4 AND isAdmin = true
const filterModel: GridFilterModel = {
items: [
{ id: 1, field: 'rating', operator: '>', value: '4' },
{ id: 2, field: 'isAdmin', operator: 'is', value: 'true' },
],
logicOperator: GridLogicOperator.And,
};
If no logicOperator
is provided, the data grid will use GridLogicOperator.Or
by default.
Initialize the filters
To initialize the filters without controlling them, provide the model to the initialState
prop.
<DataGrid
initialState={{
filter: {
filterModel: {
items: [{ field: 'rating', operator: '>', value: '2.5' }],
},
},
}}
/>
Controlled filters
Use the filterModel
prop to control the filter applied on the rows.
You can use the onFilterModelChange
prop to listen to changes to the filters and update the prop accordingly.
<DataGrid
filterModel={{
items: [{ field: 'rating', operator: '>', value: '2.5' }],
}}
/>
Disable the filters
For all columns
Filters are enabled by default, but you can easily disable this feature by setting the disableColumnFilter
prop.
<DataGrid disableColumnFilter />
For some columns
To disable the filter of a single column, set the filterable
property in GridColDef
to false
.
In the example below, the rating column can not be filtered.
<DataGrid columns={[...columns, { field: 'rating', filterable: false }]} />
apiRef
The grid exposes a set of methods that enables all of these features using the imperative apiRef
. To know more about how to use it, check the API Object section.
deleteFilterItem()
Deletes a GridFilterItem.
Signature:
deleteFilterItem: (item: GridFilterItem) => void
hideFilterPanel()
Hides the filter panel.
Signature:
hideFilterPanel: () => void
setFilterLogicOperator()
Changes the GridLogicOperator used to connect the filters.
Signature:
setFilterLogicOperator: (operator: GridLogicOperator) => void
setFilterModel()
Sets the filter model to the one given by model
.
Signature:
setFilterModel: (model: GridFilterModel, reason?: GridControlledStateReasonLookup['filter']) => void
setQuickFilterValues()
Set the quick filter values ot the one given by values
Signature:
setQuickFilterValues: (values: any[]) => void
showFilterPanel()
Shows the filter panel. If targetColumnField
is given, a filter for this field is also added.
Signature:
showFilterPanel: (targetColumnField?: string, panelId?: string, labelId?: string) => void
upsertFilterItem()
Updates or inserts a GridFilterItem.
Signature:
upsertFilterItem: (item: GridFilterItem) => void
upsertFilterItems()
Updates or inserts many GridFilterItem.
Signature:
upsertFilterItems: (items: GridFilterItem[]) => void
gridExpandedRowCountSelector
Signature:
gridExpandedRowCountSelector: (apiRef: GridApiRef) => number
// or
gridExpandedRowCountSelector: (state: GridState, instanceId?: number) => number
Example
gridExpandedRowCountSelector(apiRef)
// or
gridExpandedRowCountSelector(state, apiRef.current.instanceId)
gridExpandedSortedRowEntriesSelector
Signature:
gridExpandedSortedRowEntriesSelector: (apiRef: GridApiRef) => { id: GridRowId; model: GridValidRowModel }[]
// or
gridExpandedSortedRowEntriesSelector: (state: GridState, instanceId?: number) => { id: GridRowId; model: GridValidRowModel }[]
Example
gridExpandedSortedRowEntriesSelector(apiRef)
// or
gridExpandedSortedRowEntriesSelector(state, apiRef.current.instanceId)
gridExpandedSortedRowIdsSelector
Signature:
gridExpandedSortedRowIdsSelector: (apiRef: GridApiRef) => GridRowId[]
// or
gridExpandedSortedRowIdsSelector: (state: GridState, instanceId?: number) => GridRowId[]
Example
gridExpandedSortedRowIdsSelector(apiRef)
// or
gridExpandedSortedRowIdsSelector(state, apiRef.current.instanceId)
gridFilterModelSelector
Signature:
gridFilterModelSelector: (apiRef: GridApiRef) => GridFilterModel
// or
gridFilterModelSelector: (state: GridState, instanceId?: number) => GridFilterModel
Example
gridFilterModelSelector(apiRef)
// or
gridFilterModelSelector(state, apiRef.current.instanceId)
gridFilteredSortedRowEntriesSelector
Signature:
gridFilteredSortedRowEntriesSelector: (apiRef: GridApiRef) => { id: GridRowId; model: GridValidRowModel }[]
// or
gridFilteredSortedRowEntriesSelector: (state: GridState, instanceId?: number) => { id: GridRowId; model: GridValidRowModel }[]
Example
gridFilteredSortedRowEntriesSelector(apiRef)
// or
gridFilteredSortedRowEntriesSelector(state, apiRef.current.instanceId)
gridFilteredSortedRowIdsSelector
Signature:
gridFilteredSortedRowIdsSelector: (apiRef: GridApiRef) => GridRowId[]
// or
gridFilteredSortedRowIdsSelector: (state: GridState, instanceId?: number) => GridRowId[]
Example
gridFilteredSortedRowIdsSelector(apiRef)
// or
gridFilteredSortedRowIdsSelector(state, apiRef.current.instanceId)
gridFilteredSortedTopLevelRowEntriesSelector
Signature:
gridFilteredSortedTopLevelRowEntriesSelector: (apiRef: GridApiRef) => { id: GridRowId; model: GridValidRowModel }[]
// or
gridFilteredSortedTopLevelRowEntriesSelector: (state: GridState, instanceId?: number) => { id: GridRowId; model: GridValidRowModel }[]
Example
gridFilteredSortedTopLevelRowEntriesSelector(apiRef)
// or
gridFilteredSortedTopLevelRowEntriesSelector(state, apiRef.current.instanceId)
gridFilteredTopLevelRowCountSelector
Signature:
gridFilteredTopLevelRowCountSelector: (apiRef: GridApiRef) => number
// or
gridFilteredTopLevelRowCountSelector: (state: GridState, instanceId?: number) => number
Example
gridFilteredTopLevelRowCountSelector(apiRef)
// or
gridFilteredTopLevelRowCountSelector(state, apiRef.current.instanceId)
gridQuickFilterValuesSelector
Signature:
gridQuickFilterValuesSelector: (apiRef: GridApiRef) => any[] | undefined
// or
gridQuickFilterValuesSelector: (state: GridState, instanceId?: number) => any[] | undefined
Example
gridQuickFilterValuesSelector(apiRef)
// or
gridQuickFilterValuesSelector(state, apiRef.current.instanceId)
More information about the selectors and how to use them on the dedicated page