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

[docs] Add sticky column header demo #8297

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions docs/data/data-grid/column-header/StickyColumnHeader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import * as React from 'react';
import Box from '@mui/material/Box';
import Typography from '@mui/material/Typography';
import { DataGrid, gridClasses } from '@mui/x-data-grid';
import { useDemoData } from '@mui/x-data-grid-generator';

export default function StickyColumnHeader() {
const { data: commodityData } = useDemoData({
dataSet: 'Commodity',
rowLength: 20,
maxColumns: 20,
});

const { data: employeeData } = useDemoData({
dataSet: 'Employee',
rowLength: 20,
maxColumns: 20,
});

return (
<Box sx={{ width: '100%', height: 350, overflow: 'auto' }}>
Copy link
Member

@m4theushw m4theushw Mar 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look good on Windows:

image

First I have to scroll vertically to be able to scroll horizontally.

What do you think of removing height: 350, overflow: 'auto' and let the content expand freely? The drawback is that the column headers will be hidden under the docs' toolbar, but we can use top: 65 and put a message explaining that top: 0 should be used outside the MUI website.

image

It feels more natural without the terrible looking scrollbars.

<Typography variant="h6" my={1}>
Commodities
</Typography>
<DataGrid
{...commodityData}
autoHeight
pageSizeOptions={[5, 10, 20]}
initialState={{
pagination: { paginationModel: { pageSize: 5 } },
}}
sx={(theme) => ({
[`.${gridClasses.main}`]: {
overflow: 'unset',
},
[`.${gridClasses.columnHeaders}`]: {
position: 'sticky',
top: 0,
backgroundColor: theme.palette.background.paper,
zIndex: 1,
},
})}
/>
<Typography variant="h6" my={1}>
Employees
</Typography>
<DataGrid
{...employeeData}
autoHeight
pageSizeOptions={[5, 10, 20]}
initialState={{
pagination: { paginationModel: { pageSize: 10 } },
}}
sx={(theme) => ({
[`.${gridClasses.main}`]: {
overflow: 'unset',
},
[`.${gridClasses.columnHeaders}`]: {
position: 'sticky',
top: 0,
backgroundColor: theme.palette.background.paper,
zIndex: 1,
},
})}
/>
</Box>
);
}
68 changes: 68 additions & 0 deletions docs/data/data-grid/column-header/StickyColumnHeader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import * as React from 'react';
import Box from '@mui/material/Box';
import Typography from '@mui/material/Typography';
import { DataGrid, gridClasses } from '@mui/x-data-grid';
import { useDemoData } from '@mui/x-data-grid-generator';

export default function StickyColumnHeader() {
const { data: commodityData } = useDemoData({
dataSet: 'Commodity',
rowLength: 20,
maxColumns: 20,
});

const { data: employeeData } = useDemoData({
dataSet: 'Employee',
rowLength: 20,
maxColumns: 20,
});

return (
<Box sx={{ width: '100%', height: 350, overflow: 'auto' }}>
<Typography variant="h6" my={1}>
Commodities
</Typography>
<DataGrid
{...commodityData}
autoHeight
pageSizeOptions={[5, 10, 20]}
initialState={{
pagination: { paginationModel: { pageSize: 5 } },
}}
sx={(theme) => ({
[`.${gridClasses.main}`]: {
overflow: 'unset',
},
[`.${gridClasses.columnHeaders}`]: {
position: 'sticky',
top: 0,
backgroundColor: theme.palette.background.paper,
zIndex: 1,
},
})}
/>
<Typography variant="h6" my={1}>
Employees
</Typography>
<DataGrid
{...employeeData}
autoHeight
pageSizeOptions={[5, 10, 20]}
initialState={{
pagination: { paginationModel: { pageSize: 10 } },
}}
sx={(theme) => ({
[`.${gridClasses.main}`]: {
overflow: 'unset',
},
[`.${gridClasses.columnHeaders}`]: {
position: 'sticky',
top: 0,
backgroundColor: theme.palette.background.paper,
zIndex: 1,
},
})}
/>
</Box>
);
}
6 changes: 6 additions & 0 deletions docs/data/data-grid/column-header/column-header.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ const columns: GridColDef[] = [

{{"demo": "RenderHeaderGrid.js", "bg": "inline"}}

## Sticky column header

You can make the column header sticky when the page is scrolled:

{{"demo": "StickyColumnHeader.js", "bg": "inline"}}

## Styling header

You can check the [styling header](/x/react-data-grid/style/#styling-column-headers) section for more information.
Expand Down