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.
What you need to do is include a renderCell method in your columns array. In the above I am rendering a Button inside columns 5 and 6 which will appear on every populated row. Above that you can have a function which creates and returns a Button from Material-ui. This should be the default answer.
renderCell can be used to inject any element or component into a cell. Good components to inject might be TextFields, Icons, or basic elements such as divs (for precise styling of text in a cell). Keep in mind that DataGrids are intended for rending thousands or millions of rows of data.
Material UI's Data Grid is a powerful and flexible data table. It makes it easy for you to display data and perform out of the box functionalities such as editing, sorting, filtering, pagination and more. In this article, we'll discuss why you should use the Data Grid in Material UI in your project.
The Material-UI DataGrid component is designed for interacting with large data sets. However, it can be tricky to get the layout exactly like you want. In this article I will construct a DataGrid with the custom features listed in the table of contents (plus a Resources section):
disableColumnMenu will completely remove the button with the options disableColumnFilter will just remove the filter option disableColumnSelector will remove the hide/show columns you can check the sort true/false while creating the columns with a simple sortable = true/false
Background color can be set in MUI’s DataGrid by targeting .root -> .MuiDataGrid-renderingZone -> .MuiDataGrid-row, similar to the below code. This sets the background color at the row level. If you want to alternate row color, consider using the nth-child selector.
Other cell clicks are allowed to bubble up. An example of when this might be desired is if column 2 cells are informational and we don’t want row actions to fire when column 2 cells are clicked. There are many other handlers in the DataGrid API.
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}
>
<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
}}
/>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With