Following a suggestion made here: Get ReferenceManyField Count? I am using a ReferenceManyField in react admin, combined with a custom field to display the count of the returned records, rather than any of the specific data fields from the reference
import * as React from "react";
import {Datagrid, List, TextField, NumberField, ReferenceManyField} from 'react-admin';
const ShowTotalField = props => <div>{props.total}</div>;
const TeamList = props => {
return (
<List {...props}>
<Datagrid rowClick="edit">
<TextField label="Team" source="teamName" />
<ReferenceManyField label="Headcount" reference="reviews" source="teamName" target="team" link={false}>
<ShowTotalField />
</ReferenceManyField>
<TextField source="managerName" />
<TextField source="parentTeam" />
<NumberField source="averageTotal" />
</Datagrid>
</List>
);
};
export default TeamList;
My problem is that I don't know how to sort the DataGrid by the rendered value in the custom field ie. the count. Clicking on the column header isn't following a natural sort that I would expect.
I can see documentation to show how to specify a different field to sort by from the target resource: https://marmelab.com/react-admin/List.html#specifying-a-sort-field
However, because the value I'm sorting on (a generated count value) is not available in the target resource, there's no different field I can specify
How can I achieve the behaviour of being able to sort by this generated value? Or failing that, setup the resource or dataprovider to allow to return a custom calculated value
Thanks, mikee