将多个值推送到 FormArray
Pushing Multiple Values to a FormArray
我真的被困在这里了。尝试在网上搜索,但没有发现任何有用的东西可以使用。
我正在尝试将值推送到 FormArray。问题是我是数组值,我需要它来将它插入到我的表单中。我只能添加一条记录。它将最后一个迭代的数组对象插入到 formArray 中。
代码如下:
.TS
salesResult: FormArray;
async ClickSearch() {
//this method get the dto with the mapped formValues.
const getSalesResult = this.salesEvidenceFormService.getSalesSearchFormGroup(
this.form
);
//the above method form values passed to the web-api to return result. I get 200 records here.
const salesSearchResults: any = await this.salesEvidenceService.getSalesSearchData(
getSalesResult
);
//getting the formArray. this I have used as an arry form. As shown in below code
this.salesResult = this.form.get('salesSearchResult') ;
for (let item of salesSearchResults) {
this.salesResult.controls[0].setValue(item);
//this.form.controls.salesSearchResult.value.address = item.address;
}
}
public async form(valuationId: any): Promise<FormGroup> {
const form: FormGroup = this.formBuilder.group({
id: valuationId,
siteAreaUnit: 'm2',
lettableAreaUnit: 'm2',
selectedDateRange: 0,
selectedQuickChoice: '',
metroNonMetro: 'ALL',
salesSearchResult: this.formBuilder.array([
this.getSalesSearchResultForm()
])
});
return await form;
}
getSalesSearchResultForm(): FormGroup {
return this.formBuilder.group({
id: this.formBuilder.control(''),
masterId: this.formBuilder.control(''),
address: this.formBuilder.control(''),
salePrice: this.formBuilder.control(''),
saleDate: this.formBuilder.control(''),
initialYield: this.formBuilder.control(''),
equivalentYield: this.formBuilder.control(''),
nlaPsm: this.formBuilder.control(''),
siteAreaPsm: this.formBuilder.control(''),
lettableArea: this.formBuilder.control(''),
wale: this.formBuilder.control(''),
description: this.formBuilder.control(''),
commercialSalesExists: this.formBuilder.control(''),
documentId: this.formBuilder.control(''),
totalRecord: this.formBuilder.control('')
});
}
我只想将记录插入到我的表单中。这样我就可以在 html 中对其进行交互,以显示从 API 返回的记录。我不知道我在这里做错了什么
我也遇到了类似的问题,post编辑了这个问题。我的post中"ysf"的回答帮我解决了。您可以在 "ysf" 的评论中找到解决方案。虽然 post 是旧的,但我希望它会是一些 help.Link 到 post 是:-
我用Push方法的时候就对了。下面是我实现的。一个一般的例子:
const tempContainer = this.formBuilder.array([]);
for (const current of formArray.controls) {
const existing = this.saveForm.controls.find(
x => x.value.Id === current.value.Id
);
if (existing) {
tempContainer.push(existing);
} else {
tempContainer.push(current);
}
}
我真的被困在这里了。尝试在网上搜索,但没有发现任何有用的东西可以使用。
我正在尝试将值推送到 FormArray。问题是我是数组值,我需要它来将它插入到我的表单中。我只能添加一条记录。它将最后一个迭代的数组对象插入到 formArray 中。
代码如下:
.TS
salesResult: FormArray;
async ClickSearch() {
//this method get the dto with the mapped formValues.
const getSalesResult = this.salesEvidenceFormService.getSalesSearchFormGroup(
this.form
);
//the above method form values passed to the web-api to return result. I get 200 records here.
const salesSearchResults: any = await this.salesEvidenceService.getSalesSearchData(
getSalesResult
);
//getting the formArray. this I have used as an arry form. As shown in below code
this.salesResult = this.form.get('salesSearchResult') ;
for (let item of salesSearchResults) {
this.salesResult.controls[0].setValue(item);
//this.form.controls.salesSearchResult.value.address = item.address;
}
}
public async form(valuationId: any): Promise<FormGroup> {
const form: FormGroup = this.formBuilder.group({
id: valuationId,
siteAreaUnit: 'm2',
lettableAreaUnit: 'm2',
selectedDateRange: 0,
selectedQuickChoice: '',
metroNonMetro: 'ALL',
salesSearchResult: this.formBuilder.array([
this.getSalesSearchResultForm()
])
});
return await form;
}
getSalesSearchResultForm(): FormGroup {
return this.formBuilder.group({
id: this.formBuilder.control(''),
masterId: this.formBuilder.control(''),
address: this.formBuilder.control(''),
salePrice: this.formBuilder.control(''),
saleDate: this.formBuilder.control(''),
initialYield: this.formBuilder.control(''),
equivalentYield: this.formBuilder.control(''),
nlaPsm: this.formBuilder.control(''),
siteAreaPsm: this.formBuilder.control(''),
lettableArea: this.formBuilder.control(''),
wale: this.formBuilder.control(''),
description: this.formBuilder.control(''),
commercialSalesExists: this.formBuilder.control(''),
documentId: this.formBuilder.control(''),
totalRecord: this.formBuilder.control('')
});
}
我只想将记录插入到我的表单中。这样我就可以在 html 中对其进行交互,以显示从 API 返回的记录。我不知道我在这里做错了什么
我也遇到了类似的问题,post编辑了这个问题。我的post中"ysf"的回答帮我解决了。您可以在 "ysf" 的评论中找到解决方案。虽然 post 是旧的,但我希望它会是一些 help.Link 到 post 是:-
我用Push方法的时候就对了。下面是我实现的。一个一般的例子:
const tempContainer = this.formBuilder.array([]);
for (const current of formArray.controls) {
const existing = this.saveForm.controls.find(
x => x.value.Id === current.value.Id
);
if (existing) {
tempContainer.push(existing);
} else {
tempContainer.push(current);
}
}