Utilize Primeng data grid to calculate total sum and display it in the footer section

I have been utilizing primeng datatable in a recent project and am currently facing an issue with calculating the sum in the footer of a row grouping DataTable. The summation needs to occur while data is being edited and new data is being entered.

Below is the function that calculates 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;
}

The SectionId represents the value by which the data is grouped.

In the footer, I call this function using the first example provided on the following link - 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>

I am encountering an issue where the summary field displays a concatenation of strings instead of the actual amount. Have you come across a similar problem? While the task seems straightforward, I am unsure about how to effectively utilize this control.

Answer №1

It seems like the issue lies in the fact that the values being used are strings. To solve this problem, you will need to convert the value into a number before performing any calculations.

You have two options for this conversion: either use let numericalSum = Number(sum); or let numericalSum = +sum; as described in this response.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Steps for setting the keys of a map as a priority when initializing a state variable in Typescript for a React component

I am working with a state variable that connects a string username to a UserData object. Initially, the usernames are stored in the string array 'users'. Is there a method to assign the initial state of 'userDataMap' with the keys fro ...

Challenges with handling click events in Angular Material's mat-sidenav-content

When I add a click event to the mat-sidenav-content element like this: <mat-sidenav-content (click)="isNavBarOpened=false">, the mat-slide-toggle inside it stops functioning. Check out the code example here ...

Solving runtime JavaScript attribute issues by deciphering TypeScript compiler notifications

Here is a code snippet I am currently working with: <div class="authentication-validation-message-container"> <ng-container *ngIf="email.invalid && (email.dirty || email.touched)"> <div class="validation-error-message" *ngIf=" ...

What is the best method for obtaining user data when additional custom data is stored in Cloud Firestore?

Storing the user's information such as email, name, age, phone number, and uid is essential. In the user.model.ts file: export interface User { uid: string, name: string, email: string, phoneNumber: string, age: string } In auth. ...

Leveraging constructors for injecting dependencies in Angular is a key practice for enhancing modularity and maintainability

After reviewing the Angular Official documents and various blogs, I noticed that there are two different syntaxes for Dependency Injection (DI) when used within the constructor. Sometimes this is utilized, while other times it is not. This leads to the que ...

When trying to access an array within a nested reactive form group, a linting error was encountered

I'm currently working on a method to delete rows from a dynamic form, but I am struggling to target the array. The structure of my form group is as follows: this.piForm = this.fb.group({ milestoneSaveModel: this.fb.group({ milestonesToCr ...

Leveraging *ngIf in conjunction with routerLink

How can I apply "ngIf" to a routerLink instead of using a button? For example: *ngIf="auth.isAuthenticated()" (click)="auth.logout()" Using: <a [routerLink]="['/']">Logout ...

Angular 5 Web-Based EPUB Reader

Just getting started with Angular 5 and I'm looking to display an epub file on a page without resorting to iframes. Any suggestions on how to achieve this? ...

The Same Origin Policy has prevented access to the remote resource located at http://localhost:8082/api/countries due to a Cross-Origin Request Block

Solution The XMLHttpRequest access to 'http://localhost:8082/api/countries' from the origin 'http://localhost:4200' has been blocked by the CORS policy. The response to the preflight request is failing the access control check because t ...

Auth0 Angular - No routes found to match

I recently set up an angular application and integrated it with Auth0 by following two helpful tutorials: https://auth0.com/docs/quickstart/spa/angular2/01-login https://auth0.com/docs/quickstart/spa/angular2/02-calling-an-api Here is a brief overview o ...

Angular 2: trigger a function upon the element becoming visible on the screen

How can I efficiently trigger a function in Angular 2 when an element becomes visible on the screen while maintaining good performance? Here's the scenario: I have a loop, and I want to execute a controller function when a specific element comes into ...

Tips for dynamically modifying style mode in Vue.js

I am looking to implement a feature where the style can be changed upon clicking a button. In my scenario, I am using Sass/SCSS for styling. For example, I have three different styles: default.scss, dark.scss, and system.scss. The code for dark.scss look ...

Implement Angular in a non-conventional Angular-based venture

Is there a way to incorporate Angular into a project that is built on a different framework? Specifically, I have a .NET MVC project that uses Razor for the markup. I want to make some ajax calls and update parts of the UI without using JQuery. Instead, I ...

What is the best way to integrate Angular npm into a TypeScript React project?

At my workplace, I've been tasked with working on a project using React and TypeScript. They also want me to integrate an Angular library from npm. I'm wondering if it's feasible to do this integration or if I should create my own library in ...

How to vertically align radio buttons with varying label lengths using Angular and Bootstrap 4?

Is there a way to properly align radio buttons with variable length labels? When each label has a different length, the radio buttons appear misaligned. How can this be fixed? <div class="row"> <div class="form-check form-check-inline" *ngFor="l ...

Having trouble accessing specific results using Firestore's multiple orderBy (composite index) feature

I am facing an issue with a query that I run on various data types. Recently, one of the collections stopped returning results after I included orderBy clauses. getEntitiesOfType(entityType: EntityType): Observable<StructuralEntity[]> { const col ...

how can I update a class of an element when the input box is disabled in angular?

Is there a way in Angular to change the class of a separate element (calendar icon button) when it detects that an input textbox is disabled? I have a disabled input type textbox and I want the class of the calendar icon button to be changed based on its d ...

typescript Parameter type dependency based on value is not functioning

interface AddDataRequest{ data:any } interface AddDataResponse{ id:string } interface ITest{ addData(json:AddDataRequest):Promise<AddDataResponse> removeData(json:AddDataResponse):Promise<boolean> } function testInterface<A e ...

TypeScript's version of Java's enum (or C#'s structure)

I'm facing the challenge of creating an enum in Typescript that mimics the functionality of Java enums. In TypeScript, only integer-based enums like C# are supported, unlike in Java where we can have custom objects with non-integer related properties ...

The checkbox in ng-2-smart-table will either be checked or unchecked based on the value of the column

I am working with an ng2-smart-table and I need the second column to be a checkbox that is initially checked or empty based on the binary value in the fourth column of my data object. Currently, my code looks like this: export class UsermanagementComponen ...