从 MongoDB 填充 Material-UI DataGrid 失败,找不到唯一 ID

Mic*_*ott 11 mongodb reactjs material-ui

错误:Material-UI:数据网格组件要求所有行都具有唯一的 id 属性。当我调用表格时,我在行道具中没有提供一行是我所看到的。

这是我定义表的方式。

const columns = [
  { field: "_id", hide: true },
  { field: "user", headerName: "User", width: 70 },
  { field: "fromaddress", headerName: "Sender", width: 70 },
  { field: "dkimSpfChk", headerName: "DKIM/SPF", width: 70 },
  { field: "replySenderMismatch", headerName: "Reply MisMatch", width: 70 },
  { field: "riskywordchk", headerName: "Risky Word", width: 70 },
  { field: "domainagechk", headerName: "Sender Domain Age", width: 70 },
  { field: "attachmentChk", headerName: "Attachments", width: 70 },
  { field: "riskyLinkAge", headerName: "Body Link Age", width: 70 },
  { field: "riskyLinkTypo", headerName: "Link Typosquatting", width: 70 },
  {
    field: "senderTypoSquatting",
    headerName: "Sender TypoSquatting",
    width: 70,
  }
];
Run Code Online (Sandbox Code Playgroud)

我从我的 api 中获取数据,然后在行中填充

 useEffect(() => {
    var apiurl = "http://localhost:5000/adminportal/highRiskEmails";
    axios
      .get(apiurl)
      .then((response) => response.data)
      .then((data) => {
        setIsLoaded(true);
        setRowData(data);
      });
  }, []);
Run Code Online (Sandbox Code Playgroud)

这是数据网格

return (
  <div style={{ height: 400, width: "100%" }}>
    <DataGrid
      rows={rowData}
      columns={columns}
      id="_id"
      pageSize={15}
      checkboxSelection
    />
  </div>
);
Run Code Online (Sandbox Code Playgroud)

我知道 _id 字段在 Mongo 中是独一无二的,所以想知道我错过了什么。我是否必须定义 id 字段不是 id 而是 _id?

谢谢

Ben*_*ley 15

您现在可以使用getRowId接受函数的属性来指定每行上的哪个属性应该被视为 id。

在 OP 的情况下,你可以这样做: getRowId={(row) => row._id}

参考:https : //github.com/mui-org/material-ui-x/pull/972

  • @Ezan​​i,在您的示例中:`&lt;ReactDataGrid columns={columns} rows={rowData} getRowId ={(row) =&gt; row._id} /&gt;` (4认同)
  • 欢迎@Ezan​​i!如果对你有用请点赞:) (2认同)