在下拉菜单中设置默认值,具体取决于其他值

和风

我正在使用Reactive表单,我有一个级联下拉列表,如果用户在第一页中选择了一个现有项目(此页面中有此问题),则该下拉列表可以具有默认值,如果用户要创建一个项目,则该下拉列表可以没有任何价值。

所以这是问题所在:

当用户来自现有项目时,会选择价格表,但似乎没有任何价值,因为未显示货币。当我再次按预期选择自动选择货币的价目表时,有人对此有解决办法吗?提前致谢。

在此处输入图片说明

这是代码示例,谢谢您的时间

JSON(从pageChanged()返回

在此处输入图片说明

Component.ts(自动选择值)

  pageChanged(event){
const current = this.currentPage = event;
return this.httpClient.get<QuotationsInterface>(`this.url${this.projectId}?page=${current}&token=${this.token.token}`)
  .subscribe(
    data => {
      console.log('GET PROJECT BY ID OK',data);
      this.project = data.project;
      this.registerForm.controls['pricelist'].setValue(this.project.pricelist_id);
      this.registerForm.controls['currency'].setValue(this.project.currency_id);
    },
    error => {
      console.log("PAGE PROJECT FAILED", error)
    }
  )

}

Component.html

<div class="flex2 pl10" (change)="selectedPriceListIndex = $event.target.selectedIndex-1">
  <select formControlName="pricelist" class="form-control"  [ngClass]="{'is-invalid': submitted && f.pricelist.errors}" >
    <option value="" disabled selected>Select a Pricelist</option>
    <option   *ngFor="let p of filter.ListPricelist;let i = index;" [ngValue]="p.id"  id="pricelist_id" >{{i}} {{p.label}}</option>
  </select>
  <div *ngIf="submitted && f.pricelist.errors" class="invalid-feedback">
    <div *ngIf="f.pricelist.errors.required">Pricelist is required</div>
  </div>
</div>
  <div class="flex2 pl10">
  <select formControlName="currency" [ngClass]="{'is-invalid': submitted && f.currency.errors}" class="form-control">
    <option value >Select a currency</option>
    <option  *ngFor="let c of filter.ListPricelist[selectedPriceListIndex]?.currencies" [ngValue]="c.id" selected="selected">{{c.code}}</option>
  </select>
    <div *ngIf="submitted && f.currency.errors" class="invalid-feedback">
      <div *ngIf="f.currency.errors.required">Currency is required</div>
    </div>
  </div>

编辑:

谢谢大家的回答,我通过使用.patchValue(请检查@nima_amr answer)并替换为component.html

<option  *ngFor="let c of filter.ListPricelist[selectedPriceListIndex]?.currencies" [ngValue]="c.id" selected="selected">{{c.code}}</option>

通过以下方式:

<option  *ngFor="let c of filter.ListPricelist[registerForm.value.pricelist-1]?.currencies"  [value]="c.id">{{c.code}}</option>
什么啊

您可以将.setValue替换为.patchValue,例如Down

  pageChanged(event){
const current = this.currentPage = event;
return this.httpClient.get<QuotationsInterface>(`this.url${this.projectId}?page=${current}&token=${this.token.token}`)
  .subscribe(
    data => {
      console.log('GET PROJECT BY ID OK',data);
      this.project = data.project;
      this.registerForm.patchValue({
        pricelist:  this.project.pricelist_id,
        currency:   this.project.currency_id
      });
    },
    error => {
      console.log("PAGE PROJECT FAILED", error)
    }
  )

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在下拉菜单控件中为Excel自定义功能区控件设置默认值

来自分类Dev

在下拉列表中设置默认值

来自分类Dev

如何在magento的管理网格形式的下拉菜单中设置默认值?

来自分类Dev

默认选择下拉菜单中的值

来自分类Dev

在下拉菜单中映射值

来自分类Dev

下拉菜单取决于其他下拉菜单-angularjs

来自分类Dev

在Bootstrap下拉菜单中设置默认值

来自分类Dev

使用Angular中的对象在选择下拉菜单中设置默认值

来自分类Dev

使用SAPUI5在下拉菜单中选择默认值

来自分类Dev

如何使用angularjs设置下拉菜单默认值

来自分类Dev

为下拉菜单设置默认值

来自分类Dev

如何根据json属性在Kendo jQuery下拉菜单中设置默认值

来自分类Dev

Javascript 3下拉菜单。更改一个下拉菜单时,请为其他两个下拉菜单设置默认值。需要优化

来自分类Dev

菜单下拉菜单中哪些不使用项目ID作为默认值?

来自分类Dev

当我在下拉菜单中选择默认值时,在Rails中呈现完整形式

来自分类Dev

如何在magento的管理网格形式的下拉菜单中设置默认值?

来自分类Dev

下拉菜单默认值

来自分类Dev

Knockoutjs-在下拉菜单中设置值

来自分类Dev

AngularJS在下拉菜单中设置默认选择的选项

来自分类Dev

来自MYSQL的下拉菜单中的其他“ 1”值

来自分类Dev

设置下拉菜单的默认值

来自分类Dev

如何在下拉JavaScript中设置默认值

来自分类Dev

Rails在下拉菜单中显示其他信息

来自分类Dev

Jaspersoft在下拉菜单中重新导入的默认值是什么?

来自分类Dev

如何在PowerApps中设置下拉菜单的默认值

来自分类Dev

如何在下拉菜单中将默认值和项目标签设置为空白

来自分类Dev

显示/隐藏 div 取决于具有默认值的 AngularJS 中的下拉选择

来自分类Dev

在下拉列表中设置默认值

来自分类Dev

如何在反应中设置下拉菜单的默认值?

Related 相关文章

  1. 1

    如何在下拉菜单控件中为Excel自定义功能区控件设置默认值

  2. 2

    在下拉列表中设置默认值

  3. 3

    如何在magento的管理网格形式的下拉菜单中设置默认值?

  4. 4

    默认选择下拉菜单中的值

  5. 5

    在下拉菜单中映射值

  6. 6

    下拉菜单取决于其他下拉菜单-angularjs

  7. 7

    在Bootstrap下拉菜单中设置默认值

  8. 8

    使用Angular中的对象在选择下拉菜单中设置默认值

  9. 9

    使用SAPUI5在下拉菜单中选择默认值

  10. 10

    如何使用angularjs设置下拉菜单默认值

  11. 11

    为下拉菜单设置默认值

  12. 12

    如何根据json属性在Kendo jQuery下拉菜单中设置默认值

  13. 13

    Javascript 3下拉菜单。更改一个下拉菜单时,请为其他两个下拉菜单设置默认值。需要优化

  14. 14

    菜单下拉菜单中哪些不使用项目ID作为默认值?

  15. 15

    当我在下拉菜单中选择默认值时,在Rails中呈现完整形式

  16. 16

    如何在magento的管理网格形式的下拉菜单中设置默认值?

  17. 17

    下拉菜单默认值

  18. 18

    Knockoutjs-在下拉菜单中设置值

  19. 19

    AngularJS在下拉菜单中设置默认选择的选项

  20. 20

    来自MYSQL的下拉菜单中的其他“ 1”值

  21. 21

    设置下拉菜单的默认值

  22. 22

    如何在下拉JavaScript中设置默认值

  23. 23

    Rails在下拉菜单中显示其他信息

  24. 24

    Jaspersoft在下拉菜单中重新导入的默认值是什么?

  25. 25

    如何在PowerApps中设置下拉菜单的默认值

  26. 26

    如何在下拉菜单中将默认值和项目标签设置为空白

  27. 27

    显示/隐藏 div 取决于具有默认值的 AngularJS 中的下拉选择

  28. 28

    在下拉列表中设置默认值

  29. 29

    如何在反应中设置下拉菜单的默认值?

热门标签

归档