【问题标题】:Getting data from TableRow component in Material UI从 Material UI 中的 TableRow 组件获取数据
【发布时间】:2016-09-15 07:07:33
【问题描述】:

我在最新版本的 Material UI 中使用了一个表格组件,但我不确定当表格被选中时我应该如何从表格的行中获取数据。

文档中提到了名为 onRowSelection 的 Table 组件的道具,但它只为您提供了所选行的 RowNumber,没有其他内容。

您应该如何使用它?我不明白你的意思是说...设置为TableRow 的关键道具只使用同一TableRow 的RowNumber 道具。

下面的代码显示了我如何呈现表格本身并分配键:

handleSelect(id) {
  console.log(id);
  this.props.dispatch({ type: 'SET_SELECTED_USER', user: id });
}

renderUsers() {
  if (this.props.users && this.props.currentUser) {
    const currUser = this.props.currentUser.username;
    const userList = this.props.users.map((user) => {
      if (currUser !== user.username) {
        return (
          <TableRow key={user._id}>
            <TableRowColumn>{user.username}</TableRowColumn>
            <TableRowColumn>{user.roles[0]}</TableRowColumn>
          </TableRow>
        );
      }
    });
    return userList;
  }
}

我只是不明白当我需要访问行的键时,被选中行的 RowNumber 应该如何帮助我。

任何帮助将不胜感激!谢谢!

表格文档:http://www.material-ui.com/#/components/table

【问题讨论】:

  • 这里回答了同样的问题吗? stackoverflow.com/questions/36656447/…
  • 在某种程度上,我猜......但我的问题似乎是,与该问题的回答方式相反,key 道具不是用于识别被选中的行使用onRowSelection 时。相反,一些 abritrary rowNumber 属性由 Table 自动生成并被使用,这根本没有帮助,当我实际上需要它来使用该问题/答案所建议的 key 属性时。
  • 你可以用任何你想要的填充键值。查看该链接问题/答案中示例中的行创建 .map。您甚至可以链接到完整的对象。这有帮助吗?
  • 我不知道。问题是无法在创建行时将我想要的任何值粘贴到行的键/值道具中。问题是,当我去触发对所述行的选择时,我不明白我应该如何访问这些道具,而你只能将一个参数(我猜?)传递给获取的函数在触发onRowSelection 时调用。除非我错了,我上面的handleSelect 代码可能类似于function handleSelect(row, key) ...

标签: javascript meteor reactjs ecmascript-6 material-ui


【解决方案1】:

您可以使用行号作为用户数组 (this.props.users) 中的索引:

handleSelect(userIndex) {
  const currUser = this.props.currentUser.username;
  const users = this.props.users.filter(user => user.username !== currUser)
  this.props.dispatch({ type: 'SET_SELECTED_USER', user: users[userIndex].id });
}

【讨论】:

  • 今晚我一定会试一试并报告结果!
  • 今晚测试此代码后,它运行良好。非常感谢,答案已被肯定接受!再次感谢!
猜你喜欢
  • 2016-10-12
  • 2015-06-29
  • 2020-04-16
  • 1970-01-01
  • 2021-04-26
  • 2020-12-31
  • 1970-01-01
  • 2017-04-07
  • 2019-01-25
相关资源
最近更新 更多