如何为席卡添加席票?

时间:2019-01-25 14:22:35

标签: angular pagination angular-material angular6 angular7

我是Angular的新手,正在寻找分页卡片上的分页。我看到的示例仅对表进行分页。

我有数百个代码,希望将其限制为每页8-10张卡。如何做到这一点? 这是我的html和ts代码。

.html文件

<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文件

  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;
    });  
    }
  }

如果需要,我会将代码添加到stackblitz中。

1 个答案:

答案 0 :(得分:2)

This答案总结了如何使mat-paginator在没有mat-table的情况下工作的要点。以下是您或其他任何人将mat-paginator与其他元素/组件一起使用时需要进行的详细更改。

您需要像这样在模板的底部添加一个mat-paginator

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

然后使用ViewChild在您的组件中访问它:

@ViewChild(MatPaginator) paginator: MatPaginator;

将其链接到ngOnInit中的数据源:

this.dataSource.paginator = this.paginator;

并重写绑定数据源的方式:

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();
}

然后像这样使用模板中的数据源:

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

可以找到mat-cardmat-paginator一起工作的堆叠闪电战   here