React 和 Material UI 中的列问题

huangapple go评论82阅读模式
英文:

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 }) =&gt; {
  return (
    &lt;&gt;
      &lt;TableRow
        sx={{
          &quot;th, td&quot;: { border: 0 }
        }}
      &gt;
        &lt;TableCell component=&quot;th&quot; scope=&quot;row&quot;&gt;
          &lt;FastField
            name={`rows.${index}.attribute`}
            component={TextField}
            fullWidth
          /&gt;
        &lt;/TableCell&gt;
        &lt;TableCell&gt;
          &lt;FastField
            name={`rows.${index}.ruleId`}
            component={TextField}
            fullWidth
          /&gt;
        &lt;/TableCell&gt;
      &lt;/TableRow&gt;
      &lt;TableRow&gt;
        &lt;TableCell colSpan={2}&gt;
          &lt;FastField
            name={`rows.${index}.thirdRow`}
            component={TextField}
            fullWidth
          /&gt;
        &lt;/TableCell&gt;
      &lt;/TableRow&gt;
      {/* &lt;TableCell align=&quot;right&quot;&gt;
        &lt;IconButton
          aria-label=&quot;delete&quot;
          onClick={() =&gt; arrayHelpers.remove(index)}
          size=&quot;small&quot;
        &gt;
          &lt;RemoveCircleOutlineIcon sx={{ fontSize: &quot;1.25rem&quot; }} /&gt;
        &lt;/IconButton&gt;
      &lt;/TableCell&gt; */}
    &lt;/&gt;
  );
};

答案1

得分: 0

以下是您要翻译的内容:

Table

&lt;Table sx={{ minWidth: 650 }} aria-label=&quot;simple table&quot;&gt;
  &lt;TableHead&gt;
    &lt;TableRow
      sx={{

th: { border: 0 }
      }}
    &gt;
      &lt;TableCell colSpan={2}&gt;Attribute&lt;/TableCell&gt;
      &lt;TableCell colSpan={2}&gt;Rule ID&lt;/TableCell&gt;
      &lt;TableCell /&gt;
    &lt;/TableRow&gt;
    &lt;TableRow&gt;
      &lt;TableCell colSpan={4}&gt;Third Row&lt;/TableCell&gt;
      &lt;TableCell /&gt;
    &lt;/TableRow&gt;
  &lt;/TableHead&gt;
  &lt;TableBody&gt;
    {values.rows?.map((row, index) =&gt; (
      &lt;CustomTableRow
        key={row.id}
        row={row}
        index={index}
        arrayHelpers={arrayHelpers}
      /&gt;
    ))}
  &lt;/TableBody&gt;
&lt;/Table&gt;

CustomTableRow

