4

I am sorry, if I have missed this piece in documentation. Please show me how to sort a ReferenceField column on username value rather than id in the list/datagrid view.

<ReferenceField label="User" source="userId" reference="users" linkType="show">
  <TextField source="username" />
</ReferenceField>

As in this example sorting by Customer doesn't make much sense when ordered by Id.

Gagan
  • 163
  • 12

1 Answers1

3

tl;dr

Add sortBy="username" to the <ReferenceField> component.

Note: This solution only works with react-admin 2.1 and above!

Explanation

On their 2018 July blog post they announced:

React-admin 2.1 now allows you to specify the name of the field to use for sorting in a <ReferenceField>, using the sortBy prop:

<ReferenceField label="User" source="userId" reference="users" sortBy="user.name">
  <TextField source="name" />
</ReferenceField>

They also have a section for this in the documentation on <List> with the following code example:

<ReferenceField label="Post" source="id" reference="posts" sortBy="title">
  <TextField source="title" />
</ReferenceField>
totymedli
  • 29,531
  • 22
  • 131
  • 165
  • Adding a sortBy property made to a ReferenceField column in a DataGrid made no difference to sorting for me. I'm using the Firebase Realtime db via the [ra-data-firebase-client](https://github.com/aymendhaya/ra-data-firebase-client). I'm guessing that this is probably a back-end limitation. – ChillyPenguin Apr 10 '19 at 21:55
  • you need to configure your data adapter to pass this info in the way that your backend expects – Michail Michailidis Aug 19 '19 at 14:57