Material UI React Table onCellClick not working on TableRow
javascript, material-ui, reactjs
Solution
Attaching OnCellClick to Row is not supported. See https://github.com/callemall/material-ui/issues/2819 for more details.
However, you could use the function parameter of the handleCellClick. The signature of the function receiving the event is:
handleCellClick(row,column,event)
{
if(row ==0)
console.log('Cigarette row clicked');
}
Problem
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> ); } } ```