简体   繁体   English

ReactJS表带材料ui

[英]ReactJS table with material ui

I am trying to use the material UI minimized table to create a dashboard https://material-ui.com/pt/components/tables/我正在尝试使用材料 UI 最小化表来创建仪表板https://material-ui.com/pt/components/tables/

I ran into an issue that I'm stuck with for the last 5 hours.在过去的 5 个小时里,我遇到了一个问题。 I could successfully render the data from my database to display in the table.我可以成功地渲染数据库中的数据以显示在表格中。 However, when I retrieve the data using the map function, all is displayed in one single row x5.但是,当我使用 map function 检索数据时,所有数据都显示在一行 x5 中。

Picture: Front-end view图片:前端视图

I'm trying to display the name and data in different rows and not "Sue Flavio John Doe rajid" because each is a different object.我试图在不同的行中显示名称和数据,而不是“Sue Flavio John Doe rajid”,因为每个都是不同的 object。

I'd appreciate some help to understand where is my logical mistake.我很感激一些帮助,以了解我的逻辑错误在哪里。

function EmployeeList() {
  const [employeeList, setEmployeeList] = useState([]);

  console.log(employeeList);

  useEffect(() => {
    Axios.get("http://localhost:4000/vacations_cs_read").then((response) => {
      setEmployeeList(response.data);
    });
  }, []);

  const Row = () => {
    const [open, setOpen] = React.useState(false);
    const classes = useRowStyles();

    return (
      <React.Fragment>
        <TableRow className={classes.root}>
          <TableCell>
            <IconButton
              aria-label="expand row"
              size="small"
              onClick={() => setOpen(!open)}
            >
              {open ? <KeyboardArrowUpIcon /> : <KeyboardArrowDownIcon />}
            </IconButton>
          </TableCell>
          <TableCell component="th" scope="row">
            {employeeList.map((val, key) => {
              return <td>{val.employeeName}</td>;
            })}
          </TableCell>
          <TableCell align="left">
            {employeeList.map((val, key) => {
              const day = new Date(val.employeeStartDay).toLocaleDateString(
                "pt-BR"
              );
              return <td>{day}</td>;
            })}
          </TableCell>
          <TableCell align="left">
            <p>-</p>
          </TableCell>
          <TableCell align="left">
            <p>-</p>
          </TableCell>
          <TableCell align="left">
            <p>-</p>
          </TableCell>
        </TableRow>
        <TableRow>
          <TableCell style={{ paddingBottom: 0, paddingTop: 0 }} colSpan={6}>
            <Collapse in={open} timeout="auto" unmountOnExit>
              <Box margin={1}>
                <Typography variant="h6" gutterBottom component="div">
                  History
                </Typography>
                <Table size="small" aria-label="purchases">
                  <TableHead>
                    <TableRow>
                      <TableCell>From</TableCell>
                      <TableCell>To</TableCell>
                      <TableCell align="left">Days</TableCell>
                      <TableCell align="left">Paid</TableCell>
                    </TableRow>
                  </TableHead>
                  <TableBody>
                    {/* {row.history.map(() => (
                      <TableRow>
                        <TableCell component="th" scope="row"></TableCell>
                        <TableCell></TableCell>
                        <TableCell align="left"></TableCell>
                        <TableCell align="left"></TableCell>
                      </TableRow>
                    ))} */}
                  </TableBody>
                </Table>
              </Box>
            </Collapse>
          </TableCell>
        </TableRow>
      </React.Fragment>
    );
  };

  Row.propTypes = {
    row: PropTypes.shape({
      name: PropTypes.string,
      startDate: PropTypes.string,
      shift: PropTypes.string,
      daysReceived: PropTypes.number,
      daysUsed: PropTypes.number,
      daysLeft: PropTypes.number,
      history: PropTypes.arrayOf(
        PropTypes.shape({
          amount: PropTypes.number,
          customerId: PropTypes.string,
          date: PropTypes.string,
        })
      ),
    }),
  };

  return (
    <div>
      <TableContainer component={Paper}>
        <Table aria-label="collapsible table">
          <TableHead>
            <TableRow>
              <TableCell />
              <TableCell>Name</TableCell>
              <TableCell align="left">Employment Date</TableCell>
              <TableCell align="left">Days received</TableCell>
              <TableCell align="left">Days used</TableCell>
              <TableCell align="left">Days left</TableCell>
            </TableRow>
          </TableHead>
          <TableBody>
            {employeeList.map((val, key) => (
              <Row />
            ))}
          </TableBody>
        </Table>
      </TableContainer>
    </div>
  );
}

