我正在尝试使用输入总结在public void openbrowser(String browsername, String URL){
if(browsername.equalsIgnoreCase("broserstack")){
browserstack_setup();
}else if(browsername.equalsIgnoreCase("CH")){
System.setProperty("webdriver.chrome.driver",chromedriverpath);
driver=new ChromeDriver();
driver.get(URL);
}
}
列中输入的值。
我创建了一个包含3列的表格:mat-table
,Account Id
和Account Description
。我还在assets文件夹中创建了一个JSON文件,其中包含Value
和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
如何总结我的值并在网格图块中显示总和? 我收到以下错误...
答案 0 :(得分:4)
尝试减速机:
<mat-grid-tile> Total: {{dataSource1.data.reduce((summ, v) => summ += v.value, 0)}}</mat-grid-tile>
答案 1 :(得分:1)
试试这个
在组件
中 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>
也将<input matInput value=0>
更改为<input matInput value="element.value"/>
根据聊天讨论,您需要使用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>