Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DataGrid] Allow to filter non-filterable columns programmatically #11538

Merged
merged 12 commits into from
Jan 13, 2024
46 changes: 46 additions & 0 deletions docs/data/data-grid/filtering/ReadOnlyFilters.js
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the MIT Grid allows one filter at a max, I have allowed clearing the read-only filter by adding a filter on another column. Users can prevent this by controlling the filter and not updating the filterModel on filterModelChange.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as React from 'react';
import { DataGrid, GridToolbar } from '@mui/x-data-grid';
import { useDemoData } from '@mui/x-data-grid-generator';

const VISIBLE_FIELDS = ['name', 'rating', 'country', 'dateCreated', 'isAdmin'];

export default function ReadOnlyFilters() {
const { data } = useDemoData({
dataSet: 'Employee',
visibleFields: VISIBLE_FIELDS,
rowLength: 100,
});

const columns = React.useMemo(
() =>
data.columns.map((column) => ({
...column,
filterable: column.field !== 'name',
})),
[data.columns],
);

const [filterModel, setFilterModel] = React.useState({
items: [
{
field: 'name',
operator: 'contains',
value: 'a',
},
],
});

return (
<div style={{ height: 400, width: '100%' }}>
<DataGrid
{...data}
columns={columns}
slots={{
toolbar: GridToolbar,
}}
filterModel={filterModel}
onFilterModelChange={(newFilterModel) => setFilterModel(newFilterModel)}
/>
</div>
);
}
46 changes: 46 additions & 0 deletions docs/data/data-grid/filtering/ReadOnlyFilters.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as React from 'react';
import { DataGrid, GridFilterModel, GridToolbar } from '@mui/x-data-grid';
import { useDemoData } from '@mui/x-data-grid-generator';

const VISIBLE_FIELDS = ['name', 'rating', 'country', 'dateCreated', 'isAdmin'];

export default function ReadOnlyFilters() {
const { data } = useDemoData({
dataSet: 'Employee',
visibleFields: VISIBLE_FIELDS,
rowLength: 100,
});

const columns = React.useMemo(
() =>
data.columns.map((column) => ({
...column,
filterable: column.field !== 'name',
})),
[data.columns],
);

const [filterModel, setFilterModel] = React.useState<GridFilterModel>({
items: [
{
field: 'name',
operator: 'contains',
value: 'a',
},
],
});

return (
<div style={{ height: 400, width: '100%' }}>
<DataGrid
{...data}
columns={columns}
slots={{
toolbar: GridToolbar,
}}
filterModel={filterModel}
onFilterModelChange={(newFilterModel) => setFilterModel(newFilterModel)}
/>
</div>
);
}
9 changes: 9 additions & 0 deletions docs/data/data-grid/filtering/ReadOnlyFilters.tsx.preview
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<DataGrid
{...data}
columns={columns}
slots={{
toolbar: GridToolbar,
}}
filterModel={filterModel}
onFilterModelChange={(newFilterModel) => setFilterModel(newFilterModel)}
/>
20 changes: 20 additions & 0 deletions docs/data/data-grid/filtering/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,26 @@ In the example below, the _rating_ column can not be filtered.

{{"demo": "DisableFilteringGridSomeColumns.js", "bg": "inline", "defaultCodeOpen": false}}

### Filter non-filterable columns programmatically
Copy link
Member Author

@MBilalShafi MBilalShafi Jan 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update in context of #11512 (comment)
Sidenote: I've kept internal variable naming to have the keyword readOnly for better understanding. The alternative disabled may also convey not visible at all, so to me readOnly is the more readable naming choice (on a dx perspective).
Let me know if there's a disagreement.


You can initialize the `filterModel`, set the `filterModel` prop, or use the API method `apiRef.current.setFilterModel` to set the filters for columns with non-filterable columns. These filters will be applied but will be read-only on the UI and the user won't be able to change them.
MBilalShafi marked this conversation as resolved.
Show resolved Hide resolved

