![](/img/trans.png)
[英]How to expand and collapse the table rows in plain react js. Show only one row if the description of the row is multiple and show the expand button
[英]In materia-ui table. When I click the button to expand the row, it expands all rows. How can i make only selected row will expand?
Row Click事件展開數據表中的所有行顯示所有行的內部數據
展開按鈕單擊展開所有行而不是選定行。 每次我單擊按鈕展開第 1 行時,它也會展開第 2 行。 我該如何解決這個問題,只有特定的行會擴展?
<Table stickyHeader aria-label="sticky table"> <TableHead> <TableRow hover> <TableCell>Title</TableCell> <TableCell>Created Date</TableCell> </TableRow> </TableHead> <TableBody> {data && data((index) => ( <TableRow hover> <TableCell> <IconButton aria-label="expand row" size="small" onClick={() => setOpen(!open)} > {open ? ( <KeyboardArrowUpIcon /> ) : ( <KeyboardArrowDownIcon /> )} </IconButton> </TableCell> <TableCell component="th" scope="row"> {index.title} </TableCell> <TableCell component="th" scope="row"> {new Date( index.createdDate.seconds * 1000 ).toDateString()} </TableCell> <TableCell colSpan={6}> <Collapse in={open} timeout="auto" unmountOnExit> {index.text} </Collapse> </TableCell> </TableRow> ))} </TableBody> </Table>
我能夠將索引添加到打開功能並只允許選定的行擴展。 它按預期工作,下面是我修改后的代碼
<Table stickyHeader aria-label="sticky table"> <TableHead> <TableRow hover> <TableCell>Title</TableCell> <TableCell>Created Date</TableCell> </TableRow> </TableHead> <TableBody> {data && data((index) => ( <TableRow hover> <TableCell> <IconButton aria-label="expand row" size="small" onClick={() => setOpen(open === index ? -1 : index)} > {open == index ? ( <KeyboardArrowUpIcon /> ) : ( <KeyboardArrowDownIcon /> )} </IconButton> </TableCell> <TableCell component="th" scope="row"> {index.title} </TableCell> <TableCell component="th" scope="row"> {new Date( index.createdDate.seconds * 1000 ).toDateString()} </TableCell> <TableCell colSpan={6}> <Collapse in={open == index} timeout="auto" unmountOnExit> {index.text} </Collapse> </TableCell> </TableRow> ))} </TableBody> </Table>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.