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

Altering the dimensions of radio buttons

I am a newcomer to using material-ui. I am currently working on incorporating radio buttons in a component and would like to reduce its size. While inspecting it in Chrome, I was able to adjust the width of the svg icon (1em). However, I am unsure how to a ...

What is the best method to adjust the width of the PrimeNG ConfirmDialog widget from a logic perspective

Currently utilizing "primeng": "^11.2.0" and implementing the ConfirmDialog code below this.confirm.confirm({ header: 'Announcement', message: this.userCompany.announcement.promptMsg, acceptLabel: this.userCompany.announcement ...

Adjusting various angular-cli configuration files or providing input variables

My application caters to different customers, requiring personalized configurations based on their needs. I am looking for a way to customize the settings in the angular-cli.json file each time I run ng build. Is there a method to: 1) Dynamically cha ...

The 'any' type is not compatible with constructor functions

I am currently working on implementing a class decorator in Typescript. I have a function that accepts a class as an argument. const createDecorator = function () { return function (inputClass: any) { return class NewExtendedClass extends inputClass ...

JS implementing a listener to modify a Google Map from a separate class

Currently, I am in the process of migrating my Google Map functionality from ionic-native to JavaScript. I am facing an issue while attempting to modify the click listener of my map from a separate class. The problem seems to be related to property errors. ...

What is the procedure for entering the output generated by genMockFromModule when creating a custom mock in Jest?

Looking at my orders directory structure, I have a function in the orders/helpers file that I want to test using a manual Jest mock. orders __mocks__ helpers.ts __tests__ orders.ts helpers.ts orders.ts The orders/helpers.ts file contains ...

What steps are required to transition an Angular application developed without the Angular CLI into an Angular CLI project?

I've created an Angular app using tools like NPM (without utilizing the Angular CLI). What would be the most efficient way to transition this project into the CLI project structure? I want to have the ability to utilize commands like ng serve. ...

Removing a selected row from a data table using an HTTP request in Angular 2

I am working with a table that has data retrieved from the server. I need to delete a row by selecting the checkboxes and then clicking the delete button to remove it. Here is the code snippet: ...

When using Observables in AngularFire2, the $ref property does not get captured

Recently, I started working with AngularFire2 (version 4.0.0-rc.1) and encountered a challenge that has me stuck: getWishlist$(): FirebaseListObservable<{}> { return <FirebaseListObservable<{}>>this.store.select(getFirebaseUID) ...

Encountering the error message "Angular: invalid URL value sanitization" every time I attempt to refer to a folder within

I'm currently working on a project where I need to pull images from my disk into an Angular application using PHP as the backend for file retrieval. <ngb-carousel *ngIf="imageNames" class="carousel-fade"> ...

Creating a contravariant function member in TypeScript?

I am facing a challenge with a class that contains a member which is a function taking an instance of the same class: class Super { public member: (x: Super) => void = function(){} use() {const f = this.member; f(this)} } However, I need the me ...

Is it possible to implement a customized pathway for the functions within an Azure function app?

Recently, I set up a new function app on Azure using Azure Functions Core Tools with Typescript as the language. The app includes a test function named MyTestFunction that responds with an HTTP response when called. This particular function is located in ...

Encountering issues with Google authentication functionality while using AngularFire2

Currently in the process of setting up Google Authentication with Firebase and AngularFire2. The setup seems to be partially functional, however, there are some unusual behaviors that I am encountering. Upon the initial loading of the app, the Login button ...

An error occurred while attempting to generate two requests on the API

My online store consumes an API, but I am having trouble with the requests that the site is making. Every time I make a request, I receive two: one with OPTIONS and another with POST. This causes the API to become jumbled up. Can anyone offer some assist ...

Angular's getter value triggers the ExpressionChangedAfterItHasBeenCheckedError

I'm encountering the ExpressionChangedAfterItHasBeenCheckedError due to my getter function, selectedRows, in my component. public get selectedRows() { if (this.gridApi) { return this.gridApi.getSelectedRows(); } else { return null; } } ...

Deleting a key from a type in TypeScript using subtraction type

I am looking to create a type in TypeScript called ExcludeCart<T>, which essentially removes a specified key (in this case, cart) from the given type T. For example, if we have ExcludeCart<{foo: number, bar: string, cart: number}>, it should re ...

typegrapql encounters an issue with experimentalDecorators

I'm currently delving into TypeGraphQL and working on building a basic resolver. My code snippet is as follows: @Resolver() class HelloReslover { @Query(() => String) async hello(){ return "hello wtold" } } However, ...

A guide to displaying previous and next data on hover in Angular 2

I have developed a basic Angular 2 application and everything is functioning correctly. In the Project Detail section, the next and previous buttons work fine - clicking them displays the respective data on the view. However, I am facing an issue where hov ...

How to execute a function in a child component that is declared in the parent component using Angular

Is anyone able to help me with an issue I am facing in my Angular project? I have two components, 'app' and 'child'. Within the child component, I have a button that calls a function defined in the app component. However, this setup is ...

The perplexing behavior of RxJS Observables with Mongo Cursors

Recently, I've been working on converting a mongo cursor into an observable using my own RxJS implementation. Despite finding numerous solutions online, I wanted to challenge myself by creating one from scratch. I would greatly appreciate it if someo ...