首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在反应式表单中手动设置表单数组元素的值

在反应式表单中手动设置表单数组元素的值
EN

Stack Overflow用户
提问于 2019-12-10 21:19:51
回答 1查看 142关注 0票数 0

这让我抓狂,我已经不知所措了,这已经是我第二天的工作了,但仍然是徒劳的。

我的表单值没有显示在html中,我正在处理动态表,我事先不知道其中的行数。问题是,即使它的值保存在component对象中,计算出的总数也不会在html中显示。

这是我的html代码

代码语言:javascript
复制
<table style="overflow-x: auto;display: inline-block; white-space: nowrap;">
    <thead>
        <tr class='tableHeader'>
            <div fxLayout="row" fxLayoutAlign="start center" fxFlex="1 0 auto">
                <td fxFlex="22" class="pr-4">Name</td>
                <td fxFlex="15" class="pr-4">Price</td>
                <td fxFlex="15" class="pr-4">Loan Term</td>
                <td fxFlex="15" class="pr-4">Quantity</td>
                <td fxFlex="15" class="pr-4">Deposit</td>
                <td fxFlex="15" class="pr-4">Total</td>
            </div>
        </tr>
    </thead>
    <tbody>
        <tr formArrayName="products" *ngFor="let product of loanProductForm.get('products').controls; let i = index">
            <div [formGroupName]="i" fxLayout="row" fxLayoutAlign="start center" fxFlex="1 0 auto">
                <td fxFlex="22">
                    <mat-form-field appearance="outline" fxFlex="100" class="pr-4">
                        <mat-label>Product </mat-label>
                        <mat-select formControlName="productId" [id]="'productId' + i" required>
                            <mat-option *ngFor="let product of productList" [value]="product.productId">
                                {{product.name}}
                            </mat-option>
                        </mat-select>

                    </mat-form-field>
                </td>
                <td fxFlex="15">
                    <mat-form-field appearance="outline" fxFlex="100" class="pr-4">
                        <mat-label>Price </mat-label>
                        <input type='number' (keyup)="onPriceChange($event)" matInput formControlName="price"
                            [id]="'price' + i" name="" placeholder="Price" required>
                    </mat-form-field>
                </td>
                <td fxFlex="15">
                    <mat-form-field appearance="outline" fxFlex="100" class="pr-4">
                        <mat-label>Loan Term </mat-label>
                        <mat-select formControlName="loanTermId" [id]="'loanTermId' + i" required>
                            <mat-option *ngFor="let loanTerm of loanTermList" [value]="loanTerm.loanTermId">
                                {{loanTerm.numberOfMonths}}
                            </mat-option>
                        </mat-select>
                    </mat-form-field>
                </td>
                <td fxFlex="15">
                    <mat-form-field appearance="outline" fxFlex="100" class="pr-4">
                        <mat-label>Quantity </mat-label>
                        <input type='number' formControlName="quantity" [id]="'quantity' + i" matInput name="" id=""
                            placeholder="Quantity" required>

                    </mat-form-field>
                </td>
                <td fxFlex="15">
                    <mat-form-field appearance="outline" fxFlex="100" class="pr-4">
                        <mat-label>Deposit </mat-label>
                        <input type='number' formControlName="deposit" [id]="'deposit' + i" matInput name="" id=""
                            placeholder="Deposit" required>
                    </mat-form-field>
                </td>
                <td fxFlex="15">
                    <mat-form-field appearance="outline" fxFlex="100" class="pr-4">
                        <mat-label>Total </mat-label>
                        <input type='number' formControlName="total" [id]="'total' + i" matInput name="total" id=""
                            placeholder="Total" style="color:black; font-weight:bold" required>
                        <!-- <input disabled type='number' [(ngModel)]="totalValue" ngModel [ngModelOptions]="{standalone: true}" [id]="'total' + i" matInput name="total" id="" placeholder="Total" style="color:black; font-weight:bold" required> -->
                    </mat-form-field>
                </td>

            </div>
        </tr>
        <tr>
            <td fxFlex="10">
                <div fxLayout="row" fxLayoutAlign="start center" fxFlex="1 0 auto">
                    <button type="button" mat-stroked-button class='addBtn btn-style-2' fxFlex='100'
                        (click)='addProductButtonClick()'>Add
                        <mat-icon matSuffix>add_box</mat-icon>
                    </button>
                </div>
            </td>
        </tr>
    </tbody>
</table>

下面是我的组件代码

代码语言:javascript
复制
this.loanProductForm = this._formBuilder.group({
      products: this._formBuilder.array([
        this.addProductFormGroup()
      ])
    });



 addProductButtonClick(): void {

    (<FormArray>this.loanProductForm.get('products')).push(this.addProductFormGroup());
    console.log('Loan Products: ', this.loanProductForm.value)

  }


addProductFormGroup(): FormGroup {
    return this._formBuilder.group({
      productId: ['', Validators.required],
      price: [0, Validators.required],
      loanTermId: ['', Validators.required],
      quantity: [0, Validators.required],
      deposit: [0, Validators.required],
      total: [0, Validators.required],
    });
  }

即使它在我的object中保存得很好,但它不能在html中显示。总价计算公式为(价格*数量)-保证金=合计

这是我已经尝试过的东西,除此之外,我尝试在form对象中使用,看看值是否在表单中更改,它们接受object中的更改,但不显示在html中

代码语言:javascript
复制
this.loanProductForm.valueChanges.subscribe((values) => {
  values.products.forEach(product => {
    product.total = (product.quantity * product.price) - product.deposit
  })

  console.log('Updated values of form are: ', values.products)
})

我只想提前更新每一行的total值,谢谢。为了让这个工作起来,我已经筋疲力尽了。再次感谢大家提前伸出援手

EN

回答 1

Stack Overflow用户

发布于 2019-12-10 22:40:26

你需要的是。创建表单组时,需要订阅更改并更新合计值。

代码语言:javascript
复制
 addProductFormGroup(): FormGroup {
    const group = this._formBuilder.group({
      productId: ["", Validators.required],
      price: [0, Validators.required],
      loanTermId: ["", Validators.required],
      quantity: [0, Validators.required],
      deposit: [0, Validators.required],
      total: [0, Validators.required]
    });
    const quantity = group.get("quantity");
    const price = group.get("price");
    const deposit = group.get("deposit");
    const onChange = r => {
      if (r.price && r.quantity) {
        const total = r.price * r.quantity - r.deposit;
        group.patchValue(
          { total: total },
          { onlySelf: true, emitEvent: false } // emitEvent is crucial else it will cause and endless loop
        );
      }
    }; 
    group.valueChanges.subscribe(r => onChange(r));
    return group;
  }

Stackblitz example

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59268190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档