10

I'm new to Angular and I'm looking for pagination on mat-cards. The examples I've seen has only pagination for tables.

I have hundreds of code and would like to restrict it to 8-10 cards per page. How to acheive this? This is my html and ts code.

.html file

<div  class="flip-right" [ngClass]="mobileQuery.matches ? 'card-wrapper-mobile' : 'card-wrapper'" *ngFor="let items of datasource">
   <div class="card">
      <div class="front">
          <mat-card [ngClass]="mobileQuery.matches ? 'mobile-card' : 'desktop-card'">
                    <a routerLink="/viewPage">
                      <mat-card-header class="header">
                          <mat-card-title>{{items.name}} <mat-icon class="verified title" [inline]="true" >lock</mat-icon></mat-card-title>
                        </mat-card-header>
                            <mat-card-subtitle class="first-subtitle">Last updated on:{{items.Last_updated_on}}</mat-card-subtitle>
                            <mat-card-subtitle>Last updated by:{{items.Last_updated_by}}</mat-card-subtitle>
                        <mat-card-subtitle>{{items.created_by}}</mat-card-subtitle>

                    </a>
            </mat-card>
  </div>
  <div class="back">
      <mat-card [ngClass]="mobileQuery.matches ? 'mobile-card' : 'desktop-card'">
                <a routerLink="/viewPage">
                  <mat-card-header>
                      <mat-card-title>{{items.name}}<mat-icon class="verified" [inline]="true" >lock</mat-icon></mat-card-title>
                    </mat-card-header>

                </a>
        </mat-card>
      </div>
     </div>
    </div>

.ts file

  import { Component, OnInit,ViewChild } from '@angular/core';
  import { Subscription } from 'rxjs';
  import { CardsInfoService } from '../cards-info.service';
  import {MediaMatcher} from '@angular/cdk/layout';
  import {ChangeDetectorRef, OnDestroy} from '@angular/core';


  @Component({
  selector: 'app-private',
  templateUrl: './private.component.html',
  styleUrls: ['./private.component.scss']
  })
  export class PrivateComponent implements OnInit,OnDestroy {
  datasource=[];
  subscription: Subscription;
  mobileQuery: MediaQueryList;

  private _mobileQueryListener: () => void;

  constructor( media: MediaMatcher,
  changeDetectorRef: ChangeDetectorRef,
  private cardsInfo :CardsInfoService) { 

  this.mobileQuery = media.matchMedia('(max-width: 600px)');
  this._mobileQueryListener = () => changeDetectorRef.detectChanges();
  this.mobileQuery.addListener(this._mobileQueryListener);
  }

  ngOnDestroy(): void {
  this.mobileQuery.removeListener(this._mobileQueryListener);
  }
  ngOnInit(){
   this.cardsInfo.getCardsInfo()
   .subscribe(
    data => {
    this.datasource = data;
    });  
    }
  }

I will add my code in stackblitz if needed.

Fabian Küng
  • 5,925
  • 28
  • 40

1 Answers1

15

This answer sums up the key points on how to make mat-paginator work without a mat-table. Below are the detailed changes you or anyone else would need to make to use mat-paginator with other elements/components.

You would need to add a mat-paginator at the bottom of your template like this:

<mat-paginator [pageSizeOptions]="[5, 10, 25, 100]"></mat-paginator>

Then access it in your component with ViewChild:

@ViewChild(MatPaginator) paginator: MatPaginator;

Link it to your datasource in ngOnInit:

this.dataSource.paginator = this.paginator;

And rewrite how you bind your datasource:

obs: Observable<any>;
// Card is whatever type you use for your datasource, DATA is your data
dataSource: MatTableDataSource<Card> = new MatTableDataSource<Card>(DATA);

ngOnInit() {
  this.obs = this.dataSource.connect();
}

And then use the datasource in your template like this:

<mat-card *ngFor="let card of obs | async">
  <!-- display your data here -->
</mat-card>

Working stackblitz for mat-card with mat-paginator can be found here.

Fabian Küng
  • 5,925
  • 28
  • 40
  • Nice and clear answer! Why is `this.changeDetectorRef.detectChanges()` called on `ngOnInit()`? – GCSDC Jan 25 '19 at 16:48
  • 2
    It is to prevent the error message `ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked.`. See [this](https://stackoverflow.com/questions/34364880/expression-has-changed-after-it-was-checked) post for more information on why that happens and how to solve it (one approach as shown in the stackblitz is to trigger change detection yourself, e.g. manually via `detectChanges()`). – Fabian Küng Jan 25 '19 at 18:08
  • 1
    Can't thank you enough! This is exactly what i was looking for. – Nivesh Elangovanraaj Jan 25 '19 at 18:16