如何为席卡添加席分页器? [英] How to add mat-paginator for mat-cards?

查看:55
本文介绍了如何为席卡添加席分页器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

<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中.

I will add my code in stackblitz if needed.

推荐答案

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

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.

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

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>

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

Then access it in your component with ViewChild:

@ViewChild(MatPaginator) paginator: MatPaginator;

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

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>

可以找到

带有 mat-paginator mat-card 的工作堆栈闪电战此处.

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

这篇关于如何为席卡添加席分页器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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