```jsx
const columns = [
{ field: 'name', filterable: false },
...otherColumns,
]

<DataGrid
filterModel={{
items: [{ field: 'name', operator: 'contains', value: 'a' }],
}}
columns={columns}
/>
```

{{"demo": "ReadOnlyFilters.js", "bg": "inline", "defaultCodeOpen": false}}

## Ignore diacritics (accents)

You can ignore diacritics (accents) when filtering the rows. See [Quick filter - Ignore diacritics (accents)](/x/react-data-grid/filtering/quick-filter/#ignore-diacritics-accents).
Expand Down
4 changes: 1 addition & 3 deletions docs/pages/x/api/data-grid/grid-filter-form.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@
"type": { "name": "arrayOf", "description": "Array&lt;'and'<br>&#124;&nbsp;'or'&gt;" },
"default": "[GridLogicOperator.And, GridLogicOperator.Or]"
},
"multiFilterOperator": {
"type": { "name": "enum", "description": "'and'<br>&#124;&nbsp;'or'" }
},
"operatorInputProps": { "type": { "name": "any" }, "default": "{}" },
"readOnly": { "type": { "name": "bool" }, "default": "false" },
"showMultiFilterOperators": { "type": { "name": "bool" } },
"valueInputProps": { "type": { "name": "any" }, "default": "{}" }
},
Expand Down
48 changes: 24 additions & 24 deletions docs/pages/x/api/date-pickers/date-range-picker.json
Original file line number Diff line number Diff line change
Expand Up @@ -209,30 +209,6 @@
"default": "ArrowDropDown",
"class": null
},
{
"name": "desktopPaper",
"description": "Custom component for the paper rendered inside the desktop picker's Popper.",
"default": "PickersPopperPaper",
"class": null
},
{
"name": "desktopTransition",
"description": "Custom component for the desktop popper [Transition](https://mui.com/material-ui/transitions/).",
"default": "Grow or Fade from '@mui/material' when `reduceAnimations` is `true`.",
"class": null
},
{
"name": "desktopTrapFocus",
"description": "Custom component for trapping the focus inside the views on desktop.",
"default": "FocusTrap from '@mui/base'.",
"class": null
},
{
"name": "popper",
"description": "Custom component for the popper inside which the views are rendered on desktop.",
"default": "Popper from '@mui/material'.",
"class": null
},
{
"name": "actionBar",
"description": "Custom component for the action bar, it is placed below the picker views.",
Expand Down Expand Up @@ -278,6 +254,30 @@
"default": "IconButton",
"class": null
},
{
"name": "desktopPaper",
"description": "Custom component for the paper rendered inside the desktop picker's Popper.",
"default": "PickersPopperPaper",
"class": null
},
{
"name": "desktopTransition",
"description": "Custom component for the desktop popper [Transition](https://mui.com/material-ui/transitions/).",
"default": "Grow or Fade from '@mui/material' when `reduceAnimations` is `true`.",
"class": null
},
{
"name": "desktopTrapFocus",
"description": "Custom component for trapping the focus inside the views on desktop.",
"default": "FocusTrap from '@mui/base'.",
"class": null
},
{
"name": "popper",
"description": "Custom component for the popper inside which the views are rendered on desktop.",
"default": "Popper from '@mui/material'.",
"class": null
},
{ "name": "field", "description": "", "class": null },
{
"name": "dialog",
Expand Down
48 changes: 24 additions & 24 deletions docs/pages/x/api/date-pickers/desktop-date-range-picker.json
Original file line number Diff line number Diff line change
Expand Up @@ -205,30 +205,6 @@
"default": "ArrowDropDown",
"class": null
},
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexfauquette This seems to be a side-effect of #11303, some items are reordered for no reason after yarn docs:api. Any idea why this happens?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@michaldudak Should we sort them in a specific order?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO slots order should reflect the source. Usually we want to have the root slot at the top. Or we could sort them alphabetically but place 'root' at the top.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way it can be a configuration parameter: sortingStrategy: { porps, classes, slots } with either the sort callback or null if no sorting

