I have material UI Table in my react component. I want to add onCellClick event to each of the row of my table. But when I add onCellClick to each TableRow then it doesn't get fired. But when I add it to my Table Tag then it get's fired. The problem with this is when I add it to Table tag the only one type of action can be taken for all the rows. What if I want to do different things with each row whenever I click them. That's why I want to have all the TableRow tags their own onClick type of event. How can I do this?
Here is my code with onCellClick event on Table Tag, which I don't want.
class TagDetailTable extends Component {
handleButtonClick() {
browserHistory.push("TagList")
}
handleCellClick(){
console.log("cell clicked")
}
render() {
return (
<MuiThemeProvider>
<div>
<Table onCellClick= {this.handleCellClick}>
<TableHeader>
<TableRow>
<TableHeaderColumn>ID</TableHeaderColumn>
<TableHeaderColumn>Type</TableHeaderColumn>
<TableHeaderColumn>Category</TableHeaderColumn>
</TableRow>
</TableHeader>
<TableBody>
<TableRow>
<TableRowColumn>1</TableRowColumn>
<TableRowColumn>Cigarette</TableRowColumn>
<TableRowColumn>Compliance</TableRowColumn>
</TableRow>
<TableRow >
<TableRowColumn>2</TableRowColumn>
<TableRowColumn>Cigarette</TableRowColumn>
<TableRowColumn>Compliance</TableRowColumn>
</TableRow>
<TableRow >
<TableRowColumn>3</TableRowColumn>
<TableRowColumn>Cigarette</TableRowColumn>
<TableRowColumn>Compliance</TableRowColumn>
</TableRow>
<TableRow >
<TableRowColumn>4</TableRowColumn>
<TableRowColumn>Alcohol</TableRowColumn>
<TableRowColumn>Compliance</TableRowColumn>
</TableRow>
<TableRow >
<TableRowColumn>5</TableRowColumn>
<TableRowColumn>Alcohol</TableRowColumn>
<TableRowColumn>Compliance</TableRowColumn>
</TableRow>
{/*<TableRow>*/}
{/*<TableRowColumn>4</TableRowColumn>*/}
{/*<TableRowColumn>Alcohol</TableRowColumn>*/}
{/*<TableRowColumn>Compliance</TableRowColumn>*/}
{/*</TableRow>*/}
{/*<TableRow>*/}
{/*<TableRowColumn>4</TableRowColumn>*/}
{/*<TableRowColumn>Alcohol</TableRowColumn>*/}
{/*<TableRowColumn>Compliance</TableRowColumn>*/}
{/*</TableRow>*/}
{/*<TableRow>*/}
{/*<TableRowColumn>4</TableRowColumn>*/}
{/*<TableRowColumn>Alcohol</TableRowColumn>*/}
{/*<TableRowColumn>Compliance</TableRowColumn>*/}
{/*</TableRow>*/}
</TableBody>
</Table>
<div className="backButton">
<RaisedButton backgroundColor="#293C8E" label="Back" onClick={this.handleButtonClick}
labelColor="white">
</RaisedButton>
</div>
</div>
</MuiThemeProvider>
);
}
}
For those looking for this at the end of 2019, this did not work for me. At the moment you can just put onClick on TableCell or TableRow e.g.
<TableCell
key={column.id}
align={column.align}
onClick={() => handleClick(row.id)}
>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With