【问题标题】:Angular material table not showing data until I click paging links在我单击分页链接之前,角度材料表不显示数据
【发布时间】:2018-08-10 04:22:53
【问题描述】:

我关注了stackblitz MatDataTable。在我单击分页链接之前,该表不会填充。点击分页后一切正常。我从昨天开始尝试。我也尝试了this stackoverflow 解决方案,但没有奏效:

table.component.ts

import { Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator, MatTableDataSource } from '@angular/material';


@Component({
  selector: 'app-table',
  templateUrl: './table.component.html',
  styleUrls: ['./table.component.scss']
})
export class TableComponent implements OnInit {
  displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
  dataSource = new MatTableDataSource<PeriodicElement>(ELEMENT_DATA);

  @ViewChild(MatPaginator) paginator: MatPaginator;      

  ngOnInit() {
    this.dataSource.paginator = this.paginator;
  }
}

export interface PeriodicElement {
  name: string;
  position: number;
  weight: number;
  symbol: string;
}

const ELEMENT_DATA: PeriodicElement[] = [
  {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
  {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
  {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
  {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
  {position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
  {position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
  {position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
  {position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'}
];

table.component.html

here 复制了相同的数据。并删除了 table、tr、th 和 td 标签。根据谷歌的建议。

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

    <!-- Position Column -->
    <ng-container matColumnDef="position">
      <mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
      <mat-cell *matCellDef="let element"> {{element.position}} </mat-cell>
    </ng-container>

    <!-- Name Column -->
    <ng-container matColumnDef="name">
      <mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
      <mat-cell *matCellDef="let element"> {{element.name}} </mat-cell>
    </ng-container>

    <!-- Weight Column -->
    <ng-container matColumnDef="weight">
      <mat-header-cell *matHeaderCellDef> Weight </mat-header-cell>
      <mat-cell *matCellDef="let element"> {{element.weight}} </mat-cell>
    </ng-container>

    <!-- Symbol Column -->
    <ng-container matColumnDef="symbol">
      <mat-header-cell *matHeaderCellDef> Symbol </mat-header-cell>
      <mat-cell *matCellDef="let element"> {{element.symbol}} </mat-cell>
    </ng-container>

    <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
    <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
  </mat-table>

  <mat-paginator [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator>
</div>

Package.json 包含:

"@agm/core": "1.0.0-beta.2",
"@angular/animations": "5.2.9",
"@angular/cdk": "5.2.4",
"@angular/common": "5.2.9",
"@angular/compiler": "5.2.9",
"@angular/core": "5.2.9",
"@angular/flex-layout": "5.0.0-beta.13",
"@angular/forms": "5.2.9",
"@angular/http": "5.2.9",
"@angular/material": "5.2.4",
"@angular/cli": "1.7.3",

【问题讨论】:

  • Aniket 为您解答。如果您想要更多参与示例,请查看我的 StackBliz stackblitz.com/edit/angular-material-table-with-multi-queries。里面有很多东西可以研究。
  • TQ Peterson,如果您就上述 stackblitz 示例编写任何教程,解释 rxjs 运算符以便更好地理解,我将不胜感激。
  • :-) StackBliz 是我的教程,但你必须先学习 Angular Material 文档。当你学习时,请参考我的表格。我确实有点不同意文档,但那是在 cmets 中。我尽量保持更新。

标签: datatable pagination angular-material


【解决方案1】:

在 ngAfterViewInit 挂钩生命周期中初始化分页

这是一个例子,

ngAfterViewInit() {
    this.dataSource.paginator = this.paginator;
}

你在 onInit() 上使用分页器,从 onInit() 中移除

【讨论】:

  • 谢谢 Aniket,我尝试使用 ChangeDetectorRef 并成功了。无论如何,您的解决方案很好。
猜你喜欢
  • 2021-01-11
  • 1970-01-01
  • 2020-07-24
  • 1970-01-01
  • 1970-01-01
  • 2021-05-08
  • 2019-11-26
相关资源
最近更新 更多