简体   繁体   English

材料表:如何获取按钮点击时的行数据? (不在行选择上)

[英]Material Table: How to get the row data on Button click? (Not on row selection)

In my React component, I have a view like this:在我的 React 组件中,我有一个这样的视图:

在此处输入图像描述

This is a material table.这是一张材料表。 When clicking on the delete icon, I would like to get the rowdata.单击删除图标时,我想获取行数据。

Unfortunately I only get an event and it doesnt contain the needed information.不幸的是,我只收到一个事件,它不包含所需的信息。

This is the relevant code:这是相关代码:

class AllBooks extends React.Component {

    onDeleteButtonClick = (event, row) => {
        console.log('onDeleteButton Click!');
        console.log(event);
    }

    render() {
        return (

            <div>
                <Grid container spacing={3}>
                    <Grid item xs={6} sm={3}></Grid>
                    
                    <Grid item xs={6} sm={6}>
                        <h1 style={{textAlign: 'center'}}>All Books</h1>
                        <br /><br />

                        <TableContainer component={Paper}>
                            <Table  aria-label="simple table">
                                <TableHead>
                                    <TableRow>
                                        <TableCell>Dessert (100g serving)</TableCell>
                                        <TableCell align="right">Calories</TableCell>
                                        <TableCell align="right">Fat&nbsp;(g)</TableCell>
                                        <TableCell align="right">Carbs&nbsp;(g)</TableCell>
                                        <TableCell align="right">Protein&nbsp;(g)</TableCell>
                                        <TableCell align="center">Actions</TableCell>
                                    </TableRow>
                                </TableHead>

                                <TableBody>
                                    {rows.map((row) => (
                                        <TableRow key={row.name}>
                                            <TableCell component="th" scope="row">
                                                {row.name}
                                            </TableCell>
                                            <TableCell align="right">{row.calories}</TableCell>
                                            <TableCell align="right">{row.fat}</TableCell>
                                            <TableCell align="right">{row.carbs}</TableCell>
                                            <TableCell align="right">{row.protein}</TableCell>
                                            <TableCell align="center">
                                        
                                                    <IconButton onClick={this.onDeleteButtonClick}>
                                                        <DeleteIcon style={{ color: 'red' }}/>
                                                    </IconButton>
                                                    <IconButton>
                                                        <AddCircleOutlineIcon />
                                                    </IconButton>

                                            </TableCell>
                                        </TableRow>
                                    ))}
                                </TableBody>
                            </Table>
                        </TableContainer>
                    </Grid>

                    <Grid item xs={6} sm={3}></Grid>
                </Grid>
            </div>
    )
    }

}

What can I do to get the row data?我该怎么做才能获取行数据? Thank you very much for every help!非常感谢您的帮助!

Pass your row object in your delete call back inside JSX like this像这样在 JSX 内部的删除回调中传递你的行 object

 <IconButton onClick={() => this.onDeleteButtonClick(row) }>
     <DeleteIcon style={{ color: 'red' }}/>
 </IconButton>

alternatively, you can use bind as well,或者,您也可以使用绑定,

<IconButton onClick={this.onDeleteButtonClick.bind(this, row)}>
     <DeleteIcon style={{ color: 'red' }}/>
 </IconButton>

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

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