【发布时间】:2021-09-19 14:39:41
【问题描述】:
我正在尝试使用 MateialUI 制作一个可折叠的表格目前,我的幻灯片都有折叠但它们链接到一个“打开”状态,所以如果我打开一张幻灯片,所有其他幻灯片都会打开。
这里是一个 sansbox:https://codesandbox.io/s/withered-bash-e1mzu?file=/src/components/BrandTable.js
props 对象数组需要为我拥有的每张幻灯片提供一个可折叠的表格。
const props = [
{slide: "Protein", charts: ["Keto"]},
{slide: "Bars with benefits - beyond the nutritional profile", charts: ["Innovative approaches"]},
{slide: "Vegan", charts: []}
]
return (
<div>
{
props.map((blog) =>
<React.Fragment>
<TableRow key={props} >
<TableCell component="th" scope="row">
{blog.slide}
</TableCell>
<TableCell size="small">
<IconButton aria-label="expand row" size="small" onClick={() => setOpen(!open)}>
{open ? <KeyboardArrowUpIcon /> : <KeyboardArrowDownIcon/>}
</IconButton>
</TableCell>
</TableRow>
<TableRow>
<TableCell style={{ paddingBottom: 0, paddingTop: 0 }} colSpan={6}>
<Collapse in={open} timeout="auto" unmountOnExit>
{
open && <Box margin={1}>
<Table size="small" aria-label="purchases">
<TableBody>
{blog.charts}
</TableBody>
</Table>
</Box>
}
</Collapse>
</TableCell>
</TableRow>
}
</React.Fragment>
)
}
</div>
)
}
【问题讨论】:
-
将迭代移出
Row组件[或] 使用行ID 来跟踪打开/关闭状态。
标签: javascript reactjs material-ui