Comments (20)
Here is a working example
It's pretty much what you have in your last message 👍
from mui-x.
No, this is not the only way to do this, just one.
You could just as well apply a pure CSS way of doing this.
const StripedDataGrid = styled(DataGridPremium)(({ theme }) => ({
[`& .${gridClasses.row}:nth-child(even)`]: {
backgroundColor: theme.palette.grey[200],
'&:hover': {
backgroundColor: alpha(theme.palette.primary.main, ODD_OPACITY),
'@media (hover: none)': {
backgroundColor: 'transparent',
},
},
'&.Mui-selected': {
backgroundColor: alpha(
theme.palette.primary.main,
ODD_OPACITY + theme.palette.action.selectedOpacity,
),
'&:hover': {
backgroundColor: alpha(
theme.palette.primary.main,
ODD_OPACITY +
theme.palette.action.selectedOpacity +
theme.palette.action.hoverOpacity,
),
// Reset on touch devices, it doesn't add specificity
'@media (hover: none)': {
backgroundColor: alpha(
theme.palette.primary.main,
ODD_OPACITY + theme.palette.action.selectedOpacity,
),
},
},
},
},
}));
You could also do a global override with a custom theme if that is more convenient.
from mui-x.
notice the [`& .${gridClasses.row}:nth-child(even)/`]
CSS selector (nth-child
)
from mui-x.
@michelengelen won't that flicker when you scroll due to virtualization?
from mui-x.
@michelengelen tanks for your reply.
This is exactly the approach I used for a long time, but now with new version 7.50 it flickers and row backgrounds switch.
DataGridPremium-Striped.mov
from mui-x.
I think it could be useful to define a boolean striped property for DataGrid that automatically adds Mui-even / Mui-odd calsses with default styles when activated. Of course then developers can override and customize the style.
What do you think?
Many thanks,
Mauro
from mui-x.
You should be able to set `getRowClassName in your theme so it applies on every data grid you have.
from mui-x.
@flaviendelangle how should I put getRowClassName in createTheme({...}) call?
Many thanks for your help.
Mauro
from mui-x.
Ok, found:
MuiDataGrid: {
defaultProps: {
getRowClassName: ({ indexRelativeToCurrentPage }) =>
indexRelativeToCurrentPage % 2 === 0 ? "even" : "odd",
},
},
from mui-x.
@flaviendelangle many thanks for your help!
from mui-x.
Great that we could be of help and thanks @flaviendelangle for providing a great example.
I will close this now, but feel free to reopen if you have follow-up questions! 🙇🏼
from mui-x.
Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.
@mauro-ni: How did we do? Your experience with our support team matters to us. If you have a moment, please share your thoughts in this short Support Satisfaction survey.
from mui-x.
Playing with the new implementation I noticed that on pinned columns the stripe effect is lost.
from mui-x.
@michelengelen can I let you see how to support pinned columns?
It's probably just a question of applying the CSS to the right element, unless we don't apply the row classname to pinned columns at all.
from mui-x.
@flaviendelangle good morning, regarding the suggested approach for stripes (using alpha), I would like to point out that there is a problem when applying it to pinned columns: on scroll the content this is visible under the pinned columns. It would probably be better to use darken / lighten utilities.
Another styling issue with v 7.5 is on DataGrid used inside Paper component: header and pinned columns get a darker background.
I read about that in https://mui.com/x/migration/migration-data-grid-v6
The column headers and pinned section now require an explicit color. By default, the MUI theme.palette.background.default color will be used. To customize it, see https://mui.com/material-ui/customization/palette/#customization We will be adding a new color name to the palette for additional customization, read #12443 for more details.
Is there a way to globally handle that?
from mui-x.
As a temporary solution I managed the background customizing the theme as follows (inPaper is a prop that I set on DataGrid):
MuiDataGrid: {
styleOverrides: {
root: ({ ownerState, theme }) => {
const paperBackground = theme.palette.background.paper;
if (ownerState?.inPaper) {
return {
[`--DataGrid-pinnedBackground`]: `${paperBackground} !important`,
[`--DataGrid-containerBackground`]: `${paperBackground} !important`,
};
}
return {};
},
}
}
I don't like this solution, it is a workaround, but ... seems to work.
Do you have a better solution for that?
from mui-x.
@mauro-ni this seems like a good way to handle the pinned background until we come up with a solid solution internally. Interestingly enough I. haven't noticed the header behavior before. Thanks for pointing that out.
@flaviendelangle I had a codesandbox somewhere where I did implement that once ... just cannot find it anymore! 🤷🏼
But it is rather simple. Instead of targeting the row styles you can just as well target the cells:
`& .${gridClasses.row}.even > .${gridClasses.cell}`
from mui-x.
Maybe we could adapt https://mui.com/x/react-data-grid/style/#striped-rows to handle pinned rows
from mui-x.
@michelengelen thank you for your help.
Targeting the cell instead of the row with the code you suggested partially breaks hover effect.
`& .${gridClasses.row}.even > .${gridClasses.cell}`
from mui-x.
Here is a temporary solution that, again, I don't like (there was an issue with the implementation with alpha() because pinned columns have forced background, and for now I force the backgrounds I want).
I think that a boolean striped property on DataGrid / DataGridPro / DataGridPremium could be useful (maybe being able to specify hover & selection color).
const ODD_OPACITY = 0.2;
MuiDataGrid: {
defaultProps: {
getRowClassName: ({ indexRelativeToCurrentPage }) =>
indexRelativeToCurrentPage % 2 === 0 ? "even" : "odd",
},
styleOverrides: {
columnHeaderTitle: {
fontWeight: "bold",
},
columnHeader: {
[`& .MuiFormControl-root`]: {
flex: 1,
},
},
// HACK to handle DataGrid inside Paper
root: ({ ownerState, theme }) => {
const paperBackground = theme.palette.background.paper;
if (ownerState?.inPaper) {
return {
[`--DataGrid-pinnedBackground`]: `${paperBackground} !important`,
[`--DataGrid-containerBackground`]: `${paperBackground} !important`,
};
}
return {};
},
// HACK to make striped rows work with pinned columns
row: ({ theme }) => ({
[`&.even, &.even .${gridClasses["cell--pinnedLeft"]}, &.even .${gridClasses["cell--pinnedRight"]}`]:
{
backgroundColor:
theme.palette.mode === "light"
? lighten(theme.palette.grey[200], ODD_OPACITY)
: lighten(theme.palette.grey[900], ODD_OPACITY),
},
[`&:hover, &:hover .${gridClasses["cell--pinnedLeft"]}, &:hover .${gridClasses["cell--pinnedRight"]}`]:
{
backgroundColor:
theme.palette.mode === "light"
? `rgb(211, 223, 241) !important`
: `rgb(69, 79, 87) !important`,
"@media (hover: none)": {
backgroundColor: "transparent",
},
},
[`&.Mui-selected, &.Mui-selected .${gridClasses["cell--pinnedLeft"]}, &.Mui-selected .${gridClasses["cell--pinnedRight"]}`]:
{
backgroundColor:
theme.palette.mode === "light"
? `rgb(196, 213, 237) !important`
: `rgb(86, 102, 118) !important`,
[`&:hover, &:hover .${gridClasses["cell--pinnedLeft"]}, &:hover .${gridClasses["cell--pinnedRight"]}`]:
{
backgroundColor:
theme.palette.mode === "light"
? `rgb(188, 206, 234) !important`
: `rgb(96, 115, 134) !important`,
// Reset on touch devices, it doesn't add specificity
"@media (hover: none)": {
backgroundColor:
theme.palette.mode === "light"
? `rgb(196, 213, 237) !important`
: `rgb(86, 102, 118) !important`,
},
},
},
}),
},
},
from mui-x.
Related Issues (20)
- [data grid] `valueFormatter` or `valueGetter` are not applying for excel file exports HOT 25
- [x-license] License of MUI X Premium license shows as MUI expired package version HOT 12
- [data grid] How to apply conditional cell color and cell selection color at the same time HOT 4
- [pickers] Allow better date picker customization HOT 1
- [data grid] Edited row does not update when update "rows" state for DataGrid "rows" prop in processRowUpdate HOT 5
- test HOT 1
- [data grid] How to reset filter explicity or using apiRef? HOT 6
- [data grid] Add option to position aggregation label alongside the aggregate value HOT 3
- [data grid] Visual indicator for editable cells/rows HOT 2
- [datagrid] No slots for when all the columns are hidden and render blank rows HOT 1
- [data grid] Add "no visible columns" overlay
- [data grid] Implementing a Custom Filter for MUI DataGrid HOT 1
- [tree view] Newly bought key does not cover x-tree-view-pro: MUI X: Component not included in your license. HOT 2
- [pickers] How do I get default min and max date from `LocalizationProvider`? HOT 2
- [pickers][DateRangeCalendar] Using a controlled `DateRangeCalendar` with `currentMonthCalendarPosition` breaks scroll behaviour HOT 1
- [data grid] Row reordering does not work HOT 9
- [pickers] Date picker crash on mui.com HOT 10
- How to customize the JoyBreadcrumbs item color,
- [data grid] Column definition `undefined` value HOT 1
- [data grid] Remove edit mode select outline HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mui-x.