Угловая таблица материалов не отображает строки источника данных - PullRequest
0 голосов
/ 11 апреля 2019

Я использую таблицу угловых материалов для отображения моего источника данных, который на самом деле является наблюдаемым, но он не работает. Я попытался отобразить его за пределами стола, и он работает довольно хорошо. Я также пытался использовать changeDetectorRefs.detectChanges(). Вот мой код и отрендеренный компонент.

component.ts:

ngOnInit() {
    this.getCustomers();
    this.changeDetectorRefs.detectChanges();
  }
getCustomers(): void {
  this.purchaseService.getCustomersInfo()
      .subscribe(customers => {
        this.options = customers;
        this.dataSource=customers;
      });
}
displayedColumns = ['id'];

component.html:

<div *ngFor="let element of dataSource">
  {{element.id}}
</div>
<table  mat-table [dataSource]="dataSource" class="mat-elevation-z8">

  <!-- Position Column -->
  <ng-container matColumnDef="id">
    <th mat-header-cell *matHeaderCellDef> Customer Id </th>
    <td mat-cell *matCellDef="let element"> {{element.id}} </td>
  </ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>

представленный компонент:

Ответы [ 2 ]

0 голосов
/ 11 апреля 2019

Я надеюсь, что у вашего компонента OnPush ChangeDetectionStrategy, как показано ниже

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})

Переместите this.changeDetectorRefs.detectChanges(); после установки источника данных, чтобы решить вашу проблему.

ngOnInit() {
    this.getCustomers();
  }
getCustomers(): void {
  this.purchaseService.getCustomersInfo()
      .subscribe(customers => {
        this.options = customers;
        this.dataSource=customers;
        this.changeDetectorRefs.detectChanges();

      });
}
displayedColumns = ['id', 'name','purchases','actions'];

Рабочий образец

0 голосов
/ 11 апреля 2019

Возможно, потому что вы не настроили столбцы для 'name','purchases','actions'.Попробуйте удалить эти элементы из массива столбцов

displayedColumns = ['id'];

Демо

...