2
votes

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

1

1 Answers

4
votes

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">