Let's say I have a datagrid table like so (from the official MUI documentation):
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 ControlledFilters() {
const { data } = useDemoData({
dataSet: 'Employee',
visibleFields: VISIBLE_FIELDS,
rowLength: 100,
});
return (
<div style={{ height: 400, width: '100%' }}>
<DataGrid
{...data}
components={{
Toolbar: GridToolbar,
}}
/>
</div>
);
}
Now let's say I wanted to filter the column 'name' in this table by onClick of a button. That button, when clicked, should give a string, let's say 'Ray'. When this button is clicked, I want to automatically filter the table so that every value in the 'name' column that contains the string 'Ray' only gets displayed.
My Approach so far
I have tried using useState
from react and the filterModel
prop in DataGrid so that pressing the button filters the table like so:
....
const [filt, setFilt] = useState('') // Initialize it with an empty filter
const handleClick = () => {
setFilt('Ray');
};
return (
<div style={{ height: 400, width: '100%' }}>
<DataGrid
{...data}
components={{
Toolbar: GridToolbar,
}}
filterModel={{
items: [{ columnField: 'name', operatorValue: 'contains', value: filt },
]
}}
/>
<Button onClick={handleClick}>Change Filter</Button>
</div>
);
}
This works but the problem with this approach is that it locks every other filter and the filter is basically stuck on the 'name' column and the user can now only use the button to filter the column. It doesn't even let me remove the filter.
I have also tried the onFilterModelChange
prop but it didn't work; I am honestly confused on how to use it for this specific case. Any help would be appreciated.