【发布时间】:2021-07-20 05:21:38
【问题描述】:
我有一个 Material-UI DataGrid 组件,它有一个连续的编辑和删除按钮,但我只能做警报或控制台日志,
为什么会有按钮在一行,因为点击按钮时需要对话框,例如当用户点击删除按钮时会有一个删除操作的确认对话框,或者当点击一个编辑按钮时会有一个表单对话框更新行,
需要推送用户同时只编辑或删除一项。所以不允许多选。
我的问题是如何在点击列定义内的按钮时获取组件中的行数据,在这种情况下,点击Edit或Delete按钮。
使用"@material-ui/data-grid": "^4.0.0-alpha.26"
import Container from "@material-ui/core/Container";
import Grid from "@material-ui/core/Grid";
import Paper from "@material-ui/core/Paper";
import DeleteIcon from "@material-ui/icons/Delete";
import EditIcon from "@material-ui/icons/Edit";
import Button from "@material-ui/core/Button";
import AddIcon from "@material-ui/icons/Add";
import { makeStyles } from "@material-ui/core/styles";
import {
DataGrid,
GridColDef,
GridApi,
GridCellValue
} from "@material-ui/data-grid";
const columns: GridColDef[] = [
{ field: "id", headerName: "ID", width: 70 },
{ field: "name", headerName: "First name", width: 130 },
{ field: "surname", headerName: "Last name", width: 130 },
{
field: "edit",
headerName: "Edit",
sortable: false,
width: 130,
disableClickEventBubbling: true,
renderCell: (params) => {
const onClick = () => {
const api: GridApi = params.api;
const fields = api
.getAllColumns()
.map((c) => c.field)
.filter((c) => c !== "__check__" && !!c);
const thisRow: Record<string, GridCellValue> = {};
fields.forEach((f) => {
thisRow[f] = params.getValue(f);
});
// set to state rather then alert
return alert(JSON.stringify(thisRow, null, 4));
};
return (
<Button
variant="contained"
color="primary"
startIcon={<EditIcon />}
onClick={onClick}
>
Edit
</Button>
);
}
},
{
field: "delete",
headerName: "Delete",
sortable: false,
width: 130,
disableClickEventBubbling: true,
renderCell: (params) => {
const onClick = () => {
const api: GridApi = params.api;
const fields = api
.getAllColumns()
.map((c) => c.field)
.filter((c) => c !== "__check__" && !!c);
const thisRow: Record<string, GridCellValue> = {};
fields.forEach((f) => {
thisRow[f] = params.getValue(f);
});
// set to state rather then alert
return alert(JSON.stringify(thisRow, null, 4));
};
return (
<Button
variant="contained"
color="secondary"
startIcon={<DeleteIcon />}
onClick={onClick}
>
Delete
</Button>
);
}
}
];
const rows = [
{ id: 1, name: "Example 1", surname: "example" },
{ id: 2, name: "Example 2", surname: "example" }
];
export type User = {
id: number;
name: string;
surname: string;
};
const useStyles = makeStyles((theme) => ({
content: {
flexGrow: 1,
height: "100vh",
overflow: "auto"
},
appBarSpacer: theme.mixins.toolbar,
container: {
paddingTop: theme.spacing(4),
paddingBottom: theme.spacing(4)
},
paper: {
padding: theme.spacing(2),
display: "flex",
overflow: "auto",
flexDirection: "column",
elevation: 3
}
}));
const App = () => {
const classes = useStyles();
return (
<main className={classes.content}>
<div className={classes.appBarSpacer} />
<Container maxWidth="lg" className={classes.container}>
<Grid item xs={12}>
<Paper className={classes.paper}>
<DataGrid
rows={rows}
columns={columns}
pageSize={10}
columnBuffer={8}
autoHeight
/>
</Paper>
</Grid>
<Grid item xs={3} style={{ padding: 20 }}>
<Button
variant="contained"
color="primary"
startIcon={<AddIcon />}
onClick={() => {
console.log("new");
}}
>
New
</Button>
</Grid>
</Container>
</main>
);
};
export default App;
【问题讨论】:
-
我想知道Dialog组件在哪里。
-
您没有在
renderCell中放置任何对话框组件。 -
@Medi 如果没有实现和按需显示/隐藏,就无法打开对话框。
-
对不起,我稍后会添加对话框组件。但现在我只想设置状态,然后我可以将任何想要的组件传递给它。
标签: javascript reactjs material-ui