const CustomTableRow = ({ row, index, arrayHelpers }) =&gt; {
  return (
    &lt;&gt;
      &lt;TableRow
        sx={{
          &quot;th, td&quot;: { border: 0 }
        }}
      &gt;
      &lt;TableCell colSpan={2} component=&quot;th&quot; scope=&quot;row&quot;&gt;
        &lt;FastField
          name={`rows.${index}.attribute`}
          component={TextField}
          fullWidth
        /&gt;
      &lt;/TableCell&gt;
      &lt;TableCell colSpan={2}&gt;
        &lt;FastField
          name={`rows.${index}.ruleId`}
          component={TextField}
          fullWidth
        /&gt;
      &lt;/TableCell&gt;
      &lt;TableCell align=&quot;center&quot; colSpan={1} rowSpan={2}&gt;
        &lt;IconButton
          aria-label=&quot;delete&quot;
          onClick={() =&gt; arrayHelpers.remove(index)}
          size=&quot;small&quot;
        &gt;
          &lt;RemoveCircleOutlineIcon sx={{ fontSize: &quot;1.25rem&quot; }} /&gt;
        &lt;/IconButton&gt;
      &lt;/TableCell&gt;
    &lt;/TableRow&gt;
    &lt;TableRow
      sx={{
        &quot;th, td&quot;: { border: 0 }
      }}
    &gt;
    &lt;TableCell colSpan={4}&gt;
      &lt;FastField
        name={`rows.${index}.thirdRow`}
        component={TextField}
        fullWidth
      /&gt;
    &lt;/TableCell&gt;
  &lt;/TableRow&gt;
&lt;/&gt;
};

React 和 Material UI 中的列问题

英文:

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

&lt;Table sx={{ minWidth: 650 }} aria-label=&quot;simple table&quot;&gt;
  &lt;TableHead&gt;
    &lt;TableRow
      sx={{
        th: { border: 0 }
      }}
    &gt;
      &lt;TableCell colSpan={2}&gt;Attribute&lt;/TableCell&gt;
      &lt;TableCell colSpan={2}&gt;Rule ID&lt;/TableCell&gt;
      &lt;TableCell /&gt;
    &lt;/TableRow&gt;
    &lt;TableRow&gt;
      &lt;TableCell colSpan={4}&gt;Third Row&lt;/TableCell&gt;
      &lt;TableCell /&gt;
    &lt;/TableRow&gt;
  &lt;/TableHead&gt;
  &lt;TableBody&gt;
    {values.rows?.map((row, index) =&gt; (
      &lt;CustomTableRow
        key={row.id}
        row={row}
        index={index}
        arrayHelpers={arrayHelpers}
      /&gt;
    ))}
  &lt;/TableBody&gt;
&lt;/Table&gt;

CustomTableRow

const CustomTableRow = ({ row, index, arrayHelpers }) =&gt; {
  return (
    &lt;&gt;
      &lt;TableRow
        sx={{
          &quot;th, td&quot;: { border: 0 }
        }}
      &gt;
        &lt;TableCell colSpan={2} component=&quot;th&quot; scope=&quot;row&quot;&gt;
          &lt;FastField
            name={`rows.${index}.attribute`}
            component={TextField}
            fullWidth
          /&gt;
        &lt;/TableCell&gt;
        &lt;TableCell colSpan={2}&gt;
          &lt;FastField
            name={`rows.${index}.ruleId`}
            component={TextField}
            fullWidth
          /&gt;
        &lt;/TableCell&gt;
        &lt;TableCell align=&quot;center&quot; colSpan={1} rowSpan={2}&gt;
          &lt;IconButton
            aria-label=&quot;delete&quot;
            onClick={() =&gt; arrayHelpers.remove(index)}
            size=&quot;small&quot;
          &gt;
            &lt;RemoveCircleOutlineIcon sx={{ fontSize: &quot;1.25rem&quot; }} /&gt;
          &lt;/IconButton&gt;
        &lt;/TableCell&gt;
      &lt;/TableRow&gt;
      &lt;TableRow
        sx={{
          &quot;th, td&quot;: { border: 0 }
        }}
      &gt;
        &lt;TableCell colSpan={4}&gt;
          &lt;FastField
            name={`rows.${index}.thirdRow`}
            component={TextField}
            fullWidth
          /&gt;
        &lt;/TableCell&gt;
      &lt;/TableRow&gt;
    &lt;/&gt;
  );
};

React 和 Material UI 中的列问题

答案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 &quot;react&quot;;
import Table from &quot;@mui/material/Table&quot;;
import TableBody from &quot;@mui/material/TableBody&quot;;
import TableCell from &quot;@mui/material/TableCell&quot;;
import TableContainer from &quot;@mui/material/TableContainer&quot;;
import TableHead from &quot;@mui/material/TableHead&quot;;
import TableRow from &quot;@mui/material/TableRow&quot;;
import Paper from &quot;@mui/material/Paper&quot;;
import { Box, Button } from &quot;@mui/material&quot;;
import AddIcon from &quot;@mui/icons-material/Add&quot;;
import { FieldArray, Form, Formik } from &quot;formik&quot;;
import CustomTableRow from &quot;./CustomTableRow&quot;;
const CustomTable = () =&gt; {
const handleSubmit = () =&gt; {};
return (
&lt;TableContainer component={Paper}&gt;
&lt;Formik
initialValues={[
{
id: 1,
attribute: &quot;&quot;,
ruleId: &quot;&quot;,
thirdRow: &quot;&quot;
}
]}
onSubmit={handleSubmit}
&gt;
{({ values }) =&gt; (
&lt;Form&gt;
&lt;FieldArray
name=&quot;rows&quot;
render={(arrayHelpers) =&gt; (
&lt;React.Fragment&gt;
&lt;Box&gt;
&lt;Button
variant=&quot;contained&quot;
type=&quot;submit&quot;
startIcon={&lt;AddIcon /&gt;}
onClick={() =&gt;
arrayHelpers.unshift({
id: Date.now(),
attribute: &quot;&quot;,
ruleId: &quot;&quot;,
thirdRow: &quot;&quot;
})
}
&gt;
Add New
&lt;/Button&gt;
&lt;/Box&gt;
&lt;Table sx={{ minWidth: 650 }} aria-label=&quot;simple table&quot;&gt;
&lt;TableHead&gt;
&lt;TableRow
sx={{
th: { border: 0 }
}}
&gt;
&lt;TableCell&gt;Attribute&lt;/TableCell&gt;
&lt;TableCell&gt;Rule ID&lt;/TableCell&gt;
&lt;TableCell colSpan={2}&gt;Actions&lt;/TableCell&gt;
&lt;/TableRow&gt;
&lt;/TableHead&gt;
&lt;TableBody&gt;
{values.rows?.map((row, index) =&gt; (
&lt;CustomTableRow
key={row.id}
row={row}
index={index}
arrayHelpers={arrayHelpers}
/&gt;
))}
&lt;/TableBody&gt;
&lt;/Table&gt;
&lt;/React.Fragment&gt;
)}
/&gt;
&lt;/Form&gt;
)}
&lt;/Formik&gt;
&lt;/TableContainer&gt;
);
};
export default CustomTable;

CustomTableRow.js

    import { TableCell, TableRow, TextField, IconButton } from &quot;@mui/material&quot;;
import { FastField } from &quot;formik&quot;;
import RemoveCircleOutlineIcon from &quot;@mui/icons-material/RemoveCircleOutline&quot;;
import { Button } from &quot;@mui/material&quot;;
const CustomTableRow = ({ row, index, arrayHelpers }) =&gt; {
return (
&lt;&gt;
&lt;TableRow
sx={{
&quot;th, td&quot;: { border: 0 }
}}
&gt;
&lt;TableCell component=&quot;th&quot; scope=&quot;row&quot;&gt;
&lt;FastField
name={`rows.${index}.attribute`}
component={TextField}
fullWidth
/&gt;
&lt;/TableCell&gt;
&lt;TableCell&gt;
&lt;FastField
name={`rows.${index}.ruleId`}
component={TextField}
fullWidth
/&gt;
&lt;/TableCell&gt;
&lt;TableCell&gt;
&lt;FastField
name={`rows.${index}.ruleId`}
component={TextField}
fullWidth
/&gt;
&lt;/TableCell&gt;
&lt;/TableRow&gt;
&lt;/&gt;
);
};
CustomTableRow.displayName = &quot;CustomTableRow&quot;;
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 &#39;@material-ui/core&#39;;
function App() {
return (
&lt;Grid container spacing={2}&gt;
&lt;Grid item xs={12} md={6}&gt;
{/* Left column content */}
&lt;/Grid&gt;
&lt;Grid item xs={12} md={6}&gt;
{/* Right column content */}
&lt;/Grid&gt;
&lt;/Grid&gt;
);
}

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

huangapple
  • 本文由 发表于 2023年3月1日 14:36:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定