[英]Update Value in a Table using Reactive Forms Method
It's a bit difficult to give a simple example...举个简单的例子有点困难……
I have a model and its price displayed and an edit button in a table, when I click on the edit button, a Modal is displayed to modify the price.我有一个 model 并在表格中显示其价格和一个编辑按钮,当我单击编辑按钮时,会显示一个模态来修改价格。
In the Modal, there is an Input and a Validate Button, in the Input, the initial Price is displayed.在 Modal 中,有一个 Input 和一个 Validate 按钮,在 Input 中,显示初始价格。 I managed to display the initial price with the Get SetValue method of Reactive Forms.
我设法使用 Reactive Forms 的 Get SetValue 方法显示了初始价格。
The problem is that I would like when I modify the price and I validate it, it is displayed in the table which contained the initial price.问题是我想当我修改价格并验证它时,它会显示在包含初始价格的表格中。
Here are some screenshots to illustrate the problem:以下是一些截图来说明问题:
HTML Code :
<table class="table table-dark mb-3">
<thead>
<tr>
<th scope="col">Modèles</th>
<th scope="col">Prix Unitaire ( U )</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody *ngFor="let modele of modeles; let i = index">
<tr>
<td>Model {{ modele.idModele }} ( {{ modele.modele }} )</td>
<td>{{ modele.prix }} €</td>
<td>
<a
class="btn btn-outline-light"
(click)="editPrixModele(i)"
data-toggle="modal"
data-target="#modalPrixModele"
>
<i class="far fa-edit"></i
></a>
</td>
</tr>
</tbody>
</table>
<!-- Modal Edition Prix Model Start -->
<div
class="modal fade"
id="modalPrixModele"
tabindex="-1"
role="dialog"
aria-labelledby="exampleModalLabel"
aria-hidden="true"
>
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="modalPrixModele">Edition Du Prix :</h5>
<button
type="button"
class="close"
data-dismiss="modal"
aria-label="Close"
>
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form [formGroup]="prixModeleForm">
<label for="prixModele">Prix Modele : </label>
<input
type="text"
class="form-control"
placeholder="Entrer le Prix : ( € )"
formControlName="prixModele"
/>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">
Fermer
</button>
<button
type="button"
class="btn btn-primary"
(click)="validatePrixModele()"
>
Enregistrer
</button>
</div>
</div>
</div>
</div>
<!-- Modal Edition Prix Model End -->
TS File :
export class PrixOptionsComponent implements OnInit {
modeles = [];
travaux = [];
batiments = [];
nbrEtages = [];
chargeNominale = [];
nbrPersonnes = [];
dimensionCabine = [];
prixModeleForm: FormGroup;
constructor(
private prixOptionsService: PrixOptionsService,
private formbuilder: FormBuilder
) {}
ngOnInit() {
this.prixOptionsService.getModeles().subscribe(
(data: any) => {
this.modeles = data;
},
(error) => {
console.error(error);
},
() => {
console.log("observable modele success");
}
);
this.initPrixModeleForm();
}
initPrixModeleForm() {
this.prixModeleForm = this.formbuilder.group({
prixModele: [""],
});
}
editPrixModele(index) {
console.log(this.modeles[index]);
this.prixModeleForm.get("prixModele").setValue(this.modeles[index].prix);
}
Thank you in advance,先感谢您,
I would change somes little things: In HTML, i would pass, the modele itself, instead of the index:我会改变一些小事情:在 HTML 中,我会通过模型本身,而不是索引:
<a
class="btn btn-outline-light"
(click)="editPrixModele(modele)"
data-toggle="modal"
data-target="#modalPrixModele"
>
<i class="far fa-edit"></i>
</a>
I would keep this modele, into a property editModele:我会把这个模型保存到一个属性editModele中:
export class PrixOptionsComponent implements OnInit {
...
editModele: any = null;
...
}
So we need to change the editPrixModele function:所以我们需要修改editPrixModele function:
editPrixModele(modele) {
this.editModele = modele;
this.prixModeleForm.get("prixModele").setValue(this.editModele);
}
finally, in the validateForm function:最后,在 validateForm function 中:
validatePrixModele() {
// Check here, if value is correct ....
this.editModele.prix = this.prixModeleForm.value.prixModele;
this.editModele = null;
//Close your modal ....
this.prixModeleForm.reset();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.