json2Csv Angular - add row with totals

216 Views Asked by At

I'm currently using json2Csv for exporting data in csv table.

The data is exported correctly but I want to add footer row which will calculate total price.

The link is down below and in footer I want to get the total for prices: https://stackblitz.com/edit/angular-json2csv

In documentation there is nothing about adding row with totals (or footer row). Does anybody know how to add that?

Many thanks!

1

There are 1 best solutions below

0
On

Not sure if there is a more official way to do this but this works: This is how I would format the data if I was writing the CSV by hand. You're just telling it which columns your footers belong in.

downloadFile(){
  const cars = _.clone(this.myCars);
  cars.push({car: 'total', price : _.sumBy(cars, 'price')});
  return this.downloadService.downloadFile(cars)
}

You'll need to pull in lodash to use this answer: https://lodash.com/docs/4.17.15

Stackblitz Link