1 回答
TA贡献1828条经验 获得超13个赞
你需要使DataTable.js文件像,
import React, { Fragment, useState } from "react";
import Table from "@material-ui/core/Table";
import TableHead from "@material-ui/core/TableHead";
import TableBody from "@material-ui/core/TableBody";
import TableCell from "@material-ui/core/TableCell";
import TableRow from "@material-ui/core/TableRow";
import Button from "@material-ui/core/Button";
export default function DataTable() {
const [rowCount, setRowCount] = useState("");
const [colCount, setColCount] = useState("");
const [rowCountArray, setRowCountArray] = useState([]);
const [colCountArray, setColCountArray] = useState([]);
const [showTable, setShowTable] = useState(false);
const CreateTable = () => {
rowCountArray.length = 0;
colCountArray.length = 0;
for (let i = 1; i <= rowCount; i++) {
rowCountArray.push(i);
}
setRowCountArray(rowCountArray);
for (let i = 1; i <= colCount; i++) {
colCountArray.push(i);
}
setColCountArray(colCountArray);
setShowTable(true);
};
return (
<>
<Table>
<TableHead>
<TableRow>
<TableCell>Row Count</TableCell>
<TableCell>Col Count</TableCell>
<TableCell></TableCell>
</TableRow>
</TableHead>
<TableBody>
<TableRow>
<TableCell component="th" scope="row">
<input
type="text"
id="txtrows"
value={rowCount}
onChange={(e) => setRowCount(e.target.value)}
/>
</TableCell>
<TableCell component="th" scope="row">
<input
type="text"
id="txtcols"
value={colCount}
onChange={(e) => setColCount(e.target.value)}
/>
</TableCell>
<TableCell>
<Button variant="contained" color="primary" onClick={CreateTable}>
Create Table
</Button>
</TableCell>
</TableRow>
</TableBody>
</Table>
{showTable ? (
<Table>
<TableBody>
{rowCountArray.map((row, index) => (
<TableRow key={index}>
{colCountArray.map((col, index) => (
<TableCell key={index}>
Row {row} - Col {col}
</TableCell>
))}
</TableRow>
))}
</TableBody>
</Table>
) : null}
</>
);
}
单击“创建表”按钮,您可以创建一个函数并根据输入的计数生成一个rowCount数组colCount,
const CreateTable = () => {
rowCountArray.length = 0;
colCountArray.length = 0;
for (let i = 1; i <= rowCount; i++) {
rowCountArray.push(i);
}
setRowCountArray(rowCountArray);
for (let i = 1; i <= colCount; i++) {
colCountArray.push(i);
}
setColCountArray(colCountArray);
setShowTable(true);
};
然后使用以下方法,您可以生成具有相应行和列的表,
{rowCountArray.map((row, index) => (
<TableRow key={index}>
{colCountArray.map((col, index) => (
<TableCell key={index}>
Row {row} - Col {col}
</TableCell>
))}
</TableRow>
))}
添加回答
举报