【问题标题】:How to sum up the values entered in the mat-table column using mat-input in angular2如何使用 angular2 中的 mat-input 总结在 mat-table 列中输入的值
【发布时间】:2018-08-07 01:04:32
【问题描述】:

我正在尝试使用输入总结在 mat-table 列中输入的值。

我创建了一个包含 3 列的表:Account Id、Account Description 和 Value。我还在资产文件夹中创建了一个 JSON 文件,其中包含 acc_id 和 acc_desc 的值。

然后我会在我的表中为各个列访问这些值。

我还创建了一个Value 列,我为其分配了输入字段。

在表格旁边,我使用了一个带有两个图块的简单网格,我想在其中获取 Value 列中输入的所有值的总和。

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 { }

如何总结我的值并在网格图块中显示总和? 我收到以下错误...

【问题讨论】:

    标签: angular angular-material


    【解决方案1】:

    试试减速器:

    <mat-grid-tile> Total: {{dataSource1.data.reduce((summ, v) => summ += v.value, 0)}}</mat-grid-tile>
    

    【讨论】:

    • 我尝试使用上面的代码......但它给了我以下错误,我在我的问题中发布了一个屏幕截图
    • 这是错误 ....compiler.js:485 Uncaught Error: Template parse errors: Parser Error: Missing expected ) at column 30 in [ Total: {{dataSource1.data.reduce(( summ, v) => summ += v.value, 0)}}] 在 ng:///AppModule/AccountComponent.html@33:17 (" [ERROR ->] 总计:{{dataSource1.data.reduce((summ, v) => summ += v.value, 0)}}tile> "): ng:///AppModule/AccountComponent.html@33:17 Parser Error: Unexpected token '=' at column 35
    【解决方案2】:

    试试这个

    在组件中

     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) 
    }
    

    在 HTML 中

    <mat-grid-tile> Total: {{calculation()}}</mat-grid-tile>
    

    还将&lt;input matInput value=0&gt; 更改为&lt;input matInput value="element.value"/&gt;

    编辑:

    根据聊天讨论,您需要使用 value 参数作为动态参数来计算

    所以试试下面的方法

    <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>
    

    【讨论】:

    • @Heena 现在你面临这个问题吗?
    • 您忙吗先生...?
    • @Heena 是的。但我在想你怎么能得到表外的值?
    • 请在您的输入字段中尝试使用value="element.value" 而不是value = 0。
    猜你喜欢
    • 2020-11-17
    • 1970-01-01
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 2019-12-05
    • 2020-01-10
    相关资源
    最近更新 更多