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

[data grid] Hide column icon-separator for column resizable='false' #12017

Closed
Tracked by #9328
niralivasoya opened this issue Feb 9, 2024 · 5 comments · Fixed by #12134
Closed
Tracked by #9328

[data grid] Hide column icon-separator for column resizable='false' #12017

niralivasoya opened this issue Feb 9, 2024 · 5 comments · Fixed by #12134
Labels
component: data grid This is the name of the generic UI component, not the React module! recipe support: question Community support but can be turned into an improvement

Comments

@niralivasoya
Copy link

niralivasoya commented Feb 9, 2024

Steps to reproduce

This is how normally icon separators look like when we set disableColumnResize={false}. That means it allows you to resize the column, and this is how it looks when we hover over the header:

image

Current behavior

I want to allow the resize column only for the first column and disable the resizable to others. So, it is not allowed to resize other columns except the first one.

I want to hide the iron separator for other columns (where I set resizable to false). Ideally, we don't need an icon separator for those columns.

Expected behavior

How can I only show the icon separator for the resizable columns and hide it for others? How can I set the style? Any suggestions?

Thanks.

Context

This is the demo link:

https://codesandbox.io/p/sandbox/column-resizable-78jz77

Your environment

This is my current environment:
image

Search keywords: data-grid, column resize, hide icon speerator

@niralivasoya niralivasoya added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Feb 9, 2024
@michelengelen
Copy link
Member

Hey @niralivasoya
This is relatively easy to do:

import * as React from 'react';
import { DataGridPro, GridColDef, gridClasses } from '@mui/x-data-grid-pro';

const rows = [
  {
    id: 1,
    username: '@MUI',
    age: 20,
  },
];

const getHeaderClassName: GridColDef['headerClassName'] = (params) => {
  console.log('params', params);
  return !params.colDef.resizable ? 'hide-resize-handle' : '';
};

const columns: GridColDef[] = [
  {
    field: 'id',
  },
  {
    field: 'username',
    width: 125,
    minWidth: 150,
    maxWidth: 200,
    resizable: false,
  },
  { field: 'age', resizable: false },
].map((column) => ({ ...column, headerClassName: getHeaderClassName }));

export default function ColumnSizingGrid() {
  return (
    <div style={{ height: 250, width: '100%' }}>
      <DataGridPro
        columns={columns}
        rows={rows}
        sx={{
          [`& .${gridClasses.columnHeader}.hide-resize-handle`]: {
            [`.${gridClasses.columnSeparator}`]: {
              display: 'none',
            },
          },
        }}
      />
    </div>
  );
}

You can use the headerClassName property on a ColDef to apply a className based on a param.
There is also a page in our docs about this: Styling column headers

Let me know if you need anything else.
Thanks! 🙇🏼

@michelengelen michelengelen added support: question Community support but can be turned into an improvement component: data grid This is the name of the generic UI component, not the React module! status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Feb 12, 2024
@michelengelen
Copy link
Member

@cherniavskii should we add a recipe for this?

@michelengelen michelengelen changed the title Hide column icon-separator for column resizable='false' [data grid] Hide column icon-separator for column resizable='false' Feb 12, 2024
Copy link

The issue has been inactive for 7 days and has been automatically closed.

@michelengelen michelengelen added recipe and removed status: waiting for author Issue with insufficient information labels Feb 19, 2024
@michelengelen
Copy link
Member

Hey @niralivasoya ... while writing the recipe I found an easier way to do this:

import * as React from 'react';
import { DataGridPro, GridColDef, gridClasses } from '@mui/x-data-grid-pro';

const rows = [
  {
    id: 1,
    username: '@MUI',
    age: 20,
  },
];

const columns: GridColDef[] = [
  {
    field: 'id',
  },
  {
    field: 'username',
    width: 125,
    minWidth: 150,
    maxWidth: 200,
    resizable: false,
  },
  { field: 'age', resizable: false },
];

export default function ColumnHeaderHideSeparator() {
  return (
    <div style={{ height: 250, width: '100%' }}>
      <DataGridPro
        columns={columns}
        rows={rows}
        sx={{
          [`& .${gridClasses.columnSeparator}`]: {
            [`&:not(.${gridClasses['columnSeparator--resizable']})`]: {
              display: 'none',
            },
          },
        }}
      />
    </div>
  );
}

I am still putting this into the recipe section, but wanted to notify you here as well.

@onejeet
Copy link

onejeet commented Aug 8, 2024

Simplest solution...

 '& .MuiDataGrid-columnSeparator:not(.MuiDataGrid-columnSeparator--resizable)': {
            display: 'none !important',
          },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: data grid This is the name of the generic UI component, not the React module! recipe support: question Community support but can be turned into an improvement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants