Determine the `overall` amount and adjust `overall` to equal `quantity * price` within a Typescript

I am looking to perform a calculation similar to this: total = quantity * price and continuously update the total when either the quantity or the price changes.

template-output-snapshot

app.component.html

<form [formGroup]="editform" (ngSubmit)="edisale()" class="col s12" materialize>
    <div class="form-group">
        <table align="center" class="table table-bordered table-hover">
            <thead>
                <tr style="color:black;">
                    <th>Price</th>
                    <th>Quantity</th>
                    <th>Notes</th>
                    <th>Subtotal</th>
                </tr>
            </thead>
            <tbody>
                <tr formArrayName="products" class="group" style="cursor: pointer" *ngFor="let sale of editform.get('products').value; let i = index" [formGroupName]="i">
                    <td>
                        <input class="form-control" id="p_unit_price " type="number" class="validate" [value]="sale.p_unit_price" ng-change="tot()">
                    </td>
                    <td>
                        <input class="form-control" type="text" formControlName="p_quantity" id="p_quantity" name="p_quantity" [value]="sale.p_quantity" ng-change="tot()">
                    </td>
                    <td>
                        <div class="input-field col s2">
                            <input class="form-control" formControlName="p_description" id="p_description" type="text" class="validate" [value]="sale.p_description">
                        </div>
                    </td>
                    <td>
                        <input class="form-control" type="text" formControlName="p_subtotal" id="p_subtotal" name="p_subtotal" [value]="(sale.p_subtotal)">
                    </td>
                    <td>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    <br>
    <br>
    <div class="row">
        <div class="input-field col s2" style="float: right;">
            Total: {{Total}} ALL
            <input class="form-control" formControlName="total" id="total" type="text" class="validate" [value]="totale">
        </div>
        <div class="form-control" class="input-field col s2" style="float: right;">
            Amount Paid:
            <input class="form-control" formControlName="amount_paid" id="amount_paid" type="text" class="validate">
        </div>
        <div class="input-field col s2" style="float: right;">
            Subtotal:
            <input formControlName="subtotal" id="subtotal" type="text" class="validate" [value]="Total">
        </div>
    </div>
</form>

app.component.ts

tot() {
    return this.products
        .map(pro => pro.p_unit_price * pro.p_quantity)
        .reduce((a, b) => a + b, 0);
}

Despite modifying the quantity or price, the total remains unchanged. Seeking suggestions or explanations regarding any issues in my code would be greatly appreciated.

Your assistance is highly valued. Thank you!

Answer №1

Implement (modify)="someAction()" and establish bidirectional binding by using [(ngModel)]="variable1" on your input field

export class MyComponent {

variable1; variable2; variable3;
  constructor(
  ) { }
updateValue(){
// This method is triggered when the user changes the input, allowing you to update the corresponding variable.
}

}

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

What is the best way to remove an element from an array and add a new one?

