I know that 'disableColumnMenu' will disable the entire column, and setting 'sortable' and 'filterable' to false will remove those options for that column. Is there a way to disable specific menu options, or otherwise modify the column menu? I want to keep the columns sortable and filterable, but remove the 'show' and 'hide' options.
To remove the "Show"-columns and "Hide" menu items from the column menu, I just added the disableColumnSelector
to the DataGrid Component as show in the code image below.
You can do this by creating a custom menu and only including the filter and sort menu options.
// Assuming other imports such as React...
import {
GridColumnMenuContainer,
GridFilterMenuItem,
SortGridMenuItems
} from '@material-ui/data-grid';
const CustomColumnMenu = (props) => {
const { hideMenu, currentColumn } = props;
return (
<GridColumnMenuContainer
hideMenu={hideMenu}
currentColumn={currentColumn}
{...props}
>
<SortGridMenuItems onClick={hideMenu} column={currentColumn} />
<GridFilterMenuItem onClick={hideMenu} column={currentColumn} />
</GridColumnMenuContainer>
);
};
export default CustomColumnMenu;
Then, use it in your grid like so:
// Assuming imports are done including DataGrid, CustomColumnMenu, and React...
<DataGrid
// Assuming: rows, rowHeight, etc...
components={{
ColumnMenu: CustomColumnMenu
}}
/>
open
is marked as required in ForwardRef(GridColumnMenuContainer)
, but its value is undefined
. After much experimentation I fixed it by adding {...props} to the component like this: <GridColumnMenuContainer {...props} hideMenu={hideMenu} currentColumn={currentColumn}> Thanks Neil! With my small edit, your solution works great. –
Marsden There is also a prop disableColumnSelector={true} (passed to DataGrid component), however it will disable column selection for all columns, not only 1 specific column header.
You can do this by using the hideable
field in the columns definitions.
const columns: Array<GridColDef> = [
{
field: 'foo',
hideable: false
},
// ... other cols
]
<DataGrid columns={columns} rows={rows} />
This is not the ideal answer, but can be helpful for a specific use case for version 5.
In my case, I have the toolbar with filter option, and I wanted to remove the Filter from the options in column menu header.
There are couple of options, like creating a custom header. What I did is to add display none property to the element:
<DataGrid
{...props}
componentsProps={{
columnMenu: {
sx: {
"& li:nth-child(4)": {
// Filter is the 4th item in the column menu
display: "none",
},
},
},
}}
/>
© 2022 - 2024 — McMap. All rights reserved.
disableColumnSelector
removes only theshowColumn
option, but not thehide
option. How can thehide
option be removed? – Grunberg