简体   繁体   English

材质UI React Table onCellClick在TableRow上不起作用

[英]Material UI React Table onCellClick not working on TableRow

I have material UI Table in my react component. 我的React组件中包含实质性的UI表。 I want to add onCellClick event to each of the row of my table. 我想将onCellClick事件添加到表的每一行。 But when I add onCellClick to each TableRow then it doesn't get fired. 但是,当我将onCellClick添加到每个TableRow时,它不会被触发。 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. 问题是当我将其添加到Table标记时,所有行都只能采取一种类型的操作。 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. 这就是为什么我要让所有TableRow标记具有自己的onClick类型的事件。 How can I do this? 我怎样才能做到这一点?

Here is my code with onCellClick event on Table Tag, which I don't want. 这是我不需要的带有Table Tag上onCellClick事件的代码。

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>

            );
        }
    }

Attaching OnCellClick to Row is not supported. 不支持将OnCellClick附加到行。 See https://github.com/callemall/material-ui/issues/2819 for more details. 有关更多详细信息,请参见https://github.com/callemall/material-ui/issues/2819

However, you could use the function parameter of the handleCellClick. 但是,您可以使用handleCellClick的function参数。 The signature of the function receiving the event is: 接收事件的函数的签名为:

handleCellClick(row,column,event)
{
    if(row ==0)
       console.log('Cigarette row clicked');

}
export interface TableRowProps {
        // <tr/> is element that get the 'other' properties
        className?: string;
        displayBorder?: boolean;
        hoverable?: boolean;
        hovered?: boolean;
        /** @deprecated Instead, use event handler on Table */
        onCellClick?(e: React.MouseEvent<{}>, row: number, column: number): void;
        /** @deprecated Instead, use event handler on Table */
        onCellHover?(e: React.MouseEvent<{}>, row: number, column: number): void;
        /** @deprecated Instead, use event handler on Table */
        onCellHoverExit?(e: React.MouseEvent<{}>, row: number, column: number): void;
        /** @deprecated Instead, use event handler on Table */
        onRowClick?(e: React.MouseEvent<{}>, row: number): void;
        /** @deprecated Instead, use event handler on Table */
        onRowHover?(e: React.MouseEvent<{}>, row: number): void;
        /** @deprecated Instead, use event handler on Table */
        onRowHoverExit?(e: React.MouseEvent<{}>, row: number): void;
        rowNumber?: number;
        selectable?: boolean;
        selected?: boolean;
        striped?: boolean;
        style?: React.CSSProperties;
    }
export class TableRow extends React.Component<TableRowProps> {
    }

There is an onRowClick function which I used on <TableRow/> rather than defining handlers on Table itself. 我在<TableRow/>上使用了onRowClick函数,而不是在Table本身上定义处理程序。 It's deprecated though so the solution may not be reliable. 不过已弃用,因此解决方案可能不可靠。 I've used material-ui v0.20 so not sure if it still works but you can give it a try. 我使用了material-ui v0.20,所以不确定它是否仍然有效,但是您可以尝试一下。

For those looking for this at the end of 2019, this did not work for me. 对于那些在2019年底寻找这个的人来说,这对我不起作用。 At the moment you can just put onClick on TableCell or TableRow eg 目前,您可以在TableCell或TableRow上放onClick

<TableCell
  key={column.id}
  align={column.align}
  onClick={() => handleClick(row.id)}
>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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