Material UI React 表格 onCellClick

4

我正在尝试在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);

它抛出错误“Unknown event handler property onCellClick”并将被忽略。这是可以预料的,因为与先前版本不同,表格中没有传递onCellClick函数,请参见源代码

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

非常感谢您的任何帮助。

1个回答

1
一个单元格可以是thtd元素(查看源代码),或通过component属性传递的自定义元素。
每个元素都将支持onClick 属性
因此,您需要像这样的内容:
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>
    )}
    // ...
  );
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接