英文:
Column Issue in React and Material UI
问题
我有三列,我想添加一列来删除每一行。
我需要在右侧添加一个"删除"列。这可能吗?
我该如何操作?
CODESANDBOX: 点击这里
const CustomTableRow = ({ row, index, arrayHelpers }) => {
return (
<>
<TableRow
sx={{
"th, td": { border: 0 }
}}
>
<TableCell component="th" scope="row">
<FastField
name={`rows.${index}.attribute`}
component={TextField}
fullWidth
/>
</TableCell>
<TableCell>
<FastField
name={`rows.${index}.ruleId`}
component={TextField}
fullWidth
/>
</TableCell>
</TableRow>
<TableRow>
<TableCell colSpan={2}>
<FastField
name={`rows.${index}.thirdRow`}
component={TextField}
fullWidth
/>
</TableCell>
</TableRow>
{/* <TableCell align="right">
<IconButton
aria-label="delete"
onClick={() => arrayHelpers.remove(index)}
size="small"
>
<RemoveCircleOutlineIcon sx={{ fontSize: "1.25rem" }} />
</IconButton>
</TableCell> */}
</>
);
};
英文:
I have three columns and I wanted to add a column for removing each row.
I need to add a column for "removing" on the right. Is it possible?
How will I do it?
CODESANDBOX: CLICK HERE
const CustomTableRow = ({ row, index, arrayHelpers }) => {
return (
<>
<TableRow
sx={{
"th, td": { border: 0 }
}}
>
<TableCell component="th" scope="row">
<FastField
name={`rows.${index}.attribute`}
component={TextField}
fullWidth
/>
</TableCell>
<TableCell>
<FastField
name={`rows.${index}.ruleId`}
component={TextField}
fullWidth
/>
</TableCell>
</TableRow>
<TableRow>
<TableCell colSpan={2}>
<FastField
name={`rows.${index}.thirdRow`}
component={TextField}
fullWidth
/>
</TableCell>
</TableRow>
{/* <TableCell align="right">
<IconButton
aria-label="delete"
onClick={() => arrayHelpers.remove(index)}
size="small"
>
<RemoveCircleOutlineIcon sx={{ fontSize: "1.25rem" }} />
</IconButton>
</TableCell> */}
</>
);
};
答案1
得分: 0
以下是您要翻译的内容:
Table
<Table sx={{ minWidth: 650 }} aria-label="simple table">
<TableHead>
<TableRow
sx={{
th: { border: 0 }
}}
>
<TableCell colSpan={2}>Attribute</TableCell>
<TableCell colSpan={2}>Rule ID</TableCell>
<TableCell />
</TableRow>
<TableRow>
<TableCell colSpan={4}>Third Row</TableCell>
<TableCell />
</TableRow>
</TableHead>
<TableBody>
{values.rows?.map((row, index) => (
<CustomTableRow
key={row.id}
row={row}
index={index}
arrayHelpers={arrayHelpers}
/>
))}
</TableBody>
</Table>
CustomTableRow
const CustomTableRow = ({ row, index, arrayHelpers }) => {
return (
<>
<TableRow
sx={{
"th, td": { border: 0 }
}}
>
<TableCell colSpan={2} component="th" scope="row">
<FastField
name={`rows.${index}.attribute`}
component={TextField}
fullWidth
/>
</TableCell>
<TableCell colSpan={2}>
<FastField
name={`rows.${index}.ruleId`}
component={TextField}
fullWidth
/>
</TableCell>
<TableCell align="center" colSpan={1} rowSpan={2}>
<IconButton
aria-label="delete"
onClick={() => arrayHelpers.remove(index)}
size="small"
>
<RemoveCircleOutlineIcon sx={{ fontSize: "1.25rem" }} />
</IconButton>
</TableCell>
</TableRow>
<TableRow
sx={{
"th, td": { border: 0 }
}}
>
<TableCell colSpan={4}>
<FastField
name={`rows.${index}.thirdRow`}
component={TextField}
fullWidth
/>
</TableCell>
</TableRow>
</>
};
英文:
Just a rough start, but edit the rows to occupy 5 columns of space, 4 for the row data/fields and 1 for the remove button. You can tweak the column spans to adjust the UI to your liking.
Table
<Table sx={{ minWidth: 650 }} aria-label="simple table">
<TableHead>
<TableRow
sx={{
th: { border: 0 }
}}
>
<TableCell colSpan={2}>Attribute</TableCell>
<TableCell colSpan={2}>Rule ID</TableCell>
<TableCell />
</TableRow>
<TableRow>
<TableCell colSpan={4}>Third Row</TableCell>
<TableCell />
</TableRow>
</TableHead>
<TableBody>
{values.rows?.map((row, index) => (
<CustomTableRow
key={row.id}
row={row}
index={index}
arrayHelpers={arrayHelpers}
/>
))}
</TableBody>
</Table>
CustomTableRow
const CustomTableRow = ({ row, index, arrayHelpers }) => {
return (
<>
<TableRow
sx={{
"th, td": { border: 0 }
}}
>
<TableCell colSpan={2} component="th" scope="row">
<FastField
name={`rows.${index}.attribute`}
component={TextField}
fullWidth
/>
</TableCell>
<TableCell colSpan={2}>
<FastField
name={`rows.${index}.ruleId`}
component={TextField}
fullWidth
/>
</TableCell>
<TableCell align="center" colSpan={1} rowSpan={2}>
<IconButton
aria-label="delete"
onClick={() => arrayHelpers.remove(index)}
size="small"
>
<RemoveCircleOutlineIcon sx={{ fontSize: "1.25rem" }} />
</IconButton>
</TableCell>
</TableRow>
<TableRow
sx={{
"th, td": { border: 0 }
}}
>
<TableCell colSpan={4}>
<FastField
name={`rows.${index}.thirdRow`}
component={TextField}
fullWidth
/>
</TableCell>
</TableRow>
</>
);
};
答案2
得分: -1
我认为列问题已经解决,这对你也可能有帮助。请检查下面已更新的文件:
demo.js
import React from "react";
import Table from "@mui/material/Table";
import TableBody from "@mui/material/TableBody";
import TableCell from "@mui/material/TableCell";
import TableContainer from "@mui/material/TableContainer";
import TableHead from "@mui/material/TableHead";
import TableRow from "@mui/material/TableRow";
import Paper from "@mui/material/Paper";
import { Box, Button } from "@mui/material";
import AddIcon from "@mui/icons-material/Add";
import { FieldArray, Form, Formik } from "formik";
import CustomTableRow from "./CustomTableRow";
const CustomTable = () => {
const handleSubmit = () => {};
return (
<TableContainer component={Paper}>
<Formik
initialValues={[
{
id: 1,
attribute: "",
ruleId: "",
thirdRow: ""
}
]}
onSubmit={handleSubmit}
>
{({ values }) => (
<Form>
<FieldArray
name="rows"
render={(arrayHelpers) => (
<React.Fragment>
<Box>
<Button
variant="contained"
type="submit"
startIcon={<AddIcon />}
onClick={() =>
arrayHelpers.unshift({
id: Date.now(),
attribute: "",
ruleId: "",
thirdRow: ""
})
}
>
Add New
</Button>
</Box>
<Table sx={{ minWidth: 650 }} aria-label="simple table">
<TableHead>
<TableRow
sx={{
th: { border: 0 }
}}
>
<TableCell>Attribute</TableCell>
<TableCell>Rule ID</TableCell>
<TableCell colSpan={2}>Actions</TableCell>
</TableRow>
</TableHead>
<TableBody>
{values.rows?.map((row, index) => (
<CustomTableRow
key={row.id}
row={row}
index={index}
arrayHelpers={arrayHelpers}
/>
))}
</TableBody>
</Table>
</React.Fragment>
)}
/>
</Form>
)}
</Formik>
</TableContainer>
);
};
export default CustomTable;
CustomTableRow.js
import { TableCell, TableRow, TextField, IconButton } from "@mui/material";
import { FastField } from "formik";
import RemoveCircleOutlineIcon from "@mui/icons-material/RemoveCircleOutline";
import { Button } from "@mui/material";
const CustomTableRow = ({ row, index, arrayHelpers }) => {
return (
<>
<TableRow
sx={{
"th, td": { border: 0 }
}}
>
<TableCell component="th" scope="row">
<FastField
name={`rows.${index}.attribute`}
component={TextField}
fullWidth
/>
</TableCell>
<TableCell>
<FastField
name={`rows.${index}.ruleId`}
component={TextField}
fullWidth
/>
</TableCell>
<TableCell>
<FastField
name={`rows.${index}.ruleId`}
component={TextField}
fullWidth
/>
</TableCell>
</TableRow>
</>
);
};
CustomTableRow.displayName = "CustomTableRow";
export default CustomTableRow;
英文:
I think Column issue is solved and it might be helpful to you as well
Please check the file which have been updated below :
demo.js
import React from "react";
import Table from "@mui/material/Table";
import TableBody from "@mui/material/TableBody";
import TableCell from "@mui/material/TableCell";
import TableContainer from "@mui/material/TableContainer";
import TableHead from "@mui/material/TableHead";
import TableRow from "@mui/material/TableRow";
import Paper from "@mui/material/Paper";
import { Box, Button } from "@mui/material";
import AddIcon from "@mui/icons-material/Add";
import { FieldArray, Form, Formik } from "formik";
import CustomTableRow from "./CustomTableRow";
const CustomTable = () => {
const handleSubmit = () => {};
return (
<TableContainer component={Paper}>
<Formik
initialValues={[
{
id: 1,
attribute: "",
ruleId: "",
thirdRow: ""
}
]}
onSubmit={handleSubmit}
>
{({ values }) => (
<Form>
<FieldArray
name="rows"
render={(arrayHelpers) => (
<React.Fragment>
<Box>
<Button
variant="contained"
type="submit"
startIcon={<AddIcon />}
onClick={() =>
arrayHelpers.unshift({
id: Date.now(),
attribute: "",
ruleId: "",
thirdRow: ""
})
}
>
Add New
</Button>
</Box>
<Table sx={{ minWidth: 650 }} aria-label="simple table">
<TableHead>
<TableRow
sx={{
th: { border: 0 }
}}
>
<TableCell>Attribute</TableCell>
<TableCell>Rule ID</TableCell>
<TableCell colSpan={2}>Actions</TableCell>
</TableRow>
</TableHead>
<TableBody>
{values.rows?.map((row, index) => (
<CustomTableRow
key={row.id}
row={row}
index={index}
arrayHelpers={arrayHelpers}
/>
))}
</TableBody>
</Table>
</React.Fragment>
)}
/>
</Form>
)}
</Formik>
</TableContainer>
);
};
export default CustomTable;
CustomTableRow.js
import { TableCell, TableRow, TextField, IconButton } from "@mui/material";
import { FastField } from "formik";
import RemoveCircleOutlineIcon from "@mui/icons-material/RemoveCircleOutline";
import { Button } from "@mui/material";
const CustomTableRow = ({ row, index, arrayHelpers }) => {
return (
<>
<TableRow
sx={{
"th, td": { border: 0 }
}}
>
<TableCell component="th" scope="row">
<FastField
name={`rows.${index}.attribute`}
component={TextField}
fullWidth
/>
</TableCell>
<TableCell>
<FastField
name={`rows.${index}.ruleId`}
component={TextField}
fullWidth
/>
</TableCell>
<TableCell>
<FastField
name={`rows.${index}.ruleId`}
component={TextField}
fullWidth
/>
</TableCell>
</TableRow>
</>
);
};
CustomTableRow.displayName = "CustomTableRow";
export default CustomTableRow;
答案3
得分: -3
检查你的导入:验证已加载适当的Material UI组件。你应该集成Grid组件来处理列。
验证你的代码:确保Grid组件的Props和语法是准确的。确保xs、sm、md、lg和xl的Props加起来等于12。Grid组件使用12列布局。
import { Grid } from '@material-ui/core';
function App() {
return (
<Grid container spacing={2}>
<Grid item xs={12} md={6}>
{/* 左列内容 */}
</Grid>
<Grid item xs={12} md={6}>
{/* 右列内容 */}
</Grid>
</Grid>
);
}
确保没有与Material UI设计冲突的CSS样式,通过检查冲突来避免。并确保使用总成本有效日期,以及使用总成本有效日期,以及使用总成本有效日期,以及使用总成本有效日期,以及使用。
英文:
Look over your imports: Verify that the appropriate Material UI components have been loaded. You should integrate the Grid component for columns.
Verify your code: Verify that the Props and Syntax for the Grid component are accurate. Make sure that the xs, sm, md, lg, and xl props add up to 12. The Grid component utilises a 12-column layout.
import { Grid } from '@material-ui/core';
function App() {
return (
<Grid container spacing={2}>
<Grid item xs={12} md={6}>
{/* Left column content */}
</Grid>
<Grid item xs={12} md={6}>
{/* Right column content */}
</Grid>
</Grid>
);
}
Make sure there are no CSS styles that clash with the Material UI designs by checking for conflicts. The And'so make use the total cost effective date and'so make use the total cost-effective date and so make use the total cost effective date and'so make use the total cost effective date and'so make use the
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论