Here is the array that I am working with: [ { "id": "z12", "val": "lu", "val2": "1", }, { "id": "z13", "val": "la", "val2" ...

Enhanced hierarchical organization of trees

I came across this code snippet: class Category { constructor( readonly _title: string, ) { } get title() { return this._title } } const categories = { get pets() { const pets = new Category('Pets') return { ge ...

How to set focus on an input element in Angular 2/5

After clicking, the input element is displayed (it is initially hidden). I want this input to be in focus when it appears. This functionality works well when the input is not hidden by *ngIf (#inputElement2). Is there a way to achieve the same for #inputE ...

What is the best way to test for errors thrown by async functions using chai or chai-as-promised?

Below is the function in question: async foo() : Promise<Object> { if(...) throw new Error } I'm wondering how I should go about testing whether the error is thrown. This is my current approach: it("checking for thrown error", asy ...

Discovering the best approach to utilizing Font Awesome 6 with React

Required Packages "@fortawesome/fontawesome-svg-core": "^6.1.1", "@fortawesome/free-solid-svg-icons": "^6.1.1", "@fortawesome/react-fontawesome": "^0.1.18", "next": " ...

Is there a way to refresh an input field in Angular when the model hasn't necessarily been modified, but the value in the input field has?

I am currently dealing with an input field that is linked to a model and has restrictions in place (no special characters). To handle this, I have created a custom pipe that removes any invalid characters. Although this solution works for the most part, I ...

Step-by-step guide on integrating a custom JS file into an Angular component

Trying to grasp Angular, I embarked on adding some JavaScript logic to one of my components from a separate JS file. While following advice from a similar query (How to add custom js file to angular component like css file), it seems I missed something cru ...

Angular indicates that there is a problem with the property 'x' as it is not found in the type 'y[]'

I am trying to display the details of users using the *ngFor directive, but encountered an error earlier. Here is the code snippet: this.users=[ // first user { firstName: 'John', lastName: 'Doe', c ...

npm unable to locate a JavaScript file

Currently, I am developing an Angular 2 application that utilizes the ng2-slugify package. However, I have encountered an issue where it cannot locate one of the required slugify files, specifically "charmaps.js", even though it is stored in the same direc ...

Is the ID Column in the Minimal Material Table Demo not appearing as expected?

Hey there, I'm in the process of developing a simple demonstration of a material table - Take note that this is a stackblitz link and for some reason, the id column isn't showing up. Here's a snippet from my app.component.ts: import { C ...

Troubleshooting notifications generated by react-hook-form and zod

My customer registration form is causing all my error messages to show as "required." I suspect it might be a misconfiguration in zod or react-hook-form. Below, you'll find the code snippets. This is my generic input component: import { DetailedHTMLP ...

Restricting a checkbox to a maximum of 5 checkmarks

In a multi-column table, each column is represented by a checkmark. I want to limit the ability to tick a checkmark to only 5 checkmarks. Here is the code that has been implemented: <tbody> <ng-container *ngFor="let col of testData" ...

Validation scheme for the <speak> element

When using validators in an angular formarray for input fields, I encountered a challenge with the regex to check the <speak> tag. https://i.sstatic.net/ogFA3.png The content provided was considered valid. https://i.sstatic.net/ar5FJ.png An error ...

There is no such property as formData.delete()

When I am using a FormData object to upload a file, I want to add functionality to delete the file from FormData. However, I encountered an error stating that the delete property does not exist on the FormData object. formData.delete(fileName) Code uplo ...

Can the WebSocket interface be substituted with WebSocket itself?

I'm currently in the process of setting up a WebSocket.Server using ws, Express 4, NodeJS, and TypeScript by following a guide. However, I've encountered an issue with the provided code not working as expected from the tutorial found at . In ord ...

Arrangement of code: Utilizing a Node server and React project with a common set of

Query I am managing: a simple react client, and a node server that functions as both the client pages provider and an API for the client. These projects are tightly integrated, separate TypeScript ventures encompassed by a unified git repository. The se ...

When trying to access instance member variables, Observable does not allow it

Within the hotelService file, I am retrieving data from the backend API that contains information about hotels. hotelService file import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders} from "@angular/common/http"; impor ...

Transitioning from AngularJS to Angular 2: Exploring Alternatives to $rootScope.$on

Our journey with the AngularJS project has begun on the path towards the modern Angular. The ngMigration utility advised me to eliminate all dependencies on $rootScope since Angular does not have a concept similar to $rootScope. While this is straightforw ...

Exploring the Angular RouterModule within a Java WAR Deployment

In my Angular 6.0.5 application, I leverage Angular's Routing feature to define paths like: http://localhost:8080/area http://localhost:8080/barn http://localhost:8080/tower During development, running the app with ng serve allows me to directly en ...

Received corrupted file during blob download in Angular 7

When using Angular 7, I am making an API call by posting the URL file and attempting to download it using the 'saveAs' function from the fileSaver library. The file is successfully downloading, but it appears to be corrupted and cannot be opened. ...