Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you disable specific Material-UI DataGrid Column Menu options?

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.

enter image description here

like image 853
Robert Avatar asked Mar 07 '21 07:03

Robert


People also ask

How do you add a button to every row in a material UI table?

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.

What is renderCell?

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.

What is data grid in material UI?

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.

What is the material-UI DataGrid component?

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):

How to disable column filter and column selection?

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

How to set background color in Mui’s DataGrid?

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.

When are bubble-up cell clicks allowed in DataGrid?

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.


Video Answer


2 Answers

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
                        }}
                    />

Result of using the outlined Custom Column Menu

like image 68
Neil Guy Lindberg Avatar answered Nov 05 '22 10:11

Neil Guy Lindberg


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.

remove-show-hide-from-menu

disableColumnSelector_code

like image 29
luis Avatar answered Nov 05 '22 11:11

luis