niv*_*K.E 3 pagination angular-material angular 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>
Run Code Online (Sandbox Code Playgroud)
.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;
});
}
}
Run Code Online (Sandbox Code Playgroud)
如果需要,我将代码添加到stackblitz中。
这个答案总结了如何在没有mat-paginator工作的情况下工作的关键点mat-table。以下是您或其他任何人需要mat-paginator与其他元素/组件一起使用的详细更改。
您需要mat-paginator像这样在模板的底部添加一个:
<mat-paginator [pageSizeOptions]="[5, 10, 25, 100]"></mat-paginator>
Run Code Online (Sandbox Code Playgroud)
然后使用以下命令在您的组件中访问它ViewChild:
@ViewChild(MatPaginator) paginator: MatPaginator;
Run Code Online (Sandbox Code Playgroud)
将其链接到您的数据源ngOnInit:
this.dataSource.paginator = this.paginator;
Run Code Online (Sandbox Code Playgroud)
并重写您如何绑定数据源:
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();
}
Run Code Online (Sandbox Code Playgroud)
然后像这样在模板中使用数据源:
<mat-card *ngFor="let card of obs | async">
<!-- display your data here -->
</mat-card>
Run Code Online (Sandbox Code Playgroud)
工作stackblitz用于
mat-card与mat-paginator可找到 这里。
| 归档时间: |
|
| 查看次数: |
700 次 |
| 最近记录: |