[英]Property 'value' does not exist on type Element
我試圖總結使用輸入在mat-table
列中輸入的值。
我創建了一個包含3列的表格: Account Id
, Account Description
和Value
。 我還在資產文件夾中用acc_id
和acc_desc
的值創建了一個JSON文件。
然后,我在表格中訪問相應列的這些值。
我還創建了一個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">
<ng-container>
<input [(ngModel)]="element.value">
</ng-container>
</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 style="align-self: left;"> Total</mat-grid-tile>
<mat-grid-tile>{{calculation()}}</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 {
element: string;
value: any;
constructor( private accdetailservice: AccountdetailService ) { }
/* Table Starts here
---------------------- */
displayedColumns1 = ['acc_id', 'acc_des', 'value'];
dataSource1= new MatTableDataSource<Element>(ELEMENT_DATA);
calculation(){
return this.dataSource1.data.reduce((summ, v) => summ += parseInt(v.value), 0)
}
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 { }
我可以對在“值”列中輸入的值進行匯總,但是出現以下錯誤。
有人可以告訴我如何在表中使用value屬性而不在Element中定義它嗎 ?
這是我的輸出。
在您的ngOnInit
函數中嘗試此ngOnInit
。 您需要動態創建value
屬性。
ngOnInit(){
this.accdetailservice.accountdetails().subscribe(data =>
let tempObject = data.map(obj => ({...obj, value: 0}));
this.dataSource1.data = tempObject;
);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.