Unable to initiate ngModelChange event during deep cloning of value

I've been struggling to calculate the sum of row values, with no success. My suspicion is that the issue lies in how I am deep cloning the row values array when creating the row.

      const gblRowVal1 = new GridRowValues(1, this.color, this.headList.map(x => Object.assign(new RefQuantities(), x )), this.availableColors)

The ngModel does not seem to be pointing to the column value, causing the ngModelChange event not to trigger, thus resulting in the total not being calculated.

(ngModelChange)="onRowClick($event, row, headItem)"

Check out the Plunker grid here

Answer №1

If you want to easily calculate the total amount in your GridRowValues class, I suggest adding a new property called total. By implementing this property and using a change event like (ngModelChange), you can update the total amount whenever there is a change in the input values.

Modify your HTML template to include the change event for each input element:

<td *ngFor='#headItem of row.headList; #childIndex = index'>
  <input [(ngModel)]="headItem.value" 
         (ngModelChange)="onRowClick(row)" 
         type="number">
<td>

In the onRowClick method, iterate through the row.headList array, calculate the total based on the entered values, and assign it to the row.total property:

onRowClick(row) {
  let total = 0;
  row.headList.forEach(x => {
    if(x.value) {
      total += x.value;
    }
  })
  row.total = total;
}

Finally, display the total value by binding it to an input field similar to other ngModels:

<input readonly="readonly" [(ngModel)]="row.total" type="number">

For a live demo, you can check out the following link: DEMO

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

Can the geocoder API/search box be utilized to locate specific markers on a map?

Incorporating Mapbox into an Angular application with a high volume of markers on the map (potentially thousands) and hoping to implement a search box for users to easily locate specific markers based on unique names and coordinates. Is this functionalit ...

Next.js routes handlers do not have defined methods parameters

Struggling to find the cause of undefined params Currently delving into the world of Nextjs api routes, I keep encountering an issue where my params are coming up as undefined when trying to use them in the HTTP method. My setup includes prisma as my ORM ...

Exploring Angular 4 with the power of Rangy modules

I've recently started working with Angular 4 and decided to create a basic app by forking the Angular quickstart. Now, I'm facing a challenge as I try to incorporate Rangy into my project. In my package.json, the dependencies are listed as follo ...

Error message: Trying to use the data type 'String' as an index in the React dynamic component name map won't work

I have successfully implemented the code below and now I am attempting to convert it to Typescript. However, even though I can grasp the error to some extent, I am unsure of how to correct it. At present, I am allowing a component to have a prop called "i ...

Error: FullCalendar does not display a header for the timeGridWeek view when the dates fall

Currently, I am integrating fullcalendar 5.5.0 with Angular 10. After migrating from fullcalendar v4 to v5, I noticed an annoying issue where the header for the date before the validRange start is no longer displayed: These are the parameters being used: ...

Dealing with multiple queryParams in Angular2: Best practices

Need help implementing a filtering mechanism in my new Angular2 app. Looking to filter a list of entries in an array based on around 20 properties. I've set up filters in one component and a list component as a child that is routed to. Initially, pas ...

Error encountered in Typescript: The property 'prevUrl' is expected to be present in the props object, but it appears to be missing

When trying to access the props.prevUrl property, the following error is encountered: Property 'prevUrl' does not exist on type '{ nextUrl: string; } | { prevUrl: string; nextUrl: string; } | { prevUrl: string; confirm: () => void; }&apos ...

Mastering the Art of Injecting Objects from the Server

Utilizing Angular Universal, I am serving my Angular application through an Express server. The objective is to embed an environment object (from the server) into my application. To achieve this, I have created an InjectionToken export const ENVIRONMENT ...

Switch things up in the mat-tree angular

Is there a way to change the orientation of a mat-tree-node to "RTL" and adjust the padding to be on the right side? <mat-tree [dataSource]="dataSource" [treeControl]="treeControl"> <mat-tree-node *matTreeNodeDef="let node" matTreeNodeToggl ...

The absence of class generic parameter constraints cascades down to class properties

Trying to implement TypeScript with TypeORM using a generic class has been challenging. The issue lies within my AbstractDal which takes a generic parameter that is constrained to extend IEntity. IEntity is supposed to have an id property, yet for some rea ...

How to Generate a JPG File from a Leaflet Map in Angular 4 using Typescript

I am developing a custom application using Angular4 that involves integrating leaflet maps. One of the requirements is to export the current view of a map as a JPG image, capturing only the map with markers and polylines - similar to taking a screenshot. ...

Guide to adding annotations to a PDF document using Angular 2

As a novice in the field of Angular development, I am seeking guidance. Currently, I have an application that displays PDF files. My goal is to be able to annotate and make changes on these PDF files by adding drawings such as circles, lines, or text. Ho ...

Utilizing Angular's custom builder for asset revisioning

After coming across this specific inquiry on Stack Overflow, my goal is to implement a @angular-builders/custom-webpack:browser with a personalized Webpack setup that incorporates html-loader and file-loader to alter the names of asset files with their has ...

Issue with ngStyle not functioning properly with conditional operator

I am currently learning how to use Angular4 ngStyle by going through a tutorial. Here's the code I have been working on: app.component.html <button [ngStyle]="{ 'backgroundColor': canSave ? 'blue': 'gray', ...

Angular service tested using the put method in a test scenario

Currently, I am delving into test cases using Jasmin and karma to enhance my skills. One interesting project involves creating an Angular service that updates records in a database. While I have experience creating test cases, I must admit that testing met ...

Receiving an `Invalid module name in augmentation` error is a common issue when using the DefaultTheme in Material

After following the guidelines outlined in the migration documentation (v4 to v5), I have implemented the changes in my theme: import { createTheme, Theme } from '@mui/material/styles' import { grey } from '@mui/material/colors' declar ...

Angular4 allows the creation of new rows when products are added to a carousel component

Currently, I am working on an Angular4 application that includes a carousel displaying popular products. At the moment, the default view shows 3 products, and clicking on the left or right buttons reveals another set of 3 products. The total static values ...

Use the mat-slide-toggle to switch up the text style

Is it possible to apply different text styles using Ang-Material-2's mat-slide-toggle? https://i.stack.imgur.com/WM9FC.jpg <mat-slide-toggle [color]="primary" [checked]="true">Slide me</mat-slide-toggle> <h3>Text</h3> ...

Error with the type of CanvasGradient in the NPM package for converting text to image

I attempted to generate an image using a specific text by utilizing npm's text-to-image package, but encountered an error during typescript compilation. The errors I encountered upon running the typescript compilation command are related to files with ...

A guide on how to initiate a click event in Angular 5 using JQuery

I am trying to trigger a click event for each element based on its id, but it doesn't seem to be working. Below is the code I am using: ngOnInit() { this.getProductsLists(); } getProductsLists() { this.supplierService.getProductLists() .sub ...