[英]Custom edit and delete components on row DataGrid MUI v5 component hovered
I'm using Material UI or MUI v5 components for the UI library in my React Js app.我在我的 React Js 应用程序中为 UI 库使用 Material UI 或MUI v5组件。
I'm creating a custom edit and delete row inside the DataGrid
/ DataGridPro
component.我正在
DataGrid
/ DataGridPro
组件内创建自定义编辑和删除行。
The specification is to show edit and delete icons for the hovered row but not by adding a new column (not adding an action column).规范是显示悬停行的编辑和删除图标,但不是通过添加新列(不是添加操作列)。 I found the example for a private dashboard here.
我在这里找到了私人仪表板的示例。
So if the user decreases the width of the browser, the edit and delete icons wouldn't be hidden instead it would hide the row as seen in the image below.因此,如果用户减小浏览器的宽度,则不会隐藏编辑和删除图标,而是会隐藏该行,如下图所示。
I made it here https://codesandbox.io/s/learn-mui-data-grid-hover-row-367vh?file=/demo.js but I found some drawbacks below:我在这里做了https://codesandbox.io/s/learn-mui-data-grid-hover-row-367vh?file=/demo.js但我发现以下一些缺点:
Popper
component is rendered on top of the DataGrid
component Popper
组件渲染在DataGrid
组件之上When our mouse cursor hovered the row that was not fully shown, the Popper
component was shown like these images below.当我们的鼠标 cursor 悬停在未完全显示的行上时,
Popper
组件如下图所示。
I've tried to add the disablePortal={true}
prop to the Popper component but it would make the Popper
component rendered outside the row like this image below.我尝试将
disablePortal={true}
道具添加到 Popper 组件,但它会使Popper
组件呈现在行之外,如下图所示。
I also have tried to change the zIndex
prop for the column header (became 1000), the row (became 10), and the pagination container (became 1000) of the DataGrid
component also changed the zIndex
prop of the Popper
component (became 100) but still the Popper
component was rendered on top of the DataGrid
component like this image below.我还尝试更改了header列(变为1000)、行(变为10)和
DataGrid
组件的分页容器(变为1000)的zIndex
prop也更改了Popper
组件的zIndex
prop(变为100)但是Popper
组件仍然呈现在DataGrid
组件之上,如下图所示。
Question 1: What should I do to make the Popper
component rendered on the top of the row but still inside the DataGrid
component like this image below?问题 1:我应该怎么做才能使
Popper
组件呈现在行的顶部但仍在DataGrid
组件内,如下图所示?
Popper
component was not sticking to the DataGrid
component Popper
组件没有粘在DataGrid
组件上If I add a few new columns, the Popper
component was sticking on the end of the row like this image below.如果我添加一些新列,
Popper
组件就会像下图一样粘在行尾。 This was the condition I want.这就是我想要的条件。
If we scroll our mouse to the start of the row, the Popper
component was not sticking to the end of the row like this image below.如果我们将鼠标滚动到行首,
Popper
组件不会像下图那样粘在行尾。 This was not the condition I want.这不是我想要的条件。
Question 2: What should I do to make the Popper component stick to the end of the row regardless of scrolling horizontally our mouse like this image below?问题 2:我应该怎么做才能使 Popper 组件粘在行的末尾而不管我们的鼠标水平滚动如下图所示?
Here is the demo https://codesandbox.io/s/learn-mui-data-grid-hover-row-367vh?file=/Demo2.jsx这是演示https://codesandbox.io/s/learn-mui-data-grid-hover-row-367vh?file=/Demo2.jsx
Or is there any better way to achieve this case?或者有没有更好的方法来实现这种情况?
Finally, I could make it after learning about these features:最后,在了解了这些功能后,我可以做到:
componentsProps
https://mui.com/components/data-grid/components/#row to control which row is hovered componentsProps
https://mui.com/components/data-grid/components/#row控制悬停哪一行Here are the steps:以下是步骤:
componentsProps
to control which row is hoveredcomponentsProps
控制悬停的行componentsProps={{
row: {
onMouseEnter: onMouseEnterRow,
onMouseLeave: onMouseLeaveRow
}
}}
actions
column and only show the edit and delete icons when the row is hoveredactions
列,仅在悬停该行时显示编辑和删除图标{
field: "actions",
headerName: "",
width: 120,
sortable: false,
disableColumnMenu: true,
renderCell: (params) => {
if (hoveredRow === params.id) {
return (
<Box
sx={{
backgroundColor: "whitesmoke",
width: "100%",
height: "100%",
display: "flex",
justifyContent: "center",
alignItems: "center"
}}
>
<IconButton onClick={() => console.log(params.id)}>
<EditIcon />
</IconButton>
<IconButton onClick={() => console.log(params.id)}>
<DeleteIcon />
</IconButton>
</Box>
);
} else return null;
}
}
actions
column and change the DataGrid
stylesactions
列并更改DataGrid
styles<DataGridPro
// some code here ...
initialState={{ pinnedColumns: { right: ["actions"] } }}
sx={{
"& .MuiDataGrid-iconSeparator": {
display: "none"
},
"& .MuiDataGrid-pinnedColumnHeaders": {
boxShadow: "none",
backgroundColor: "transparent"
},
"& .MuiDataGrid-pinnedColumns": {
boxShadow: "none",
backgroundColor: "transparent",
"& .MuiDataGrid-cell": {
padding: 0
}
},
"& .MuiDataGrid-row": {
cursor: "pointer",
"&:hover": {
backgroundColor: "whitesmoke"
},
"&:first-child": {
borderTop: "1px solid rgba(224, 224, 224, 1)"
}
},
"& .MuiDataGrid-cell:focus": {
outline: "none"
},
"& .MuiDataGrid-cell:focus-within": {
outline: "none"
}
}}
/>
Here is the demo https://codesandbox.io/s/learn-mui-data-grid-column-pinning-gzgn0?file=/demo.js这是演示https://codesandbox.io/s/learn-mui-data-grid-column-pinning-gzgn0?file=/demo.js
All you really need to do is to adjust the translateX
value to position it inside the table.您真正需要做的就是将
translateX
值调整为 position 它在表格内。
Adjust Paper component's translateX
value to -270px
:将 Paper 组件的
translateX
值调整为-270px
:
<Paper sx={{ transform: "translateX(-270px)", zIndex: 100 }} style={myStyle}>
<Button onClick={()=> console.log("edit: ", value.id)}>
Edit
</Button>
<Button onClick={()=> console.log("delete: ", value.id)}>
Delete
</Button>
</Paper>
And add this style(Optional):并添加此样式(可选):
const myStyle = {
background: '#F5F5F5',
border: 'none',
boxShadow: 'none'
}
Like so:像这样:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.