I am trying to sum up the values entered in the mat-table column using an input.
I have created a table with 3 columns: Account Id, Account Description and Value. I have also created a JSON file in the assets folder with the values for acc_id and acc_desc .
I then access these values in my table for the respective columns.
I have also created a Value column for which i have assigned input field.
Next to the table i took a simple grid with two tiles where i want to take the sum of all the values entered in the Value column.
account.component.html
<!-- Table starts here -->
<mat-card>
<div class="example-container mat-elevation-z8">
  <mat-table #table [dataSource]="dataSource1">
    <!-- Account No. Column -->
    <ng-container matColumnDef="acc_id">
      <mat-header-cell *matHeaderCellDef> Account ID. </mat-header-cell>
      <mat-cell *matCellDef="let element">{{element.acc_id}}</mat-cell>
    </ng-container>
    <!-- Account Description Column -->
    <ng-container matColumnDef="acc_des">
      <mat-header-cell *matHeaderCellDef> Account Description </mat-header-cell>
      <mat-cell *matCellDef="let element">{{element.acc_des }} </mat-cell>
    </ng-container>
        <!-- Value Column -->
    <ng-container matColumnDef="value">
      <mat-header-cell *matHeaderCellDef> Value </mat-header-cell>
      <mat-cell *matCellDef="let element"><input matInput value=0> </mat-cell>
    </ng-container>
    <mat-header-row *matHeaderRowDef="displayedColumns1" ></mat-header-row>
    <mat-row *matRowDef="let row; columns: displayedColumns1;" > </mat-row>
  </mat-table>
   <mat-grid-list cols="2" rowHeight="20:1">
  <mat-grid-tile> Total</mat-grid-tile>
 <mat-grid-tile> {{dataSource1.data.reduce((summ, v) => summ += v.value, 0)}} </mat-grid-tile>
</mat-grid-list>
</div>
</mat-card>
account.component.ts
import {Component, OnInit} from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import { ReactiveFormsModule, FormGroup,FormControl } from '@angular/forms';
import { MatTableDataSource} from '@angular/material';
import { AccountdetailService } from '../accountdetail.service';
@Component({
  selector: 'app-account',
  templateUrl: './account.component.html',
  styleUrls: ['./account.component.scss']
   })
export class AccountComponent implements OnInit {
constructor( private accdetailservice: AccountdetailService ) { }
  /* Table Starts here
  ---------------------- */
 displayedColumns1 = ['acc_id', 'acc_des', 'value'];
 dataSource1= new MatTableDataSource<Element>(ELEMENT_DATA);
ngOnInit(){
  this.accdetailservice.accountdetails()
  .subscribe(data =>
     this.dataSource1.data = data;
  ); }
 }
const ELEMENT_DATA: Element[] = [];
accountdetails.service.ts
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class AccountdetailService {
  constructor(private http:Http ) { }
  accountdetails()
  {
    return this.http.get('/assets/accountdetails.json')
    .map(result => result.json());
  }}
accountdetails.json
[
    {
        "acc_id": 1001,
        "acc_des": "aaa"
    },
    {
        "acc_id": 1002,
        "acc_des": "aaa"
    }
]
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ReactiveFormsModule,FormsModule } from '@angular/forms';
import { HttpModule} from '@angular/http';
import { AppMaterialModule } from './app-material.module';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { AccountComponent } from './account/account.component';
import { AccountdetailService } from './accountdetail.service';
@NgModule({
  declarations: [
    AppComponent,
    AccountComponent    
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    ReactiveFormsModule,
    BrowserAnimationsModule,
    AppMaterialModule,
    FormsModule ,
    HttpModule   
  ],
  entryComponents:[ ],
  providers: [ AccountdetailService],
  bootstrap: [AppComponent]
})
export class AppModule { }
How can i sum up my values and display the sum in the grid tile? I am getting the below error...


try this
In component
 ngOnInit(){
  this.accdetailservice.accountdetails()
  .subscribe(data =>
  this.dataSource1.data= data.map(obj => ({...obj, value: 0}))
  );
 }
calculation(){ 
return dataSource1.data.reduce((summ, v) => summ += parseInt(v.value), 0) 
}
in Html
<mat-grid-tile> Total: {{calculation()}}</mat-grid-tile>
also change <input matInput value=0> to <input matInput value="element.value"/>
as per the chat discussion you need to use value parameter as a dynamic parameter to calculate
So try this below way
<ng-container matColumnDef="value">
   <mat-header-cell *matHeaderCellDef> Value </mat-header-cell>
   <mat-cell *matCellDef="let element">
     <ng-container>
       <input  [(ngModel)]="element.value">
     </ng-container>
   </mat-cell>
    </ng-container>
                        Try a reducer:
<mat-grid-tile> Total: {{dataSource1.data.reduce((summ, v) => summ += v.value, 0)}}</mat-grid-tile>
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With