我正在尝试使用材料 UI 最小化表来创建仪表板 https://material-ui.com/pt/components/tables/
我遇到了一个问题,我在过去 5 小时内一直被困。我可以成功地将数据库中的数据呈现在表中。但是,当我使用 map 函数检索数据时,所有内容都显示在一行 x5 中。
我试图在不同的行中显示名称和数据,而不是“Sue Flavio John Doe rajid”,因为每行都是不同的对象。
如果您能帮助我了解我的逻辑错误在哪里,我将不胜感激。
select distinct jobid from appskills apps inner join applicant app on apps.appid=app.appid
inner join jobskills js on apps.skillid=js.skillid where app.name='Monica'
and not exists (select jobid from jobskills j where js.jobid=j.jobid and js.skillid<>j.skillid)
答案 0 :(得分:0)
您应该使用的结构应如下所示:
function Row(props) {
// you can access the employee object as `props.employee`
...
}
...
{employeeList.map((val, key) => <Row key={key} employee={val} />}
每次调用 row 函数时,它都会为您提供一个新行。为了让它知道该行中应该包含哪些数据,您应该将该数据作为道具传递,而不是使用状态。因此,数据流是 render 方法有一个员工列表,它对每个员工调用一次行构建器函数,返回一个要渲染的行列表。
答案 1 :(得分:0)
好吧,您没有将数据提供给迭代员工的 map
调用中的每一行。然后,你在每个单元格中再次迭代整个事情..
这应该有效:
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>
);