【问题标题】:Mat-Table inside Mat-Dialog not updatingMat-Dialog 中的 Mat-Table 未更新
【发布时间】:2019-04-28 02:13:30
【问题描述】:

我正在开发一个 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 未显示新值。

【问题讨论】:

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


    【解决方案1】:

    我没有对此进行测试,但您是否尝试过这样的事情:

    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
    }
    

    【讨论】:

      【解决方案2】:

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

       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);
            });
       }
      

      我个人不明白为什么它会这样工作,任何澄清将不胜感激。

      【讨论】:

      猜你喜欢
      • 2018-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-20
      • 2019-08-31
      • 2020-04-09
      • 2019-01-26
      • 2019-07-23
      相关资源
      最近更新 更多