react材料ui表无法从行中获取元素

IT技术 reactjs material-ui
2021-04-29 12:13:46

我想使用材料 ui 库中的表格进行react:http : //www.material-ui.com/#/components/table

我显示它,它工作得很好,我正在尝试获取与我选择的行相关联的对象,但我找不到这样做的方法,我看到了该属性,children或者onRowSelection我无法在这里获取我的对象我的代码。如何获取所选行的整个对象?

import React, { PropTypes, Component } from 'react'

import Table from 'material-ui/lib/table/table';
import TableHeaderColumn from 'material-ui/lib/table/table-header-column';
import TableRow from 'material-ui/lib/table/table-row';
import TableHeader from 'material-ui/lib/table/table-header';
import TableRowColumn from 'material-ui/lib/table/table-row-column';
import TableBody from 'material-ui/lib/table/table-body';


export default class MagicTableReact extends Component {
    constructor(props) {
        super(props);
        this.state = {
            data : [{id:0,name:"joe"},{id:1,name:"john"},{id:2,name:"Brad"},{id:3,name:"Jack"},{id:4,name:"Andrew"}],
            fixedHeader: true,
            fixedFooter: true,
            stripedRows: false,
            showRowHover: true,
            selectable: true,
            multiSelectable: false,
            enableSelectAll: false,
            deselectOnClickaway: true,
            height: '300px',
        };
    };


    _onRowSelection(e){
        console.log(e)

    }


    render() {

        return (

            <div>
                <div className="col-sm-6">
                    <h1>MagicTableReact</h1>
                    <Table
                        height={this.state.height}
                        fixedHeader={this.state.fixedHeader}
                        fixedFooter={this.state.fixedFooter}
                        selectable={this.state.selectable}
                        multiSelectable={this.state.multiSelectable}
                        >
                        <TableHeader>
                            <TableRow>
                                <TableHeaderColumn>ID</TableHeaderColumn>
                                <TableHeaderColumn>Name</TableHeaderColumn>
                            </TableRow>
                        </TableHeader>
                        <TableBody
                        >
                            {this.state.data.map((user, i) =>
                                <TableRow key={i}
                                    onRowSelection={this._onRowSelection.bind(this)}>
                                    <TableRowColumn>{user.id}</TableRowColumn>
                                    <TableRowColumn>{user.name}</TableRowColumn>
                                </TableRow>
                            )}
                        </TableBody>
                    </Table>
                </div>
            </div>
        )
    }
}

MagicTableReact.propTypes = {

};
4个回答

更新:在 material-ui 0.15.4 中,传递给 onRowSelection 处理程序的参数已更改。请参阅@Jonathn 的答案以了解未来的正确用法。

onRowSelection<Table>not的属性<TableRow>此外,它在调用 ( e)时不会使用事件调用处理程序,而是使用键调用它。然后您可以使用它来查找行。像这样:

_onRowSelection(key) {
  console.log(key, this.state.data[key])
},

getInitialState() {
  return {
    data : [{id:0,name:"joe"},{id:1,name:"john"},{id:2,name:"Brad"},{id:3,name:"Jack"},{id:4,name:"Andrew"}],
    fixedHeader: true,
    fixedFooter: true,
    stripedRows: false,
    showRowHover: true,
    selectable: true,
    multiSelectable: false,
    enableSelectAll: false,
    deselectOnClickaway: true,
    height: '300px',
  };
},

render() {
  return (
    <div>
      <div className="col-sm-6">
        <h1>MagicTableReact</h1>
        <Table
          height={this.state.height}
          fixedHeader={this.state.fixedHeader}
          fixedFooter={this.state.fixedFooter}
          selectable={this.state.selectable}
          multiSelectable={this.state.multiSelectable}
          onRowSelection={this._onRowSelection}
        >
          <TableHeader>
            <TableRow>
              <TableHeaderColumn>ID</TableHeaderColumn>
              <TableHeaderColumn>Name</TableHeaderColumn>
            </TableRow>
          </TableHeader>
          <TableBody
          >
            {this.state.data.map((user, i) =>
              <TableRow key={i} value={user}>
                <TableRowColumn>{user.id}</TableRowColumn>
                <TableRowColumn>{user.name}</TableRowColumn>
              </TableRow>
            )}
          </TableBody>
        </Table>
      </div>
    </div>
  );
}

使用 [Material-UI 0.15.4]

onRowSelection只返回noneall或包含所选行的行号的数组。

使用onCellClick,也为<Table>水平,收益rowNumbercolumnNumber以及event此事件包含下方的表格单元格target,您可以data-从那里引用标签等。

例子:

function handleCellClick (rowNumber, columnNumber, evt) {
  console.log("activityId", evt.target.dataset.myRowIdentifier);
}

function createTableRow (content) {
  let activityId = content.shift();
  return (
    <TableRow key={activityId}>
      {content.map((columnContent, i) => {
        return (
          <TableRowColumn key={i} data-my-row-identifier={activityId} >{columnContent}</TableRowColumn>
        );
      })}
    </TableRow>
  );
}

return (
  <div>
    <Table onCellClick={handleCellClick}>
      <TableHeader displaySelectAll={false} adjustForCheckbox={false}>
      {createHeaderRow(tableHeader)}
      </TableHeader>
      <TableBody displayRowCheckbox={false}>
      {filteredTable.map(createTableRow)}
      </TableBody>
    </Table>
  </div>
);

我在 StackOverflow 上的第一个答案......希望它有帮助 :-)

<Table onCellClick={(row, col, event) => console.log(event.target.innerHTML)}>

您可以使用您的初始数据数组并根据行号引用索引来访问您选择的行和与其关联的对象。这是一个基本示例,显然取决于您想对数据做什么,您可能需要添加更多逻辑。

_onRowSelection(rows) {
    for (let i = 0; i < rows.length; i++) {
        console.log(this.state.data[rows[i]])
    }
},

getInitialState() {
  return {
    data : [{id:0,name:"joe"},{id:1,name:"john"},{id:2,name:"Brad"},{id:3,name:"Jack"},{id:4,name:"Andrew"}]
  };
},

render() {
  return (
    <div>
      <div className="col-sm-6">
        <h1>MagicTableReact</h1>
        <Table onRowSelection={(rows) => this._onRowSelection(rows)}>
          <TableHeader>
            <TableRow>
              <TableHeaderColumn>ID</TableHeaderColumn>
              <TableHeaderColumn>Name</TableHeaderColumn>
            </TableRow>
          </TableHeader>
          <TableBody
          >
            {this.state.data.map((user, i) =>
              <TableRow key={i} value={user}>
                <TableRowColumn>{user.id}</TableRowColumn>
                <TableRowColumn>{user.name}</TableRowColumn>
              </TableRow>
            )}
          </TableBody>
        </Table>
      </div>
    </div>
  );
}