Mat-Dialog中的Mat-Table不更新

时间:2019-04-27 00:26:22

标签: angular angular-material angular-reactive-forms mat-table

我正在使用Angular 7.3.8应用程序,并且正在使用Angular Material库。我可以通过NgOnInit周期用数据初始化mat-table组件,但是当我尝试通过某些功能用一些数据更新表时,UI不会被更新。

已经尝试使用NgZone强制更新UI,但仍然无法正常工作。

这是模板的代码:

 <table mat-table [dataSource]="dataSource" class="mat-elevation-z8">

  <!-- Checkbox Column -->
  <ng-container matColumnDef="select">
    <th mat-header-cell *matHeaderCellDef>
      <mat-checkbox (change)="$event ? masterToggle() : null"
                    [checked]="selection.hasValue() && isAllSelected()"
                    [indeterminate]="selection.hasValue() && !isAllSelected()"
                    >
      </mat-checkbox>
    </th>
    <td mat-cell *matCellDef="let row">
      <mat-checkbox (click)="$event.stopPropagation()"
                    (change)="$event ? selection.toggle(row) : null"
                    [checked]="selection.isSelected(row)"
                    >
      </mat-checkbox>
    </td>
  </ng-container>

  <!-- Name Column -->
  <ng-container matColumnDef="day">
    <th mat-header-cell *matHeaderCellDef> Day </th>
    <td mat-cell *matCellDef="let element"> {{element.day | titlecase}} </td>
  </ng-container>

  <!-- Weight Column -->
  <ng-container matColumnDef="startHour">
    <th mat-header-cell *matHeaderCellDef> Start Hour </th>
    <td mat-cell *matCellDef="let element"> {{element.startHour}} </td>
  </ng-container>

  <!-- Symbol Column -->
  <ng-container matColumnDef="endHour">
    <th mat-header-cell *matHeaderCellDef> End Hour </th>
    <td mat-cell *matCellDef="let element"> {{element.endHour}} </td>
  </ng-container>

  <ng-container matColumnDef="action">
      <th mat-header-cell *matHeaderCellDef> Action </th>
      <td mat-cell *matCellDef="let element"> {{element.action}} </td>
  </ng-container>

  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns;"
      (click)="selection.toggle(row)">
  </tr>
</table>

这是更新功能:

ngOnInit(){
  this.dataSource = [
    { day: 'Monday' , startHour: '9: 00 am', endHour: '13:00 pm', 
     action: '' };
}

addPeriod(form: any){
   const obj = {
      day: form.value.pickedDay,
      startHour: form.value.startHour + ': 00 am',
      endHour: form.value.endHour + ': 00 pm',
      action: ''
   } as TableSchedule;

   this.zone.run(() => {
      this.dataSource.push(obj);
      console.log('new data:', this.dataSource);
   });
}

日志显示dataSource数组正在更新,但UI不显示新值。

2 个答案:

答案 0 :(得分:0)

我没有对此进行测试,但是您是否尝试过以下方法:

public rows = [];

ngOnInit() {
    this.rows.push({ day: 'Monday', startHour: '9: 00 am', endHour: '13:00 pm', action: '' });
    this.dataSource = new MatTableDataSource(this.rows);
}

addPeriod(form: any) {
    const obj = {
        day: form.value.pickedDay,
        startHour: form.value.startHour + ': 00 am',
        endHour: form.value.endHour + ': 00 pm',
        action: '',
    };

    this.rows.push(obj);
    this.dataSource = new MatTableDataSource(this.rows); // not sure if this is even needed
}

答案 1 :(得分:0)

我终于设法通过使用“ ...”扩展运算符分配新数组来使其工作:

 const array = this.dataSource;
 if (this.checkDuplicate(array, obj)) {
      console.log('DUPLICATE');
 } else {
      array.push(obj);
      this.zone.run(() => {
        this.dataSource = [...array];
        console.log('new data:', this.dataSource);
      });
 }

我个人不明白为什么会这样工作,请您澄清一下。