Hei*_*erg 29 pagination angular-material angular
我是角色的新手,并试图在我的应用程序中实现分页.我正在尝试使用这个材料组件.
用下面的代码,我可以得到length,pagesize和pageSizeOptions在我的.ts文件
<md-paginator [length]="length"
[pageSize]="pageSize"
[pageSizeOptions]="pageSizeOptions"
</md-paginator>
Run Code Online (Sandbox Code Playgroud)
export class PaginatorConfigurableExample {
length = 100;
pageSize = 10;
pageSizeOptions = [5, 10, 25, 100];
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当页面被更改时,我似乎无法触发更改上表中数据的功能.此外,我怎么使用nextPage,previousPage,hasNextPage,和hasPreviousPage方法呢?
ael*_*lkz 34
我在这里苦苦挣扎.但我可以告诉你我做了一些研究.基本上,你刚开始添加在页面@Output事件foo.template.ts:
<md-paginator #paginator
[length]="length"
[pageIndex]="pageIndex"
[pageSize]="pageSize"
[pageSizeOptions]="[5, 10, 25, 100]"
(page)="pageEvent = getServerData($event)"
>
</md-paginator>
Run Code Online (Sandbox Code Playgroud)
之后,您必须在foo.component.ts类和其他类中添加pageEvent属性来处理分页器要求:
pageEvent: PageEvent;
datasource: null;
pageIndex:number;
pageSize:number;
length:number;
Run Code Online (Sandbox Code Playgroud)
并添加将获取服务器数据的方法:
ngOnInit() {
getServerData(null) ...
}
public getServerData(event?:PageEvent){
this.fooService.getdata(event).subscribe(
response =>{
if(response.error) {
// handle error
} else {
this.datasource = response.data;
this.pageIndex = response.pageIndex;
this.pageSize = response.pageSize;
this.length = response.length;
}
},
error =>{
// handle error
}
);
return event;
}
Run Code Online (Sandbox Code Playgroud)
因此,基本上每次单击paginator时,都会激活getServerData(..)方法,该方法将调用foo.service.ts获取所需的所有数据.在这种情况下,您不需要处理nextPage和nextXXX事件,因为它将在视图呈现时自动计算.
希望这可以帮到你.如果你有成功,请告诉我.=]
avo*_*ado 19
根据 Wesley Coetzee 的回答,我写了这个。希望它可以帮助任何使用谷歌搜索这个问题的人。我在交换列表中间的分页器大小时遇到了错误,这就是我提交答案的原因:
分页器 html 和列表
<mat-paginator [length]="localNewspapers.length" pageSize=20
(page)="getPaginatorData($event)" [pageSizeOptions]="[10, 20, 30]"
showFirstLastButtons="false">
</mat-paginator>
<mat-list>
<app-newspaper-pagi-item *ngFor="let paper of (localNewspapers |
slice: lowValue : highValue)"
[newspaper]="paper">
</app-newspaper-pagi-item>
Run Code Online (Sandbox Code Playgroud)
组件逻辑
import {Component, Input, OnInit} from "@angular/core";
import {PageEvent} from "@angular/material";
@Component({
selector: 'app-uniques-newspaper-list',
templateUrl: './newspaper-uniques-list.component.html',
})
export class NewspaperUniquesListComponent implements OnInit {
lowValue: number = 0;
highValue: number = 20;
// used to build a slice of papers relevant at any given time
public getPaginatorData(event: PageEvent): PageEvent {
this.lowValue = event.pageIndex * event.pageSize;
this.highValue = this.lowValue + event.pageSize;
return event;
}
}
Run Code Online (Sandbox Code Playgroud)
Wes*_*zee 15
嘿所以我偶然发现了它,并使它工作,这是如何:
在我的component.html中
<mat-paginator #paginator [pageSize]="pageSize" [pageSizeOptions]="[5, 10, 20]" [showFirstLastButtons]="true" [length]="totalSize"
[pageIndex]="currentPage" (page)="pageEvent = handlePage($event)">
</mat-paginator>
Run Code Online (Sandbox Code Playgroud)
在我的组件里面
public array: any;
public displayedColumns = ['', '', '', '', ''];
public dataSource: any;
public pageSize = 10;
public currentPage = 0;
public totalSize = 0;
@ViewChild(MatPaginator) paginator: MatPaginator;
constructor(private app: AppService) { }
ngOnInit() {
this.getArray();
}
public handlePage(e: any) {
this.currentPage = e.pageIndex;
this.pageSize = e.pageSize;
this.iterator();
}
private getArray() {
this.app.getDeliveries()
.subscribe((response) => {
this.dataSource = new MatTableDataSource<Element>(response);
this.dataSource.paginator = this.paginator;
this.array = response;
this.totalSize = this.array.length;
this.iterator();
});
}
private iterator() {
const end = (this.currentPage + 1) * this.pageSize;
const start = this.currentPage * this.pageSize;
const part = this.array.slice(start, end);
this.dataSource = part;
}
Run Code Online (Sandbox Code Playgroud)
所有的分页工作都是从该iterator方法中完成的.此方法适用了skip,并take和该分配到dataSource的材料表.
使用Slice Pipe将Angular Paginator与数据表链接的另一种方法.这些数据仅从服务器获取一次.
视图:
<div class="col-md-3" *ngFor="let productObj of productListData |
slice: lowValue : highValue">
//actual data dispaly
</div>
<mat-paginator [length]="productListData.length" [pageSize]="pageSize"
(page)="pageEvent = getPaginatorData($event)">
</mat-paginator>
Run Code Online (Sandbox Code Playgroud)
零件
pageIndex:number = 0;
pageSize:number = 50;
lowValue:number = 0;
highValue:number = 50;
getPaginatorData(event){
console.log(event);
if(event.pageIndex === this.pageIndex + 1){
this.lowValue = this.lowValue + this.pageSize;
this.highValue = this.highValue + this.pageSize;
}
else if(event.pageIndex === this.pageIndex - 1){
this.lowValue = this.lowValue - this.pageSize;
this.highValue = this.highValue - this.pageSize;
}
this.pageIndex = event.pageIndex;
}
Run Code Online (Sandbox Code Playgroud)
小智 6
成分:
import { Component, AfterViewInit, ViewChild } from @angular/core;
import { MatPaginator } from @angular/material;
export class ClassName implements AfterViewInit {
@ViewChild(MatPaginator) paginator: MatPaginator;
length = 1000;
pageSize = 10;
pageSizeOptions: number[] = [5, 10, 25, 100];
ngAfterViewInit() {
this.paginator.page.subscribe(
(event) => console.log(event)
);
}
Run Code Online (Sandbox Code Playgroud)
HTML
<mat-paginator
[length]="length"
[pageSize]="pageSize"
[pageSizeOptions]="pageSizeOptions"
[showFirstLastButtons]="true">
</mat-paginator>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
49036 次 |
| 最近记录: |