分类列集合参考(例如总和)

发布于 2025-02-06 01:00:53 字数 1049 浏览 0 评论 0原文

在React-Admin中,对于数据网格中的一个记录,我想获得与该记录的关系的总数,然后能够对其进行排序。

例如,假设我为作者和追随者提供了一个加入表。对于作者列表,我想要一列,告诉我该作者的关注者总数。然后,我想对该列进行排序,以便可以看到哪些作者拥有最少/最少的关注者。

为了获取“关注者的数量” -Column我创建了一个自定义组件,可以为每个作者返回追随者计数:

const ReferenceCount = ({ reference, target }) => {
  const record = useRecordContext();
  const { total, isLoading, error } = useGetManyReference(reference, {
    target: target,
    id: record.id
    //sort: { field: "?", order: "DESC" }
  });
  if (isLoading) {
    return <p>loading...</p>;
  }
  if (error) {
    return <p>ERROR</p>;
  }
  return total;
};

并在数据杂志中使用了它(源是作者):

  <List>
    <Datagrid>
      <TextField source="name" />

      <ReferenceCount
        reference="authorFollower"
        target="authorId"
        label="Number of Followers"
      />
       </Datagrid>
  </List>

它有效!但是该列无法分类,因为值与任何字段无关。因此,我无法将sort属性添加到自定义组件中,也不能使用sortby -tag。似乎自定义排序按钮也取决于字段。

有什么方法可以实现这一目标吗?

In react-admin, for one record in a data grid, I want to get the total number of a relation to that record, and then be able to sort it.

For example, let's say I have a join table for authors and followers. For the author list I want a column that tells me the total number of followers for this author. Then I want to sort that column so that I can see which authors have the most/least followers.

To get the "number of followers"-column I created a custom Component that can return the follower-count for each author:

const ReferenceCount = ({ reference, target }) => {
  const record = useRecordContext();
  const { total, isLoading, error } = useGetManyReference(reference, {
    target: target,
    id: record.id
    //sort: { field: "?", order: "DESC" }
  });
  if (isLoading) {
    return <p>loading...</p>;
  }
  if (error) {
    return <p>ERROR</p>;
  }
  return total;
};

and used it within a datagrid (source is authors):

  <List>
    <Datagrid>
      <TextField source="name" />

      <ReferenceCount
        reference="authorFollower"
        target="authorId"
        label="Number of Followers"
      />
       </Datagrid>
  </List>

It works! But the column is not sortable as the values are not related to any field. So I cannot add the sort property to the custom component, or use the sortBy-tag. It seems that the custom sort button also depends on a field.

Is there a any way to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文