分类列集合参考(例如总和)
在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论