Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add ability to override the Datagrid header row #6496

Merged
merged 3 commits into from
Aug 12, 2021

Conversation

fzaninotto
Copy link
Member

@fzaninotto fzaninotto commented Aug 10, 2021

Problem

To override the datagrid headers (e.g. to have a header span over 2 columns), one has to copy the entire Datagrid code.

Solution

By default, <Datagrid> renders its header using <DatagridHeader>, an internal react-admin component. You can pass a custom component as the header prop to override that default. This can be useful e.g. to add a second header row, or to create headers spanning multiple columns.

For instance, here is a simple datagrid header that displays column names with no sort and no "select all" button:

import { TableHead, TableRow, TableCell } from '@material-ui/core';

const DatagridHeader = ({ children }) => (
    <TableHead>
        <TableRow>
            <TableCell></TableCell> {/* empty cell to account for the select row checkbox in the body */}
            {Children.map(children, child => (
                <TableCell key={child.props.source}>
                    {child.props.source}
                </TableCell>
            ))}
        </TableRow>
    </TableHead>
);

const PostList = props => (
    <List {...props}>
        <Datagrid header={<DatagridHeader />}>
            {/* ... */}
        </Datagrid>
    </List>
);

Bonus: Easier maintenance due to smaller files

@fzaninotto fzaninotto added the RFR Ready For Review label Aug 10, 2021
@fzaninotto fzaninotto added this to the 3.18 milestone Aug 10, 2021
@fzaninotto fzaninotto merged commit 5338363 into next Aug 12, 2021
@fzaninotto fzaninotto deleted the datagrid-header-custom branch August 12, 2021 07:31
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
RFR Ready For Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant