简体   繁体   English

使用反应式 Forms 方法更新表中的值

[英]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">&times;</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.

相关问题 如何使用角电抗形式从多个输入中计算值? - How to calculate value from multiple inputs using angular reactive forms? 如何使用反应式获取由插值代码编写的输入值? - How to get the input value written by interpolation code using Reactive Forms? 在模板中使用 Angular Reactive Forms.get() 方法会导致不必要的方法调用(如组件方法)吗? - Will using Angular Reactive Forms .get() method in template cause unnecessary method calls like a component method? 在 Angular 中使用响应式表单与服务 - Using reactive forms in Angular with services 使用服务器方法更新反应变量 - Update reactive var with server Method 反应性 Forms 不更新表单字段中的值 Angular 7 - Reactive Forms Not Updating Value In Form Field Angular 7 角反应形式值不会使用Google地址自动完成功能更新 - angular reactive forms value does not get updated on using google address autocomplete 如何使用Reactive Forms连接值? - How to concatenate values using Reactive Forms? 使用反应形式的角度密码和确认密码验证 - password and confirmpassword validation in angular using reactive forms 如何使用 Angular 中的反应式表单更新错误消息对象? - How to update error messages object with Reactive forms in Angular?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM