【问题标题】:Material UI React Table onCellClick not working on TableRowMaterial UI React Table onCellClick 在 TableRow 上不起作用
【发布时间】:2016-10-20 09:23:21
【问题描述】:

我的反应组件中有材料 UI 表。我想将 onCellClick 事件添加到表的每一行。但是当我将 onCellClick 添加到每个 TableRow 时,它不会被触发。但是当我将它添加到我的表格标签时,它就会被解雇。问题是当我将它添加到表标签时,只能对所有行采取一种类型的操作。如果我想在每次单击时对每一行做不同的事情怎么办。这就是为什么我希望所有 TableRow 标记都有自己的 onClick 类型的事件。我怎样才能做到这一点?

这是我在表格标签上带有 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>

            );
        }
    }

【问题讨论】:

    标签: javascript reactjs material-ui


    【解决方案1】:

    对于那些在 2019 年底寻找这个的人来说,这对我不起作用。 目前你可以只在 TableCell 或 TableRow 上放 onClick 例如

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

    【讨论】:

    • 我认为这不是最好的主意。我们所有的表格数据都是用 TableCell 呈现的,onClick 事件只会在所有单元格上触发事件,这意味着你可以说整行......
    【解决方案2】:

    不支持将 OnCellClick 附加到行。详情请见https://github.com/callemall/material-ui/issues/2819

    但是,您可以使用handleCellClick 的函数参数。接收事件的函数的签名是:

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

    【讨论】:

    • 在这个函数中如何获取我点击了哪一行?
    • 您将获得行索引作为参数,您可以使用它进行相应的处理...
    • 我不明白。你能告诉我我应该在这个方法中放什么
    • 如上所示,您是否要在表中使用静态数据?或者你会使用动态道具吗?对于静态数据的情况,我已对我的答案进行了修改。
    • 我将使用动态道具。
    【解决方案3】:
    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> {
        }
    

    我在&lt;TableRow/&gt; 上使用了一个onRowClick 函数,而不是在Table 本身上定义处理程序。虽然它已被弃用,因此解决方案可能不可靠。我用过material-ui v0.20,所以不确定它是否仍然有效,但你可以试一试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-31
      • 2017-02-07
      • 2017-03-06
      • 2020-03-22
      • 2019-01-19
      • 2016-12-26
      • 2018-04-11
      • 2020-04-23
      相关资源
      最近更新 更多