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

[BUG] MUI useDataGrid setFilters #5860

Closed
JoeGaffney opened this issue Apr 17, 2024 · 5 comments
Closed

[BUG] MUI useDataGrid setFilters #5860

JoeGaffney opened this issue Apr 17, 2024 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@JoeGaffney
Copy link

Describe the bug

setFilters does not update the dataGridProps filterModel

filters themselves update and setFilters works

Steps To Reproduce

use useDataGrid with filter mode set to client "off"

Follow the tutorial for https://refine.dev/docs/ui-integrations/material-ui/hooks/use-data-grid/#filtering but setting filter to off.

Possibly also an issue on server mode If you want to see the active filters reflected in the mui date grid controls.

Expected behavior

Filters should set the model

Here is a work around but ideally should happen in the useDataGrid hook

const handleFilter = (e: React.ChangeEvent<HTMLInputElement>, checked: boolean) => {
  let result: any = {
    field: "status",
    value: checked ? "active" : undefined,
    operator: "contains",
  };
  setFilters([result]);
};

// we make an update to the filter model and passin the updated filter model to the data grid
const { filterModel, ...restDataGridProps } = dataGridProps;
const filterModelUpdated: any = {
  items: [...filters],
  logicOperator: "and",
};

<DataGrid {...restDataGridProps} filterModel={filterModelUpdated} columns={columnsBankAccounts} />

Packages

@refinedev/mui

Additional Context

No response

@JoeGaffney JoeGaffney added the bug Something isn't working label Apr 17, 2024
@aliemir
Copy link
Member

aliemir commented Apr 18, 2024

Hey @JoeGaffney, sorry for the issue! We'll investigate it and find out what went wrong. Filters had to be connected bi-directionally. Maybe something changed in the <DataGrid /> API or we may have missed something during our refactors. Thank you for providing a workaround 🚀

@JoeGaffney
Copy link
Author

Hey @aliemir no problem. It's super coinvent that you are providing both server and client side filtering in with the useDataGrid, its a big time saver.

@aliemir aliemir self-assigned this Apr 30, 2024
@aliemir
Copy link
Member

aliemir commented Apr 30, 2024

Hey @JoeGaffney, worked on this issue for a while and come to a conclusion for now. We're supporting both Pro and community versions of the MUI Data Grid with our @refinedev/mui's useDataGrid hook.

In the community version, data grid is limited to single column filtering. With Refine, you can place your filters outside of data grid to support multiple filters on multiple fields. When we try to reflect the changes in filters of Refine to filter model of data grid, we end up messing with the current state of the filters and filtering in column headers break. For the pro version it works without issues since it supports multiple fields to be filtered in the same time.

Reflecting changes on the data grid may break the existing apps using both column headers to filter and provide filtering with external elements. In the future we may introduce an API to pick and reflect only the wanted fields on the filter model but for now we've decided on not taking any actions here.

Do you have multiple filters on your data grid? Do you manage those in column headers or do you manage them outside of the data grid? We'd like to know your use case better if you can provide anything more and maybe we can try to implement a different solution for it. 🚀

As a workaround, yours work well with single fields and I can suggest another one without changing the filterModel prop. <DataGrid /> component accepts an apiRef prop which you can generate a ref using useGridApiRef and set filters and sorters using the ref.

Let me know if we can help you with any other issue 🙏

@aliemir aliemir closed this as not planned Won't fix, can't repro, duplicate, stale Apr 30, 2024
@JoeGaffney
Copy link
Author

Hey @aliemir I was just using the one filter using the basic version.

I also did notice some issue with this approach that I can not always manually changes the filters after. I will look into the useGridApiRef

@JoeGaffney
Copy link
Author

Thanks @aliemir using the ref works perfectly and simplfies the code
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants