材质UI React Table onCellClick

时间:2018-08-15 12:12:44

标签: reactjs material-ui

我正在尝试在Material UI表上实现一个简单的onCellClick侦听器。

在早期版本中,表作用域onCellClick中的一个函数用于提供进行点击的行和列号,如图here

当前放置此功能时-

import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import Table from '@material-ui/core/Table';
import TableBody from '@material-ui/core/TableBody';
import TableCell from '@material-ui/core/TableCell';
import TableHead from '@material-ui/core/TableHead';
import TableRow from '@material-ui/core/TableRow';
import Paper from '@material-ui/core/Paper';

const styles = {
  }
let id = 0;
function createData(name, calories, fat, carbs, protein) {
  id += 1;
  return { id, name, calories, fat, carbs, protein };
}

const rows = [
  createData('Frozen yoghurt', 159, 6.0, 24, 4.0),
  createData('Ice cream sandwich', 237, 9.0, 37, 4.3),
  createData('Eclair', 262, 16.0, 24, 6.0),
  createData('Cupcake', 305, 3.7, 67, 4.3),
  createData('Gingerbread', 356, 16.0, 49, 3.9),
];

function SimpleTable(props) {
  const { classes } = props;

  return (
    <Paper className={classes.root}>
      <Table className={classes.table} 
      onCellClick={(rowNumber, 
      columnId) =>
       console.log(rowNumber,columnId)}>
        <TableHead>
          <TableRow>
            <TableCell>Dessert (100g serving)</TableCell>
            <TableCell numeric>Calories</TableCell>
            <TableCell numeric>Fat (g)</TableCell>
            <TableCell numeric>Carbs (g)</TableCell>
            <TableCell numeric>Protein (g)</TableCell>
          </TableRow>
        </TableHead>
        <TableBody>
          {rows.map(row => {
            return (
              <TableRow key={row.id}>
                <TableCell component="th" scope="row">
                  {row.name}
                </TableCell>
                <TableCell numeric>{row.calories}</TableCell>
                <TableCell numeric>{row.fat}</TableCell>
                <TableCell numeric>{row.carbs}</TableCell>
                <TableCell numeric>{row.protein}</TableCell>
              </TableRow>
            );
          })}
        </TableBody>
      </Table>
    </Paper>
  );
}


export default withStyles(styles)(SimpleTable);

它引发错误未知事件处理程序属性onCellClick。它将被忽略。这是可以预期的,因为与以前的版本不同,表source code中没有传递onCellClick函数。

现在如何在Material-UI中实现onCellClick功能?

我们将不胜感激任何帮助。

1 个答案:

答案 0 :(得分:0)

单元格可以是thtd元素(see the source code)或通过component属性传递的自定义元素。

其中的每一个都将支持onClick property

所以您需要这样的东西:

handleClick = (id, column) => {
  return (event) => {
    console.log(`You clicked on row with id ${id}, in column ${column}.`);
  }
}

render() {
  return (
    // ...
    {this.state.rows.map((row) => (
      <TableRow key={row.id}>
        <TableCell onClick={this.handleClick(row.id, "calories")}>
          {row.calories}
        </TableCell>
        // ...
        <TableCell onClick={this.handleClick(row.id, "protein")}>
          {row.protein}
        </TableCell>
      </TableRow>
    )}
    // ...
  );
}