首页 > 解决方案 > 当我在 Material-ui 表中渲染一个按钮时,该按钮不可点击

问题描述

我的桌子看起来像这样:

            <TableContainer component={Paper} style={{height: "40vh", width: "90vh"}}>
                <Table size="small" sx={{ minWidth: 200 }}>
                    <TableHead>
                        <TableRow>
                            <TableCell align="center" width="90"></TableCell>
                            {consequences_description.map((description) => (
                                <TableCell align="center" width="90">{description}</TableCell>
                                )
                            )}
                        </TableRow>
                    </TableHead>
                    <TableBody>    
    
                        {risk_matrix.map((row, row_index) => (
                        <TableRow
                            key={row_index}
                            sx={{ '&:last-child td, &:last-child th': { border: 0 } }}
                        >
                            <TableCell component="th" scope="row">
                                {likelyhood_description[row_index]}
                            </TableCell>
                            {row.map( (column, column_index) => (
                                <TableCell align="center">
                                    <ToggleButton 
                                        risk={column}
                                        row_index={row_index}
                                        column_index={column_index}
                                        />
                                </TableCell>
                            ))}
                        </TableRow>
                        ))}
    
                    </TableBody>
                </Table>
            </TableContainer>

表内的按钮被调用<ToggleButton/>,它看起来像这样:

        <ThemeProvider theme={filtersTheme}>    
          <Button variant="contained" color={handleColor()} onClick={console.log("Clicked")} >{risk}</Button>
        </ThemeProvider>

表格一呈现,控制台就会显示“已单击”,这意味着一旦按钮由于某种原因呈现在表格内,就会调用 onClick 函数,但我无法单击它并触发 onClick 函数。有没有办法做到这一点?

标签: javascriptreactjsmaterial-uijsx

解决方案


尝试像这样调用 onClick 函数:

onClick={() => { console.log('Clicked');

推荐阅读