【问题标题】:How to add a column link to material-ui table and redirect to summary detail page using column link id如何将列链接添加到 material-ui 表并使用列链接 ID 重定向到摘要详细信息页面
【发布时间】:2021-09-03 00:14:22
【问题描述】:

我正在处理 Material-UI 表,例如下面的basic table,但我在开头添加了一个 id 列。

假设我在我的 React 应用程序中的路径 localhost:3000/basic-table 有这个基本表格示例,我如何使用下面的代码并将 <tableCell>{row.id}</tableCell> 变成 <a href link>,这将允许用户点击这个 @987654326 @column 然后向用户展示一个新屏幕,我可以在哪里获取这个 id 并显示更多关于它的数据?

我还需要一种返回主页报告的方法,即父页面。

我想要实现的基本上是来自主父报告,用户可以单击行 ID,然后会根据该 ID 向用户显示详细报告,即像向下钻取报告一样。

从文档中,我找不到任何可以实现此目的的示例,因为不确定如何添加列链接。

谁能帮忙或者给我举个例子。

      {rows.map((row) => (
        <TableRow key={row.id}>
          <TableCell component="th" scope="row">
            {row.id}
          </TableCell>
          <TableCell align="right">{row.name}</TableCell>
          <TableCell align="right">{row.calories}</TableCell>
          <TableCell align="right">{row.fat}</TableCell>
          <TableCell align="right">{row.carbs}</TableCell>
          <TableCell align="right">{row.protein}</TableCell>
        </TableRow>
      ))}

【问题讨论】:

  • 对于其他人来说,下面的答案很好,但我最终使用了下面的代码 /elsewhere/${row.id} } style={{ textDecoration: 'underline', color: 'black' }} > {value} 达到我的要求。这是来自stackoverflow.com/questions/50691049/…

标签: reactjs material-ui report material-table drilldown


【解决方案1】:

您可以使用历史 API

import { useHistory } from 'react-router-dom';

const YourComponent = () => {
...
const history = useHistory();
return {
    ...
    rows.map((row) => (
        <TableRow key={row.id} onClick={() => history.push(yourLocation)}>
          <TableCell component="th" scope="row">
            {row.id}
          </TableCell>
          <TableCell align="right">{row.name}</TableCell>
          <TableCell align="right">{row.calories}</TableCell>
          <TableCell align="right">{row.fat}</TableCell>
          <TableCell align="right">{row.carbs}</TableCell>
          <TableCell align="right">{row.protein}</TableCell>
        </TableRow>
      ))}
}

【讨论】:

  • 我实际上只想要{row.id} 列上的链接,当用户单击此 id 列链接时,我如何通过此 id 值说 localhost:3000/basic-table/summary?id=5? With your push(yourLocation)` -这需要成为我需要在 App.js 中创建的路由吗?
  • 你可以推送任何路由。 history.push('/basic-table/summary?id=${row.id}')
  • 您可以在代码中的任何位置使用历史 API 来导航到所需的页面
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-04
  • 2018-05-29
  • 2019-10-19
  • 2021-04-04
  • 1970-01-01
  • 2019-06-11
  • 2018-08-07
相关资源
最近更新 更多