The structure you should be using should look something like this:您应该使用的结构应如下所示:

function Row(props) {
   // you can access the employee object as `props.employee`
   ...
}

...

{employeeList.map((val, key) => <Row key={key} employee={val} />}

Each time you call the row function, it gives you a new row.每次调用 function 行时,它都会为您提供一个新行。 For it to know what data should be in that row, you should be passing that data as a prop, instead of using the state.要让它知道该行中应该包含哪些数据,您应该将该数据作为道具传递,而不是使用 state。 The flow of data would therefore be that the render method has a list of employees, and it calls the row builder function once with each of them, which gives back a list of rows to render.因此,数据流将是 render 方法有一个员工列表,并且它对每个员工调用一次行构建器 function,这会返回要渲染的行列表。

Well, you are not giving the data to each row in the map call where you iterate over the employees.好吧,您没有将数据提供给迭代员工的map调用中的每一行。 And then, instead you iterate of the whole thing again in each cell..然后,相反,您在每个单元格中再次迭代整个事情..

This should work:这应该有效:

const Row = ({val}) => {
  const [open, setOpen] = React.useState(false);
  const classes = useRowStyles();
  
  return (
    <React.Fragment>
      <TableRow className={classes.root}>
        <TableCell>
          <IconButton
            aria-label="expand row"
            size="small"
            onClick={() => setOpen(!open)}
          >
            {open ? <KeyboardArrowUpIcon /> : <KeyboardArrowDownIcon />}
          </IconButton>
        </TableCell>
        <TableCell component="th" scope="row">
          {val.employeeName}
        </TableCell>
        <TableCell align="left">
          {(new Date(val.employeeStartDay)).toLocaleDateString("pt-BR")}
        </TableCell>
        <TableCell align="left">
          <p>-</p>
        </TableCell>
        <TableCell align="left">
          <p>-</p>
        </TableCell>
        <TableCell align="left">
          <p>-</p>
        </TableCell>
      </TableRow>
      <TableRow>
        <TableCell style={{ paddingBottom: 0, paddingTop: 0 }} colSpan={6}>
          <Collapse in={open} timeout="auto" unmountOnExit>
            <Box margin={1}>
              <Typography variant="h6" gutterBottom component="div">
                History
              </Typography>
              <Table size="small" aria-label="purchases">
                <TableHead>
                  <TableRow>
                    <TableCell>From</TableCell>
                    <TableCell>To</TableCell>
                    <TableCell align="left">Days</TableCell>
                    <TableCell align="left">Paid</TableCell>
                  </TableRow>
                </TableHead>
                <TableBody>
                  {/* {row.history.map(() => (
                      <TableRow>
                      <TableCell component="th" scope="row"></TableCell>
                      <TableCell></TableCell>
                      <TableCell align="left"></TableCell>
                      <TableCell align="left"></TableCell>
                      </TableRow>
                    ))} */}
                </TableBody>
              </Table>
            </Box>
          </Collapse>
        </TableCell>
      </TableRow>
    </React.Fragment>
  );
};

Row.propTypes = {
  row: PropTypes.shape({
    name: PropTypes.string,
    startDate: PropTypes.string,
    shift: PropTypes.string,
    daysReceived: PropTypes.number,
    daysUsed: PropTypes.number,
    daysLeft: PropTypes.number,
    history: PropTypes.arrayOf(
      PropTypes.shape({
        amount: PropTypes.number,
        customerId: PropTypes.string,
        date: PropTypes.string,
      })
    ),
  }),
};

return (
  <div>
    <TableContainer component={Paper}>
      <Table aria-label="collapsible table">
        <TableHead>
          <TableRow>
            <TableCell />
            <TableCell>Name</TableCell>
            <TableCell align="left">Employment Date</TableCell>
            <TableCell align="left">Days received</TableCell>
            <TableCell align="left">Days used</TableCell>
            <TableCell align="left">Days left</TableCell>
          </TableRow>
        </TableHead>
        <TableBody>
          {employeeList.map((val, key) => <Row val={val} key={key}/>)}
        </TableBody>
      </Table>
    </TableContainer>
  </div>
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM