New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[QUESTION] How to get sum of column values in footer. #101
Comments
If you can, do this outside of the footer function for performance reasons. If you have a small dataset though, you could do it in the render function of the column footer: Outside of column, more performant const data = [...]
let ageSum = 0
for (let i = 0; i <= data.length; i++) {
ageSum += data[i].age
}
const columns = [{
header: 'Age',
footer: 'Total: ' + ageSum
}] Inside column, less performant const data = [...]
const columns = [{
header: 'Age',
footer: () => {
let ageSum = 0
for (let i = 0; i <= data.length; i++) {
ageSum += data[i].age
}
return 'Total: ' + ageSum
}
}] |
Thanks a lot for response. However, with this solution it is rendering value as 0. here is my code snippet. var marketValSum = 0;
var cashSum = 0;
const columns = [
{ accessor: 'name', header: 'Name',width: 205 },
{ accessor: 'marketValue', header: 'Market Value', footer: 'Total: ' + marketValSum},
{ accessor: 'cash', header: 'Cash', footer: 'Total: ' + marketValSum}
];
const AccountsGrid = (props) => {
const accountData = props.accounts;
if(accountData.length >0) {
for (let i = 0; i < accountData.length; i++) {
marketValSum += accountData[i].marketValue;
cashSum += accountData[i].cash;
}
}
return (
<ReactTable
columns={columns}
data={props.accounts}
className='-striped -highlight'
defaultPageSize={10}
/>
);
}; |
Try this: const AccountsGrid = (props) => {
var marketValSum = 0;
var cashSum = 0;
const accountData = props.accounts;
if(accountData.length >0) {
for (let i = 0; i < accountData.length; i++) {
marketValSum += accountData[i].marketValue;
cashSum += accountData[i].cash;
}
}
const columns = [
{ accessor: 'name', header: 'Name',width: 205 },
{ accessor: 'marketValue', header: 'Market Value', footer: 'Total: ' + marketValSum},
{ accessor: 'cash', header: 'Cash', footer: 'Total: ' + cashSum}
]
return (
<ReactTable
columns={columns}
data={props.accounts}
className='-striped -highlight'
defaultPageSize={10}
/>
);
}; |
It works. thanks a lot. |
You could also do this:
if your columns look like this:
|
I've been able to make the code above work, however, if my table is filterable, when the table displays a set of filtered rows, the footer sums do not change to match the rows. How can I make it recalculate the sum of the filtered rows only? |
@Mike-SP |
@gary-menzel How to change the value of footers? I can get filtered data but don't know how to change the footer. Can you give me an example? |
Hi @zarela am getting an error '_' is not defined no-undef |
@phares Make sure you are importing lodash to use _ |
If the data in the table was editable would this work to update the sum value accordingly? |
For me the solution with Howerver, I did find another solution. Here is my footer definition, for a the sum of a column with costs-data:
|
For anyone who stumbles on this like I did, here's a current method that worked for me. It uses the native JavaScript {
accessorKey: 'your_accessor_key',
footer: ({ table }) => table.getFilteredRowModel().rows.reduce((total, row) => total + row.getValue('your_accessor_key'), 0),
} Note that you may need to cast the return value of |
This works, but also can lead to heavy calculations during renders. Consider doing calculations like this in a useMemo based on the data itself |
I am getting the total of age in my scenario. |
@sandeepd4d Difference between 20 and -40 is 40 :) It would be 20, if you change 20 to 0. |
Hello, could you please help with rendering footer.
|
how do we actually use
There is no documentation or examples anywhere that I can find, and I see no intuitive way to use this |
I tried |
I need to sum the column values and show that in footer. Footer Demo seems to have missing the code.
The text was updated successfully, but these errors were encountered: