javascript - 无法从 react material-ui 数据网格列表中删除项目,可能是什么问题?
问题描述
我有一个基本的客户名单。我正在使用材质 UI DataGrid 来呈现列表。虽然每个列表都有编辑和删除项目的按钮。但是我无法从列表中删除该项目,即使当我尝试控制台时我得到了客户的特定 ID。 这是我在代码框中的完整代码的链接。
CustomerList.js(我用注释突出显示了我的代码,以便您可以轻松跳转到该特定区域)
import React, { useState, useEffect } from "react";
import { Button, Grid, Typography, Box } from "@material-ui/core";
import { DataGrid } from "@material-ui/data-grid";
import { useStyles } from "./CustomerListStyle";
import TextField from "@material-ui/core/TextField";
import Dialog from "@material-ui/core/Dialog";
import DialogActions from "@material-ui/core/DialogActions";
import DialogContent from "@material-ui/core/DialogContent";
import DialogContentText from "@material-ui/core/DialogContentText";
import DialogTitle from "@material-ui/core/DialogTitle";
import { useHistory } from "react-router-dom";
// my initial data which I'm passing to state names customers
const customerData = [
{ id: 1, name: "Customer 1", gender: "Male", email: "user@gmail.com" },
{ id: 2, name: "Customer 2", gender: "Male", email: "user@gmail.com" },
{ id: 3, name: "Customer 3", gender: "Female", email: "user@gmail.com" }
];
const CustomerList = () => {
const classes = useStyles();
const [customers, setCustomers] = React.useState(customerData);
const [open, setOpen] = React.useState(false);
const [name, setName] = React.useState("");
const [gender, setGender] = React.useState("");
const [email, setEmail] = React.useState("");
// just to render MUI DataGrid
const columns = [
{ field: "id", headerName: "ID", width: 70 },
{ field: "name", headerName: "Name", width: 200 },
{
field: "gender",
headerName: "Gender",
width: 150
},
{
field: "email",
headerName: "Email",
description: "This column has a value getter and is not sortable.",
width: 250
},
{
field: "action",
headerName: "Action",
width: 250,
// Important: passing id from customers state so I can delete or edit each user
renderCell: (id) => (
<>
<Button
style={{
backgroundColor: "#ffcc00",
marginRight: 40,
padding: "3px 35px"
}}
variant="contained"
color="primary"
type="submit"
>
Edit
</Button>
<Button
style={{
backgroundColor: "#e8605d",
padding: "3px 35px"
}}
onClick={() => handleDelete(id)}
variant="contained"
color="primary"
type="submit"
>
Delete
</Button>
</>
)
}
];
const handleClickOpen = () => {
setOpen(true);
};
const handleClose = () => {
setOpen(false);
};
// Issue: not deleting the row from table
const handleDelete = (id) => {
setCustomers(customers.filter((user) => user.id !== id));
console.log(id);
};
const handleSubmit = (e) => {
e.preventDefault();
console.log(e.value);
const newCustomer = {
id: Math.floor(Math.random() * 999),
name: name,
gender: gender,
email: email
};
setCustomers([...customers, newCustomer]);
console.log(`user data is ${newCustomer}`);
console.log(customers);
};
return (
<>
<Grid
container
direction="row"
justify="center"
alignItems="center"
className={classes.listHeader}
>
<Grid item md={8}></Grid>
<Grid item md={4}>
<Button
className={classes.btn}
onClick={handleClickOpen}
variant="contained"
color="primary"
type="submit"
>
Add New Customer
</Button>
<Dialog
disableBackdropClick
open={open}
onClose={handleClose}
aria-labelledby="form-dialog-title"
>
<DialogTitle id="form-dialog-title">Add New Customer</DialogTitle>
<form noValidate onSubmit={handleSubmit}>
<DialogContent>
<TextField
value={name}
onChange={(event) => setName(event.target.value)}
autoFocus
margin="dense"
id="name"
label="Name"
type="text"
fullWidth
/>
<TextField
value={gender}
onChange={(event) => setGender(event.target.value)}
margin="dense"
id="gender"
label="Gender"
type="text"
fullWidth
/>
<TextField
value={email}
onChange={(event) => setEmail(event.target.value)}
margin="dense"
id="email"
label="Email Address"
type="email"
fullWidth
/>
</DialogContent>
<DialogActions>
<Button onClick={handleClose} color="primary">
Cancel
</Button>
<Button onClick={handleClose} color="primary" type="submit">
Add
</Button>
</DialogActions>
</form>
</Dialog>
</Grid>
</Grid>
<div className={classes.customerList}>
<DataGrid
rows={customers}
columns={columns}
pageSize={5}
checkboxSelection={false}
disableSelectionOnClick={true}
/>
</div>
</>
);
};
export default CustomerList;
解决方案
您调用id
的handleDelete
不是 id,而是一个客户对象。用类似的东西替换它
const handleDelete = (clickedUser) => {
setCustomers(customers.filter((user) => user.id !== clickedUser.id));
console.log(clickedUser.id);
};
推荐阅读
- c# - 如何从基础对象数组中的子对象检索数据?
- java - 如何从 javax.lang.model.VariableElement 获取类型和泛型类型?
- java - 共享首选项中的 getBoolean 方法有问题,尽管磁盘上的值已更改,但始终显示为真
- python - 如何匹配选定的二元组列表和标记推文字典列表
- python - 如何按列上的条件更改行值(python,pandas)
- mysql - phpMyAdmin 在不断更新的表上更改存储引擎
- docker - 如何使用 docker swarmn 从我的前端 docker nginx 调用 nodeJs docker 中的后端
- node.js - 有没有办法让我的代码不区分大小写
- c - C程序循环重复两次而不是一次
- go - 将全局和外部上下文作为参数传递给 Hugo 部分模板