In react-admin I have a page that shows a list of balances, it looks like this:
// extra code not included
export const BalanceList = props => (
<List filters={<BalanceFilter/>} actions={<BalanceActions/>} {...props} perPage={-1}>
<Datagrid rowClick="edit">
<TextField source="id" />
<DateField source="date" />
<ReferenceField source="account_id" reference="accounts">
<TextField source="name"/>
</ReferenceField>
<TextField source="currency.name" label="Currency" />
<TextField source="currency.symbol" label="Symbol" />
<NumberField source="used" options={{ maximumFractionDigits: 4 }} />
<NumberField source="free" options={{ maximumFractionDigits: 4 }} />
<NumberField source="total" options={{ maximumFractionDigits: 4 }} />
<NumberField source="total_value" options={{ maximumFractionDigits: 2 }} />
</Datagrid>
</List>
);
I would like to display a line in the end, between the list and the pagination. Or also inside the list component if it's easier to achieve.
That line should show the total of the used amount, fee amount etc. to calculate those, it should simply sum all the values of the relative column. E.g. total_value should be a sum of all "total_value" rows.
I'm new to react and react-admin and no idea how to achieve this. Please provide some code in your answer, thank you.
UPDATE: I tried to use the aside component:
const Aside = ({ data, ids }) => (
<div style={{ width: 200, margin: '1em' }}>
<Typography variant="title">Post details</Typography>
<Typography variant="body1">
Total value: {ids.map(id => data[id]).reduce((sum, balance) => sum + balance.total_value)}
</Typography>
</div>
);
export const BalanceList = props => (
<List aside={<Aside />} filters={<BalanceFilter/>} actions={<BalanceActions/>} {...props} perPage={-1}>
<Datagrid rowClick="edit">
<TextField source="id" />
<DateField source="date" />
<ReferenceField source="account_id" reference="accounts">
<TextField source="name"/>
</ReferenceField>
<TextField source="currency.name" label="Currency" />
<TextField source="currency.symbol" label="Symbol" />
<NumberField source="used" options={{ maximumFractionDigits: 4 }} />
<NumberField source="free" options={{ maximumFractionDigits: 4 }} />
<NumberField source="total" options={{ maximumFractionDigits: 4 }} />
<NumberField source="total_value" options={{ maximumFractionDigits: 2 }} />
</Datagrid>
</List>
);
But for some unknown reason the reduce
function complains that it can't be applied to an empty array. Why empty? data
is supposed to be the same array passed to the DataGrid...