作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个表,其中包含从数据库中获取的学生列表。当我单击每个学生行时,将显示一个模式弹出窗口,其中包含另一个 4 行 3 列的表格。模式中的每一列将包含 4 个复选框。当我单击一个复选框时,我想将该行的状态列从“待定”更改为“已付款”,并将徽章属性类型从“危险”更改为“成功”。问题是,当我单击一个复选框时,它会将所有列的状态从“待定”更改为“已付款”,而不仅仅是更改那一行。
这是我的代码
import {
Table,
TableBody,
TableCell,
Badge,
TableFooter,
TableRow,
TableHeader,
TableContainer,
Input,
Modal,
ModalHeader,
ModalBody,
ModalFooter,
Button,
} from "@windmill/react-ui";
import MaterialTable from "material-table";
import AddBox from "@material-ui/icons/AddBox";
import ArrowDownward from "@material-ui/icons/ArrowDownward";
import Check from "@material-ui/icons/Check";
import ChevronLeft from "@material-ui/icons/ChevronLeft";
import ChevronRight from "@material-ui/icons/ChevronRight";
import Clear from "@material-ui/icons/Clear";
import DeleteOutline from "@material-ui/icons/DeleteOutline";
import Edit from "@material-ui/icons/Edit";
import FilterList from "@material-ui/icons/FilterList";
import FirstPage from "@material-ui/icons/FirstPage";
import LastPage from "@material-ui/icons/LastPage";
import Remove from "@material-ui/icons/Remove";
import SaveAlt from "@material-ui/icons/SaveAlt";
import Search from "@material-ui/icons/Search";
import ViewColumn from "@material-ui/icons/ViewColumn";
import React, { forwardRef, useState } from "react";
const Admin = () => {
const [isModalOpen, setIsModalOpen] = useState(false);
const [id, setId] = useState("");
function openModal(ids) {
setId(ids.studentId);
setIsModalOpen(true);
}
function closeModal() {
setIsModalOpen(false);
}
const tableIcons = {
Add: forwardRef((props, ref) => <AddBox {...props} ref={ref} />),
Check: forwardRef((props, ref) => <Check {...props} ref={ref} />),
Clear: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
Delete: forwardRef((props, ref) => <DeleteOutline {...props} ref={ref} />),
DetailPanel: forwardRef((props, ref) => (
<ChevronRight {...props} ref={ref} />
)),
Edit: forwardRef((props, ref) => <Edit {...props} ref={ref} />),
Export: forwardRef((props, ref) => <SaveAlt {...props} ref={ref} />),
Filter: forwardRef((props, ref) => <FilterList {...props} ref={ref} />),
FirstPage: forwardRef((props, ref) => <FirstPage {...props} ref={ref} />),
LastPage: forwardRef((props, ref) => <LastPage {...props} ref={ref} />),
NextPage: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />),
PreviousPage: forwardRef((props, ref) => (
<ChevronLeft {...props} ref={ref} />
)),
ResetSearch: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
Search: forwardRef((props, ref) => <Search {...props} ref={ref} />),
SortArrow: forwardRef((props, ref) => (
<ArrowDownward {...props} ref={ref} />
)),
ThirdStateCheck: forwardRef((props, ref) => (
<Remove {...props} ref={ref} />
)),
ViewColumn: forwardRef((props, ref) => <ViewColumn {...props} ref={ref} />),
};
const [status, setStatus] = useState("pending");
const [badge, setBadge] = useState("danger");
const handleClick = (e) => {
//Do something if checkbox is clicked
if (e.target.checked) {
setStatus("paid");
setBadge("success");
} else {
setStatus("pending");
setBadge("danger");
}
};
// Table to be rendered in the modal
function renderTable() {
return (
<TableContainer>
<Table>
<TableHeader>
<TableRow>
<TableCell>Dues</TableCell>
<TableCell>Amount</TableCell>
<TableCell>Status</TableCell>
</TableRow>
</TableHeader>
<TableBody>
<TableRow>
<TableCell>
<div className='flex items-center text-sm'>
<span className='font-semibold ml-2'>Level 100</span>
</div>
</TableCell>
<TableCell>
<span className='text-sm'>100</span>
</TableCell>
<TableCell>
<Badge type={badge}>{status}</Badge>
<Input
type='checkbox'
className='ml-6'
onClick={(e) => handleClick(e)}
/>
</TableCell>
</TableRow>
<TableRow>
<TableCell>
<div className='flex items-center text-sm'>
<span className='font-semibold ml-2'>Level 200</span>
</div>
</TableCell>
<TableCell>
<span className='text-sm'>100</span>
</TableCell>
<TableCell>
<Badge type={badge}>{status}</Badge>
<Input
type='checkbox'
className='ml-6'
onClick={(e) => handleClick(e)}
/>
</TableCell>
</TableRow>
<TableRow>
<TableCell>
<div className='flex items-center text-sm'>
<span className='font-semibold ml-2'>Level 300</span>
</div>
</TableCell>
<TableCell>
<span className='text-sm'>100</span>
</TableCell>
<TableCell>
<Badge type={badge}>{status}</Badge>
<Input
type='checkbox'
className='ml-6'
onClick={(e) => handleClick(e)}
/>
</TableCell>
</TableRow>
<TableRow>
<TableCell>
<div className='flex items-center text-sm'>
<span className='font-semibold ml-2'>Level 400</span>
</div>
</TableCell>
<TableCell>
<span className='text-sm'>100</span>
</TableCell>
<TableCell>
<Badge type={badge}>{status}</Badge>
<Input
type='checkbox'
className='ml-6'
onClick={(e) => handleClick(e)}
/>
</TableCell>
</TableRow>
</TableBody>
</Table>
<TableFooter></TableFooter>
</TableContainer>
);
}
return (
<React.Fragment>
{/* Modal popup */}
<Modal isOpen={isModalOpen} onClose={closeModal}>
<ModalHeader>{id}</ModalHeader>
<ModalBody>{renderTable()}</ModalBody>
<ModalFooter>
<Button
className='w-full sm:w-auto'
layout='outline'
onClick={closeModal}>
Cancel
</Button>
<Button className='w-full sm:w-auto'>Accept</Button>
</ModalFooter>
</Modal>
<MaterialTable
icons={tableIcons}
columns={[
{ title: "Student ID", field: "studentId" },
{ title: "Level", field: "level" },
{ title: "Programme", field: "programme" },
]}
data={[
{
studentId: "UG0222021",
level: "200",
programme: "BCOM",
},
{
studentId: "UG0199821",
level: "200",
programme: "BCOM",
},
]}
title='Students Information'
onRowClick={(event, rowData) => openModal(rowData)}
/>
</React.Fragment>
);
};
export default Admin;
最佳答案
这是一种更通用的方法,我冒昧地使用 typescript ,因为它更容易显示正在发生的事情。
const initialStatuses = {
inputLevel100: "pending",
inputLevel200: "pending",
inputLevel300: "pending",
inputLevel400: "pending",
}
const initialBadges = {
inputLevel100: "danger",
inputLevel200: "danger",
inputLevel300: "danger",
inputLevel400: "danger",
}
const [status, setStatus] = React.useState<{ [key: string]: string }>(initialBadges);
const [badge, setBadge] = React.useState<{ [key: string]: string }>(initialStatuses);
const handleClick = (e: { target: { checked: boolean; name: string} }) => {
const name = e.target.name;
if (e.target.checked) {
setBadge({
...badge,
[name]: "success",
});
setStatus({
...status,
[name]: "paid",
});
}
};
您只需说出即可获得状态或徽章的值
status["Level200"] // Should return either "pending" or "paid"
或
badge["Level200"] // Should return either "danger" or "success"
这应该非常高效(请参阅 Performance of key lookup in JavaScript object 了解更多信息)
但是,更重要的是,您现在有了一种通用的方法来添加更多状态和徽章,而不必担心为每个新添加的状态或徽章添加 useState
此外,这种方法的另一个额外优势是您可以(比方说)从远程 API 获取状态和徽章列表,或者(比方说)有一个包含列表的 statuses.js
文件客户端的状态和一个 badges.js
文件,然后您可以将其映射。这将使您的代码更易于扩展,并且删除/添加新的状态和徽章变得微不足道
关于javascript - 如何检查 reactjs 中单个复选框的状态( Material 表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70283927/
我是一名优秀的程序员,十分优秀!