-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Conversation
Deploy preview: https://deploy-preview-11538--material-ui-x.netlify.app/ Updated pages: |
There was a problem hiding this comment.
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
.
@@ -232,6 +238,7 @@ const GridFilterForm = React.forwardRef<HTMLDivElement, GridFilterFormProps>( | |||
const classes = useUtilityClasses(rootProps); | |||
const valueRef = React.useRef<any>(null); | |||
const filterSelectorRef = React.useRef<HTMLInputElement>(null); | |||
const multiFilterOperator = filterModel.logicOperator ?? GridLogicOperator.And; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Solves https://codesandbox.io/p/sandbox/zealous-shamir-jktxkz?file=%2Fsrc%2FDemo.tsx%3A20%2C11 i.e filterModel.logicOperator
is undefined
on initializing multiple filters. This issue is on next and not on master. Let me know if there's a better fix you are aware of.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be Or
by default?
mui-x/packages/grid/x-data-grid/src/models/gridFilterModel.ts
Lines 16 to 18 in 243ae70
* @default `GridLogicOperator.Or` | |
*/ | |
logicOperator?: GridLogicOperator; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting point. @cherniavskii
I feel this @default GridLogicOperator.Or
statement is incorrect. Because if you check the current behavior on https://mui.com/x/react-data-grid/filtering/multi-filters/ it's GridLogicOperator.And
(try adding 2-3 filters without touching logic operator)
Any guesses if I should update the default value on this prop or the default behavior on the attached demo is incorrect?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, here we set the default logic operator:
mui-x/packages/grid/x-data-grid/src/hooks/features/filter/gridFilterState.ts
Lines 8 to 10 in 69f1ca4
export const getDefaultGridFilterModel: () => GridFilterModel = () => ({ | |
items: [], | |
logicOperator: GridLogicOperator.And, |
Feel free to update the jsdoc to reflect the actual default value 👍🏻
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the removal of this prop represents a breaking change to the interface of this component. The prop was documented so reasonable to assume it is (was) part of the public API for implementing GridFilterForm directly.
However this breaking change was not mentioned in any of the migration documentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@WillCDev Which prop are you referring to?
@@ -103,6 +103,26 @@ You can use the `onFilterModelChange` prop to listen to changes to the filters a | |||
|
|||
{{"demo": "ControlledFilters.js", "bg": "inline", "defaultCodeOpen": false}} | |||
|
|||
### Read-only filters | |||
|
|||
You can initialize the `filterModel`, set the `filterModel` prop, or use `apiRef.current.setFilterModel` to define the filters for columns with `colDef.filterable` set to `false`. These filters will be applied but the user won't be able to change them. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A small possible positive side effect would be this kind of filtering will now be possible in read-only mode without tweaking groupingColDef
.
packages/grid/x-data-grid/src/components/panel/filterPanel/GridFilterPanel.tsx
Outdated
Show resolved
Hide resolved
@@ -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 |
There was a problem hiding this comment.
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.
This pull request has conflicts, please resolve those before we can evaluate the pull request. |
@@ -205,30 +205,6 @@ | |||
"default": "ArrowDropDown", | |||
"class": null | |||
}, | |||
{ |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
Co-authored-by: Andrew Cherniavskii <[email protected]> Signed-off-by: Bilal Shafi <[email protected]>
colDef.filterable
part of [data grid] v7 API: Make the behavior ofGridColDef
flags consistent #10552ToDos:
Doc: https://deploy-preview-11538--material-ui-x.netlify.app/x/react-data-grid/filtering/#read-only-filters
Example usage: https://codesandbox.io/p/sandbox/bold-platform-9jg53c?file=%2Fsrc%2FDemo.tsx%3A41%2C9
Changelog
Breaking changes
filterModel
, or by updating thefilterModel
by API methodapiRef.current.setFilterModel
.