gpt4 book ai didi

forms - Angular 4 Form FormArray Add a Button 添加或删除表单输入行

转载 作者:太空狗 更新时间:2023-10-29 16:50:56 25 4
gpt4 key购买 nike

我正在使用 Angular 4.0.2 构建应用程序。如何向表单添加按钮以添加新的输入行和删除特定行的删除按钮?我的意思是我想要一个像这样的表格。我希望我的表单看起来像这样:

Form Image .

这是我的代码:

添加发票.component.html

    <h3 class="page-header">Add Invoice</h3>
<form [formGroup]="invoiceForm">
<div formArrayName="itemRows">
<div *ngFor="let itemrow of itemRows.controls; let i=index" [formGroupName]="i">
<h4>Invoice Row #{{ i + 1 }}</h4>
<div class="form-group">
<label>Item Name</label>
<input formControlName="itemname" class="form-control">
</div>
<div class="form-group">
<label>Quantity</label>
<input formControlName="itemqty" class="form-control">
</div>
<div class="form-group">
<label>Unit Cost</label>
<input formControlName="itemrate" class="form-control">
</div>
<div class="form-group">
<label>Tax</label>
<input formControlName="itemtax" class="form-control">
</div>
<div class="form-group">
<label>Amount</label>
<input formControlName="amount" class="form-control">
</div>
<button *ngIf="i > 1" (click)="deleteRow(i)" class="btn btn-danger">Delete Button</button>
</div>
</div>
<button type="button" (click)="addNewRow()" class="btn btn-primary">Add new Row</button>
</form>
<p>{{invoiceForm.value | json}}</p>

这是我的 add-invoice.component.ts 代码

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormControl, FormArray, FormGroup } from '@angular/forms';

@Component({
selector: 'app-add-invoice',
templateUrl: './add-invoice.component.html',
styleUrls: ['./add-invoice.component.css']
})

export class AddInvoiceComponent implements OnInit {
invoiceForm: FormGroup;

constructor(
private _fb: FormBuilder
) {
this.createForm();
}

createForm(){
this.invoiceForm = this._fb.group({
itemRows: this._fb.array([])
});
this.invoiceForm.setControl('itemRows', this._fb.array([]));
}

get itemRows(): FormArray {
return this.invoiceForm.get('itemRows') as FormArray;
}

addNewRow(){
this.itemRows.push(this._fb.group(itemrow));
}

ngOnInit(){

}
}

最佳答案

这是您的代码的简化版本:

当你初始化你的表单时,你可以在你的 formArray 中添加一个空的表单组:

ngOnInit() {
this.invoiceForm = this._fb.group({
itemRows: this._fb.array([this.initItemRows()])
});
}

get formArr() {
return this.invoiceForm.get('itemRows') as FormArray;
}

然后是函数:

initItemRows() {
return this._fb.group({
// list all your form controls here, which belongs to your form array
itemname: ['']
});
}

这是 addNewRowdeleteRow 函数:

addNewRow() {
this.formArr.push(this.initItemRows());
}

deleteRow(index: number) {
this.formArr.removeAt(index);
}

您的表单应如下所示:

<form [formGroup]="invoiceForm">
<div formArrayName="itemRows">
<div *ngFor="let itemrow of formArr.controls; let i=index" [formGroupName]="i">
<h4>Invoice Row #{{ i + 1 }}</h4>
<div>
<label>Item Name</label>
<input formControlName="itemname">
</div>
<button type="button" (click)="deleteRow(i)" *ngIf="formArr.controls.length > 1" >
Delete
</button>
</div>
</div>
<button type="button" (click)="addNewRow()">Add new Row</button>
</form>

这是一个

DEMO

关于forms - Angular 4 Form FormArray Add a Button 添加或删除表单输入行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43520010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com