"name": "desktopPaper",
"description": "Custom component for the paper rendered inside the desktop picker's Popper.",
"default": "PickersPopperPaper",
"class": null
},
{
"name": "desktopTransition",
"description": "Custom component for the desktop popper [Transition](https://mui.com/material-ui/transitions/).",
"default": "Grow or Fade from '@mui/material' when `reduceAnimations` is `true`.",
"class": null
},
{
"name": "desktopTrapFocus",
"description": "Custom component for trapping the focus inside the views on desktop.",
"default": "FocusTrap from '@mui/base'.",
"class": null
},
{
"name": "popper",
"description": "Custom component for the popper inside which the views are rendered on desktop.",
"default": "Popper from '@mui/material'.",
"class": null
},
{
"name": "actionBar",
"description": "Custom component for the action bar, it is placed below the picker views.",
Expand Down Expand Up @@ -274,6 +250,30 @@
"default": "IconButton",
"class": null
},
{
"name": "desktopPaper",
"description": "Custom component for the paper rendered inside the desktop picker's Popper.",
"default": "PickersPopperPaper",
"class": null
},
{
"name": "desktopTransition",
"description": "Custom component for the desktop popper [Transition](https://mui.com/material-ui/transitions/).",
"default": "Grow or Fade from '@mui/material' when `reduceAnimations` is `true`.",
"class": null
},
{
"name": "desktopTrapFocus",
"description": "Custom component for trapping the focus inside the views on desktop.",
"default": "FocusTrap from '@mui/base'.",
"class": null
},
{
"name": "popper",
"description": "Custom component for the popper inside which the views are rendered on desktop.",
"default": "Popper from '@mui/material'.",
"class": null
},
{ "name": "field", "description": "", "class": null }
],
"classes": [],
Expand Down
24 changes: 12 additions & 12 deletions docs/pages/x/api/date-pickers/mobile-date-range-picker.json
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,18 @@
"default": "PickersModalDialogRoot",
"class": null
},
{
"name": "mobilePaper",
"description": "Custom component for the paper rendered inside the mobile picker's Dialog.",
"default": "Paper from '@mui/material'.",
"class": null
},
{
"name": "mobileTransition",
"description": "Custom component for the mobile dialog [Transition](https://mui.com/material-ui/transitions/).",
"default": "Fade from '@mui/material'.",
"class": null
},
{
"name": "actionBar",
"description": "Custom component for the action bar, it is placed below the picker views.",
Expand Down Expand Up @@ -256,18 +268,6 @@
"default": "IconButton",
"class": null
},
{
"name": "mobilePaper",
"description": "Custom component for the paper rendered inside the mobile picker's Dialog.",
"default": "Paper from '@mui/material'.",
"class": null
},
{
"name": "mobileTransition",
"description": "Custom component for the mobile dialog [Transition](https://mui.com/material-ui/transitions/).",
"default": "Fade from '@mui/material'.",
"class": null
},
{ "name": "field", "description": "", "class": null }
],
"classes": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@
"description": "Props passed to the logic operator input component."
},
"logicOperators": { "description": "Sets the available logic operators." },
"multiFilterOperator": { "description": "The current logic operator applied." },
"operatorInputProps": { "description": "Props passed to the operator input component." },
"readOnly": {
"description": "<code>true</code> if the filter is disabled/read only. i.e. <code>colDef.fiterable = false</code> but passed in <code>filterModel</code>"
},
"showMultiFilterOperators": {
"description": "If <code>true</code>, the logic operator field is visible."
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,6 @@
"rightArrowIcon": "Icon displayed in the right view switch button.",
"switchViewButton": "Button displayed to switch between different calendar views.",
"switchViewIcon": "Icon displayed in the SwitchViewButton. Rotated by 180° when the open view is 'year'.",
"desktopPaper": "Custom component for the paper rendered inside the desktop picker's Popper.",
"desktopTransition": "Custom component for the desktop popper [Transition](https://mui.com/material-ui/transitions/).",
"desktopTrapFocus": "Custom component for trapping the focus inside the views on desktop.",
"popper": "Custom component for the popper inside which the views are rendered on desktop.",
"actionBar": "Custom component for the action bar, it is placed below the picker views.",
"shortcuts": "Custom component for the shortcuts.",
"layout": "Custom component for wrapping the layout.\nIt wraps the toolbar, views, action bar, and shortcuts.",
Expand All @@ -173,6 +169,10 @@
"textField": "Form control with an input to render a date or time inside the default field.\nIt is rendered twice: once for the start element and once for the end element.\nReceives the same props as `@mui/material/TextField`.",
"clearIcon": "Icon to display inside the clear button.",
"clearButton": "Button to clear the value.",
"desktopPaper": "Custom component for the paper rendered inside the desktop picker's Popper.",
"desktopTransition": "Custom component for the desktop popper [Transition](https://mui.com/material-ui/transitions/).",
"desktopTrapFocus": "Custom component for trapping the focus inside the views on desktop.",
"popper": "Custom component for the popper inside which the views are rendered on desktop.",
"field": "",
"dialog": "Custom component for the dialog inside which the views are rendered on mobile.",
"mobilePaper": "Custom component for the paper rendered inside the mobile picker's Dialog.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,6 @@
"rightArrowIcon": "Icon displayed in the right view switch button.",
"switchViewButton": "Button displayed to switch between different calendar views.",
"switchViewIcon": "Icon displayed in the SwitchViewButton. Rotated by 180° when the open view is 'year'.",
"desktopPaper": "Custom component for the paper rendered inside the desktop picker's Popper.",
"desktopTransition": "Custom component for the desktop popper [Transition](https://mui.com/material-ui/transitions/).",
"desktopTrapFocus": "Custom component for trapping the focus inside the views on desktop.",
"popper": "Custom component for the popper inside which the views are rendered on desktop.",
"actionBar": "Custom component for the action bar, it is placed below the picker views.",
"shortcuts": "Custom component for the shortcuts.",
"layout": "Custom component for wrapping the layout.\nIt wraps the toolbar, views, action bar, and shortcuts.",
Expand All @@ -170,6 +166,10 @@
"textField": "Form control with an input to render a date or time inside the default field.\nIt is rendered twice: once for the start element and once for the end element.\nReceives the same props as `@mui/material/TextField`.",
"clearIcon": "Icon to display inside the clear button.",
"clearButton": "Button to clear the value.",
"desktopPaper": "Custom component for the paper rendered inside the desktop picker's Popper.",
"desktopTransition": "Custom component for the desktop popper [Transition](https://mui.com/material-ui/transitions/).",
"desktopTrapFocus": "Custom component for trapping the focus inside the views on desktop.",
"popper": "Custom component for the popper inside which the views are rendered on desktop.",
"field": ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@
"switchViewButton": "Button displayed to switch between different calendar views.",
"switchViewIcon": "Icon displayed in the SwitchViewButton. Rotated by 180° when the open view is 'year'.",
"dialog": "Custom component for the dialog inside which the views are rendered on mobile.",
"mobilePaper": "Custom component for the paper rendered inside the mobile picker's Dialog.",
"mobileTransition": "Custom component for the mobile dialog [Transition](https://mui.com/material-ui/transitions/).",
"actionBar": "Custom component for the action bar, it is placed below the picker views.",
"shortcuts": "Custom component for the shortcuts.",
"layout": "Custom component for wrapping the layout.\nIt wraps the toolbar, views, action bar, and shortcuts.",
Expand All @@ -167,8 +169,6 @@
"textField": "Form control with an input to render a date or time inside the default field.\nIt is rendered twice: once for the start element and once for the end element.\nReceives the same props as `@mui/material/TextField`.",
"clearIcon": "Icon to display inside the clear button.",
"clearButton": "Button to clear the value.",
"mobilePaper": "Custom component for the paper rendered inside the mobile picker's Dialog.",
"mobileTransition": "Custom component for the mobile dialog [Transition](https://mui.com/material-ui/transitions/).",
"field": ""
}
}
Loading