2

我在弹出窗口中使用垫表。在 mat-table 内会有 mat-autocomplete 的行。问题是我需要动态添加行。我正在使用一个formGroup,里面有formArray。

这将是 FormGroup 的模型

   {
  formArray:
    [
      {
         name:"someName",
         material:"someMaterial",
         quantity:5
      },
      {
         name:"someName2",
         material:"someMaterial",
         quantity:5
      },    
    ]

}

首先网格将是空的,但是当我单击一个按钮时,它会在 formArray (这是网格的数据源)内插入一个 formGroup。但是当这种情况发生时,用户界面完全冻结,我无能为力。

这是HTML文件代码

<form [formGroup]="positionFormGroup">
    <div class="card-body" style="height: 500px; overflow: auto;"  >
        <table class="full-width" style="overflow: scroll;" mat-table [dataSource]="this.positionFormGroup.get('positionInfos').value" formArrayName = "positionInfos">

            <!-- Material Column -->
            <ng-container matColumnDef="Material">
                <th mat-header-cell *matHeaderCellDef class="text-center align-column">
                    <p [innerHTML]=" 'MachineCapacity.Grid_Label_IdMaterial' | translate"></p>
                </th>
                <td mat-cell *matCellDef="let data; let i = index" [formGroupName]="i">
                    {{data.idMaterial}}

                    <input type="text" formControlName="idMaterial" [matAutocomplete]="materialAuto">
                    <mat-autocomplete #materialAuto="matAutocomplete">
                        <mat-option *ngFor="let option of materialFilteredOptions | async" [value]="option.description"
                            class="auto-option">
                            {{option.description}}
                        </mat-option>
                    </mat-autocomplete>

                </td>
            </ng-container>

            <!-- Quantity Column -->
            <ng-container matColumnDef="Quantity">
                <th mat-header-cell *matHeaderCellDef class="text-center align-column">
                    <p [innerHTML]=" 'MachineCapacity.Grid_Label_Quantity' | translate"></p>
                </th>
                <td mat-cell *matCellDef="let data">
                    {{data.quantity}}
                </td>
            </ng-container>

我能够找到问题所在。这是因为我使用 mat-auto 完成的参考。当我从输入标记中删除 matAutoComplete 引用或更改 formControlName 时,它​​可以工作。

这是 TS 代码:

  ngOnInit(): void {

   // this.positionInfos = new FormArray([]);
    this.positionFormGroup = this.formBuilder.group({
      positionInfos: new FormArray([])
    });

    //this.positionFormGroup.

  }

  addMaterial(){
    let data  = {
      rowID :'f',
      position : 'f',
      idMaterial:'f',
      quantity: 'f',
      isDeleted: 'f',
      isActive:true
    }
    

    this.positionFormGroup.get('positionInfos')?.push(this.formBuilder.group(data))
    //console.log( this.positionFormGroup.get('positionInfos').value)
  }

如果我遗漏了什么,请告诉我,在此先感谢

4

1 回答 1

1

与参考问题无关,UI 根本没有冻结。

这是因为变化检测而发生的,我想是的。这就是为什么我们有一种 UI 冻结的感觉。

正是在这一点上,[dataSource]="this.positionFormGroup.get('positionInfos').value"无限次触发变化检测

我不知道,为什么它会触发无限次。

已经有嵌套表单组的答案。

html

<form [formGroup]="positionFormGroup" novalidate>
  <table formArrayName="positionInfos">

    <div *ngFor="let data of control; let i = index">
      <div [formGroupName]="i">

        <input type="text" formControlName="idMaterial" [matAutocomplete]="materialAuto">
        <mat-autocomplete #materialAuto="matAutocomplete">
          <mat-option *ngFor="let option of materialFilteredOptions | async" [value]="option.description">
            {{option.description}}
          </mat-option>
        </mat-autocomplete>
      </div>
    </div>

  </table>
</form>

ts

export class AppComponent {
  positionFormGroup: FormGroup;
  materialControl: FormControl = new FormControl('');
  materialFilteredOptions: Observable<{ description: string; }[]>;
  options = [
    { description: 'item1' },
    { description: 'item2' },
    { description: 'item3' },
    { description: 'item4' }
  ]

  control: any;
  constructor(private formBuilder: FormBuilder) {
  }

  ngOnInit(): void {
    this.materialFilteredOptions = this.materialControl.valueChanges
      .pipe(
        startWith(''),
        map(value => this.options)
      );
    this.positionFormGroup = this.formBuilder.group({
      positionInfos: new FormArray([])
    });

    this.addMaterial();
    this.control = this.getControls().controls;
  }


  addMaterial() {
    (this.positionFormGroup.get('positionInfos') as FormArray).push(this.formBuilder.group({
      rowID: '0',
      position: '0',
      idMaterial: 'item1',
      quantity: 1,
      isDeleted: false,
      isActive: true
    }));
  }

  confirm(){
    console.log( this.positionFormGroup.get('positionInfos').value)
  }

  getControls(){
    console.log( (this.positionFormGroup.get('positionInfos') as FormArray));
    return (this.positionFormGroup.get('positionInfos') as FormArray);
  }
于 2020-08-01T18:40:24.637 回答