Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add mat-paginator for mat-cards?

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.

like image 903
Nivesh Elangovanraaj Avatar asked Jan 25 '19 14:01

Nivesh Elangovanraaj


People also ask

How do I display two mat cards on the same row?

Try adding display: inline-block !

How do I get pageIndex in Mat-Paginator?

You can get the current page index by using the page Output event. The $event from page returns three pieces of information: pageIndex.


1 Answers

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.

like image 58
Fabian Küng Avatar answered Sep 30 '22 05:09

Fabian Küng