【发布时间】:2020-01-13 10:32:34
【问题描述】:
<form [formGroup]="cashflowForm">
<mat-tab-group [selectedIndex]="selected.value" (selectedIndexChange)="selected.setValue($event)">
<mat-tab *ngFor="let tab of tab; let index = index" [label]="tab">
<mat-tab-group>
<mat-tab *ngFor="let tabz of tabs; let i=index " [label]="tabz.Name">
<div formArrayName="itemRows" *ngFor="let item of cashflowForm.get('itemRows').controls">
<div [formGroupName]="0">
<table>
<thead>
<tr>
<th>
Estimated Cash Flows Date
</th>
<th>
Principle Amount
</th>
<th>
Interest Amount
</th>
<th>
Total
</th>
<th>
PV of Estimated Cash Flows
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="text" name="ECFDate" formControlName="ECFDate" />
</td>
<td>
<input type="text" name="principalFCValue" formControlName="principalFCValue" />
</td>
<td>
<input type="text" name="intrestCRecovery" formControlName="intrestCRecovery" />
</td>
<td>
<input type="text" name="cashflowtotal" formControlName="cashflowtotal" />
</td>
<td>
<input type="text" name="PVEstablishCash" formControlName="PVEstablishCash" />
</td>
</tr>
</tbody>
</table>
<table>
<tr>
<td> <a href="javascript:void(0);" (click)="addNewTab(tabz.id)">Add Row</a></td>
</tr>
</table>
</div>
</div>
</mat-tab>
</mat-tab-group>
</mat-tab>
</mat-tab-group>
</form>
<router-outlet></router-outlet>
import { Component } from '@angular/core';
import { FormControl, FormGroup, FormBuilder, Validators, FormArray } from '@angular/forms';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'angularMaterialTask';
public cashflowForm: FormGroup;
tab = ['First', 'Second'];
tabs = ['Inside First']
selected = new FormControl(0);
constructor(private fb: FormBuilder){}
ngOnInit(){
this.cashflowForm = this.fb.group({
itemRows: this.fb.array([this.addItemRows()])
});
}
addItemRows(): FormGroup{
return this.fb.group({
ECFDate: [''],
principalFCValue: ['', Validators.required],
intrestCRecovery: ['', Validators.required],
cashflowtotal: [''],
PVEstablishCash: ['', Validators.required]
});
}
addNewTab(){
(<FormArray>this.cashflowForm.get('itemRows')).push(this.addItemRows());
}
removeTab(index: number) {
this.tab.splice(index, 1);
}
}
我有一个包含两个主要选项卡的表单,第一和第二。我想在每个选项卡中添加一些控件,但是每个选项卡的内容可能相同也可能不同。 如何为每个标签添加动态内容。
现在代码正在运行。但它正在向两个选项卡添加控件。当我单击 AddRow 按钮时,它会添加控件,但它反映在所有选项卡(第一个和第二个)中。
下图描述相同。 enter image description here
【问题讨论】:
-
addNewTab(){ (
this.cashflowForm.get('itemRows')).push(this.addItemRows()); } -
在此处添加您的 html 和 compoent.ts 代码!
-
上面是用来添加控件的函数
-
代码已添加