如何在FormArray(反应性表单)中使用mat-autocomplete(角材料自动完成) [英] How to use mat-autocomplete (Angular Material Autocomplete) inside FormArray (Reactive Forms)

查看:65
本文介绍了如何在FormArray(反应性表单)中使用mat-autocomplete(角材料自动完成)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我具有以下表单结构:

  this.myForm = this.formBuilder.group({
          date: ['', [Validators.required]],
          notes: [''],
          items: this.initItems()

        });
  initItems() {
    var formArray = this.formBuilder.array([]);
    for (let i = 0; i < 2; i++) {
      formArray.push(this.formBuilder.group({
        name: ['', [Validators.required]],
        age: ['', [Validators.required]],
      }));
    }

    return formArray;
  }

和应该是自动完成的名称控件,如何将所有名称控件与自动完成列表相关联?

解决方案

我通过将FormArray中的每个name控件与filteredOption数组相关联来解决了这个问题:

  ManageNameControl(index: number) {
    var arrayControl = this.myForm.get('items') as FormArray;
    this.filteredOptions[index] = arrayControl.at(index).get('name').valueChanges
      .pipe(
      startWith<string | User>(''),
      map(value => typeof value === 'string' ? value : value.name),
      map(name => name ? this._filter(name) : this.options.slice())
      );

  }

然后,每次我在form Array中创建一个formgroup(创建新项)之后,我需要像这样在新索引处调用上述函数:

  addNewItem() {
    const controls = <FormArray>this.myForm.controls['items'];
    let formGroup = this.formBuilder.group({
      name: ['', [Validators.required]],
      age: ['', [Validators.required]],
    });
    controls.push(formGroup);
    // Build the account Auto Complete values
    this.ManageNameControl(controls.length - 1);

  }

在.html文件中,我们需要引用所需的filteredOption数组,我们可以使用i索引来做到这一点:

  <mat-option *ngFor="let option of filteredOptions[i] | async " [value]="option">
  {{ option.name }}
  </mat-option>

请在此处查看详细答案 https://stackblitz.com/edit/angular-szxkme?file=app%2Fautocomplete-display-example.ts

更新: 要使用特定对象的默认值填充数组,您可以使用以下接收表单来完成此操作:

 let formGroup = this.fb.group({
      name: [{value: { name: 'Mary' } , disabled: false}, [Validators.required]],
      age: ['', [Validators.required]],
    });

stackblitz

let's say I have the following form structure:

  this.myForm = this.formBuilder.group({
          date: ['', [Validators.required]],
          notes: [''],
          items: this.initItems()

        });
  initItems() {
    var formArray = this.formBuilder.array([]);
    for (let i = 0; i < 2; i++) {
      formArray.push(this.formBuilder.group({
        name: ['', [Validators.required]],
        age: ['', [Validators.required]],
      }));
    }

    return formArray;
  }

and the name control supposed to be an autocomplete, how can I relate the all the name controls to an autocomplete list?

解决方案

I solved this by relating each name control inside the FormArray to a filteredOption array:

  ManageNameControl(index: number) {
    var arrayControl = this.myForm.get('items') as FormArray;
    this.filteredOptions[index] = arrayControl.at(index).get('name').valueChanges
      .pipe(
      startWith<string | User>(''),
      map(value => typeof value === 'string' ? value : value.name),
      map(name => name ? this._filter(name) : this.options.slice())
      );

  }

Then After each time I build a formgroup inside the form Array (create new item), I need to call the above function at the new index like this:

  addNewItem() {
    const controls = <FormArray>this.myForm.controls['items'];
    let formGroup = this.formBuilder.group({
      name: ['', [Validators.required]],
      age: ['', [Validators.required]],
    });
    controls.push(formGroup);
    // Build the account Auto Complete values
    this.ManageNameControl(controls.length - 1);

  }

In the .html file, we need to refer to the desired filteredOption array, we can do this by using the i index:

  <mat-option *ngFor="let option of filteredOptions[i] | async " [value]="option">
  {{ option.name }}
  </mat-option>

please see the detailed answer here https://stackblitz.com/edit/angular-szxkme?file=app%2Fautocomplete-display-example.ts

Update: to populate the array with a default value for a specific object, you can do it using the receive forms like this:

 let formGroup = this.fb.group({
      name: [{value: { name: 'Mary' } , disabled: false}, [Validators.required]],
      age: ['', [Validators.required]],
    });

stackblitz

这篇关于如何在FormArray(反应性表单)中使用mat-autocomplete(角材料自动完成)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