Material-ui - TableRow wrapped in component wont show checkbox

偶尔善良 提交于 2019-12-08 15:59:12

问题


I'm using Material-ui in my project and I'm facing an issue.

I would like to use Table component to show a dynamic list of items with checkbox on each row.

This is what my render looks like :

<Table multiSelectable={true}>
    <TableHeader>
        <TableRow>
            <TableHeaderColumn>Reference</TableHeaderColumn>
                .... All others columns ...
            <TableHeaderColumn>Actions</TableHeaderColumn>
        </TableRow>
    </TableHeader>
    <TableBody displayRowCheckbox={ true }>
        { this.generateOrderRows() }
    </TableBody>
</Table>

The generateOrderRows() method :

generateOrderRows() {
    var rows = [];
    if (this.state.orders) {
        this.state.orders.map(function(order) {
            rows.push(<OrderListRow key={order._id} order={order} selected={false}/>);
        });
    }
    if (rows.length == 0) {
        rows.push(<tr key="1"><td className="text-center" colSpan="9">It seems there is no results... :'(</td></tr>);
    }
    return rows;
}

My Table rendering well and I'm able to multi-select rows by clicking on it without any problems. But none of my rows display the checkbox for the selection, even by passing parent props to TableRow like this :

render() {
    const { order, ...otherProps } = this.props;
    return(
        <TableRow { ...otherProps }>
            <TableRowColumn>{ order.reference }</TableRowColumn>
                ... All others columns ...
            <TableRowColumn>
                <RaisedButton label="View" primary={true} linkButton={true} href={ '/order/' + order._id }/>
            </TableRowColumn>
        </TableRow>
    );
}

If I inspect my TableRows with the React Dev Tools, I can see that each of them receive the prop displayRowCheckbox=true from TableBody.

So I can't figure out why my checkboxes dont show up. Any Idea ?


回答1:


I hit the same issue... (using material-ui@0.14.4)

Apparently material-ui TableBody pushes the checkbox component into its children. You need to grab it from your custom TableRow props and render it explicitly in your custom TableRow render() method.

Note: You need to both spread otherProps into the TableRow and explicitly render the checkbox.

// OrderListRow...
render() {
    const { order, ...otherProps } = this.props;
    return(
        <TableRow { ...otherProps }>
            {otherProps.children[0] /* checkbox passed down from Table-Body*/}
            <TableRowColumn>{ order.reference }</TableRowColumn>
                ... All others columns ...
            <TableRowColumn>
                <RaisedButton label="View" primary={true} linkButton={true} href={ '/order/' + order._id }/>
            </TableRowColumn>
        </TableRow>
    );
}

https://github.com/callemall/material-ui/issues/1749#issuecomment-217667754 https://github.com/callemall/material-ui/blob/master/src/Table/TableBody.js#L173



来源:https://stackoverflow.com/questions/37479568/material-ui-tablerow-wrapped-in-component-wont-show-checkbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!