简体   繁体   中英

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:

在此处输入图像描述

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

 <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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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