加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

使用FormArray的Angular Material可编辑表

发布时间:2020-12-17 17:47:31 所属栏目:安全 来源:网络整理
导读:我正在尝试使用最新的材质cdk for angular来构建一个内联可编辑表格. 题 How can I make mat-table use [formGroupName] so that the form fields can be referenced by its correct form path? 这是我到目前为止所得到的:Complete StackBlitz example 模板
我正在尝试使用最新的材质cdk for angular来构建一个内联可编辑表格.

How can I make mat-table use [formGroupName] so that the form fields can be referenced by its correct form path?

这是我到目前为止所得到的:Complete StackBlitz example

模板

<form [formGroup]="form">
  <h1>Works</h1>
  <div formArrayName="dates" *ngFor="let date of rows.controls; let i = index;">
    <div [formGroupName]="i">
      <input type="date" formControlName="from" placeholder="From date">
      <input type="date" formControlName="to" placeholder="To date">
    </div>
  </div>


  <h1>Wont work</h1>
  <table mat-table [dataSource]="dataSource" formArrayName="dates">
    <!-- Row definitions -->
    <tr mat-header-row *matHeaderRowDef="displayColumns"></tr>
    <tr mat-row *matRowDef="let row; let i = index; columns: displayColumns;" [formGroupName]="i"></tr>

    <!-- Column definitions -->
    <ng-container matColumnDef="from">
      <th mat-header-cell *matHeaderCellDef> From </th>
      <td mat-cell *matCellDef="let row"> 
        <input type="date" formControlName="from" placeholder="From date">
      </td>
    </ng-container>

    <ng-container matColumnDef="to">
      <th mat-header-cell *matHeaderCellDef> To </th>
      <td mat-cell *matCellDef="let row">
        <input type="date" formControlName="to" placeholder="To date">
      </td>
    </ng-container>
  </table>
  <button type="button" (click)="addRow()">Add row</button>
</form>

零件

export class AppComponent implements  OnInit  {
  data: TableData[] = [ { from: new Date(),to: new Date() } ];
  dataSource = new BehaviorSubject<AbstractControl[]>([]);
  displayColumns = ['from','to'];
  rows: FormArray = this.fb.array([]);
  form: FormGroup = this.fb.group({ 'dates': this.rows });

  constructor(private fb: FormBuilder) { }

  ngOnInit() {
    this.data.forEach((d: TableData) => this.addRow(d,false));
    this.updateView();
  }

  emptyTable() {
    while (this.rows.length !== 0) {
      this.rows.removeAt(0);
    }
  }

  addRow(d?: TableData,noUpdate?: boolean) {
    const row = this.fb.group({
      'from'   : [d && d.from ? d.from : null,[]],'to'     : [d && d.to   ? d.to   : null,[]]
    });
    this.rows.push(row);
    if (!noUpdate) { this.updateView(); }
  }

  updateView() {
    this.dataSource.next(this.rows.controls);
  }
}

问题

这不行.控制台产量

ERROR Error: Cannot find control with path: ‘dates -> from’

似乎[formGroupName] =“i”没有效果,导致路径应该是日期 – > 0 – >从使用formArray时.

我当前的解决方法:对于这个问题,我绕过了内部路径查找(formControlName =“from”)并直接使用表单控件:[formControl] =“row.get(‘from’)”,但我想我知道如何(或者至少为什么我不能)使用Reactive Form首选方式.

欢迎任何提示.谢谢.

由于我认为这是一个错误,我已经使用angular / material2 github repo注册了an issue.

解决方法

我会使用matCellDef绑定中可以得到的索引:

*matCellDef="let row; let index = index" [formGroupName]="index"

Forked Stackblitz

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读