1

I'm using primeng datatable in one project. I'm trying to get the sum in the footer of the row grouping DataTable. Summation must take place during data editing and entering new data.

Folow function calculate the sum:

sumNum1: number;
calculateGroupTotal1(sectionId: number) {
    this.sumNum1 = this.model.Register10Data.map(c => c.SectionId === 1 ? c.Num1 : 0)
        .reduce((sum, current) => +sum + current);

    console.log(this.sumNum1);

    return this.sumNum1;
}

SectionId is value by which grouping data.

In footer i call this function. I did it by the first example from here - http://www.primefaces.org/primeng/#/datatable/rowgroup

<template pTemplate="rowgroupfooter" let-rowData>
<td style="text-align:left">Sum</td>
<td style="text-align:center">X</td>
<td style="text-align:center">X</td>
<td>{{calculateGroupTotal1(rowData['SectionId'])}}</td>
<td></td>
<td></td>
<td></td>
</template>

Why in summary field i get concatenation of strings and not get amount? Maybe someone did such a thing? The task is quite trivial but I do not know how to use this control.

Community
  • 1
  • 1
user6408649
  • 1,227
  • 3
  • 16
  • 40

1 Answers1

1

I guess the problem is that the values you are using are strings. You need to cast the value to a number in order to perform your calculations.

You can either use let numericalSum = Number(sum); or let numericalSum = +sum; as explained in this answer.

Community
  • 1
  • 1
alex kucksdorf
  • 2,573
  • 1
  • 15
  • 26