使用带有可观察数组作为数据源的 mat-table 使用 mat-paginator [英] using mat-paginator with mat-table using observable array as the datasource

查看:30
本文介绍了使用带有可观察数组作为数据源的 mat-table 使用 mat-paginator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用可观察数组作为数据源.这很好用,只是我现在无法弄清楚如何使用分页器.下面是html和ts

I am using an observable array as the datasource. This works fine except that i am unable to figure out how to use paginator now. below is the html and ts

html

 <table mat-table #TABLE [dataSource]="cards" class="mat-elevation-z8">          
                <!-- Email Column -->
             <ng-container matColumnDef="date">
                <th mat-header-cell *matHeaderCellDef  class="tbl-th"> Date </th>
                <td mat-cell *matCellDef="let element"> {{core.pretifyDate(element.date)}} </td>
              </ng-container>

            <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
            <tr mat-row *matRowDef="let row; columns: displayedColumns;">
            </tr>
          </table>
          <mat-paginator [pageSizeOptions]="[5, 10, 20, 50, 100]" [pageSize]="pageSize" showFirstLastButtons></mat-paginator>

.ts

export class CardQueueComponent implements OnInit {

  @ViewChild('TABLE', {static: false}) table: MatTable<any>;
  @ViewChild(MatPaginator, {static: true}) paginator: MatPaginator;

  displayedColumns: string[] = [ 'image', 'customer', 'email',  'date', 'process' ];
  cards: Observable<Card[]> 
  pageSize = 5


  constructor(public dataSvc: SBDataService,
              public core:CoreService,
              private changeDetectorRefs: ChangeDetectorRef,
              ) { }

    async ngOnInit() {

      this.cards = this.dataSvc.fetchCards().pipe(
        map((cards: any) => cards.map(cardObj => {
          var c = new Card(cardObj.key, cardObj._frontImageUrl, cardObj._date, cardObj._rawData)

        return c
      }))
    );


    this.changeDetectorRefs.detectChanges();

    //this.dataSource.paginator = this.paginator;

  }

}

所以分页器上的最后一行注释是我在直接关联卡片观察后需要弄清楚的.请指教

so the last commented line on paginator is something i need to figure out with after i directly associated cards observavble. Please advise

推荐答案

您只需要获得对表的 DataSource 并在其中设置分页器.例如:

You just need to get a reference to your tables' DataSource and set your paginator in it. Eg:

dataSource = new MatTableDataSource()

async ngOnInit() {

  this.dataSvc.fetchCards().pipe(
    map((cards: any) => cards.map(cardObj => {
      var c = new Card(cardObj.key, cardObj._frontImageUrl, cardObj._date, cardObj._rawData)
  })).subscribe(cards => this.dataSource.data = cards);

);

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

  <mat-table [dataSource]="dataSource">

这篇关于使用带有可观察数组作为数据源的 mat-table 使用 mat-paginator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