-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[docs] Add styling row group recipe #19349
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
Changes from 4 commits
0871cdc
a9cd835
c2d8204
a79834c
02a9f74
22ae725
8c82059
de8bd66
17e1004
a64ddff
f5c884c
d2ddd7f
a500ba3
3aea7f0
6b1cbec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import * as React from 'react'; | ||
| import { | ||
| DataGridPremium, | ||
| useGridApiRef, | ||
| useKeepGroupedColumnsHidden, | ||
| } from '@mui/x-data-grid-premium'; | ||
| import { useMovieData } from '@mui/x-data-grid-generator'; | ||
|
|
||
| const EXPECTED_GROSS = 2000000000; | ||
|
|
||
| export default function RowGroupingStyling() { | ||
| const apiRef = useGridApiRef(); | ||
| const data = useMovieData(); | ||
|
|
||
| const getRowClassName = (params) => { | ||
| const node = apiRef.current?.getRowNode(params.id); | ||
|
|
||
| if (!node) { | ||
| return ''; | ||
| } | ||
|
|
||
| if (node.type === 'group') { | ||
| const childIds = node.children || []; | ||
|
|
||
| let hasExpectedGross = false; | ||
|
|
||
| for (const childId of childIds) { | ||
| const childNode = apiRef.current?.getRowNode(childId); | ||
| if (childNode && childNode.type === 'leaf') { | ||
| const childRow = apiRef.current?.getRow(childId); | ||
| if (childRow?.gross && childRow.gross > EXPECTED_GROSS) { | ||
| hasExpectedGross = true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (hasExpectedGross) { | ||
| return 'highlighted-group'; | ||
| } | ||
| return ''; | ||
| } | ||
|
|
||
| if (node.parent) { | ||
| const row = params.row; | ||
|
|
||
| if (row.gross > EXPECTED_GROSS) { | ||
| return 'highlighted-child'; | ||
| } | ||
| } | ||
|
|
||
| return ''; | ||
| }; | ||
|
|
||
| const initialState = useKeepGroupedColumnsHidden({ | ||
| apiRef, | ||
| initialState: { | ||
| rowGrouping: { | ||
| model: ['company'], | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| return ( | ||
| <div style={{ height: 400, width: '100%' }}> | ||
| <DataGridPremium | ||
| {...data} | ||
| apiRef={apiRef} | ||
| initialState={initialState} | ||
| getRowClassName={getRowClassName} | ||
| sx={(theme) => ({ | ||
| '& .highlighted-group': { | ||
| '&::before': { | ||
| content: '""', | ||
| display: 'block', | ||
| position: 'absolute', | ||
| left: 0, | ||
| width: 4, | ||
| height: 'calc(var(--height) * 0.8)', | ||
| marginTop: 'calc(var(--height) * 0.1)', | ||
| backgroundColor: 'success.main', | ||
| borderTopRightRadius: 4, | ||
| borderBottomRightRadius: 4, | ||
| }, | ||
| }, | ||
| '& .highlighted-child': { | ||
| backgroundColor: `color-mix(in srgb, ${(theme.vars || theme).palette.success.main} 8%, transparent)`, | ||
| '&:hover': { | ||
| backgroundColor: `color-mix(in srgb, ${(theme.vars || theme).palette.success.main} 12%, transparent)`, | ||
| }, | ||
| }, | ||
| })} | ||
| /> | ||
| </div> | ||
| ); | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,98 @@ | ||||||||||
| import * as React from 'react'; | ||||||||||
| import { | ||||||||||
| DataGridPremium, | ||||||||||
| useGridApiRef, | ||||||||||
| useKeepGroupedColumnsHidden, | ||||||||||
| GridRowClassNameParams, | ||||||||||
| } from '@mui/x-data-grid-premium'; | ||||||||||
| import { useMovieData } from '@mui/x-data-grid-generator'; | ||||||||||
|
|
||||||||||
| type Movie = ReturnType<typeof useMovieData>['rows'][number]; | ||||||||||
|
|
||||||||||
| const EXPECTED_GROSS = 2000000000; | ||||||||||
|
|
||||||||||
| export default function RowGroupingStyling() { | ||||||||||
| const apiRef = useGridApiRef(); | ||||||||||
| const data = useMovieData(); | ||||||||||
|
|
||||||||||
| const getRowClassName = (params: GridRowClassNameParams<Movie>) => { | ||||||||||
| const node = apiRef.current?.getRowNode(params.id); | ||||||||||
siriwatknp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
|
|
||||||||||
| if (!node) { | ||||||||||
| return ''; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (node.type === 'group') { | ||||||||||
| const childIds = node.children || []; | ||||||||||
|
|
||||||||||
| let hasExpectedGross = false; | ||||||||||
|
|
||||||||||
| for (const childId of childIds) { | ||||||||||
| const childNode = apiRef.current?.getRowNode(childId); | ||||||||||
siriwatknp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
| /** | |
| * The node of the row that the current cell belongs to. | |
| */ | |
| rowNode: N; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean updating the implementation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it should be a minimal and non breaking change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't necessarily need to be done in the same PR though. Keeping the scope relevant should also be fine.
Uh oh!
There was an error while loading. Please reload this page.