angular - 如何在角度 4 中重置组件类的分页
问题描述
这是我的分页组件类
import { Component, Input, EventEmitter, Output } from '@angular/core';
@Component({
selector: 'my-pagination',
templateUrl: './pagination.component.html',
styleUrls: ['./pagination.component.css']
})
export class PaginationComponent {
@Input() page: number;
@Input() count: number;
@Input() perPage: number;
@Input() loading: boolean;
@Input() pagesToShow: number;
@Output() goPrev = new EventEmitter<boolean>();
@Output() goNext = new EventEmitter<boolean>();
@Output() goPage = new EventEmitter<number>();
lastPageRefOld: number;
lastPageNew: number;
constructor() { }
getMin(): number {
return ((this.perPage * this.page) - this.perPage) + 1;
}
getMax(): number {
let max = this.perPage * this.page;
if (max > this.count) {
max = this.count;
}
return max;
}
onPage(n: number): void {
this.goPage.emit(n);
}
onPrev(): void {
this.goPrev.emit(true);
}
onNext(next: boolean): void {
this.goNext.emit(next);
}
totalPages(): number {
return Math.ceil(this.count / this.perPage) || 0;
}
lastPage(): boolean {
return this.perPage * this.page > this.count;
}
getPages(): number[] {
const c = Math.ceil(this.count / this.perPage);
const p = this.page || 1;
const pagesToShow = this.pagesToShow || 9;
const pages: number[] = [];
pages.push(p);
const times = pagesToShow - 1;
for (let i = 0; i < times; i++) {
if (pages.length < pagesToShow) {
if (Math.min.apply(null, pages) > 1) {
pages.push(Math.min.apply(null, pages) - 1);
}
}
if (pages.length < pagesToShow) {
if (Math.max.apply(null, pages) < c) {
pages.push(Math.max.apply(null, pages) + 1);
}
}
}
pages.sort((a, b) => a - b);
return pages;
}
}
这是我用于所有页面的分页html
<div class="pagination" *ngIf="count > 0">
<div>
<span class="page-totals"> {{ page }} of {{ totalPages() }} page</span>
</div>
<div class="numbers">
<!--<button class="button-pages" (click)="onPage(1)" [hidden]="page === 1 || loading" [ngClass]="{ 'disabled': page === 1 || loading }">First</button>-->
<button class="button-pages" (click)="onPrev()" [hidden]="page === 1 || loading" [ngClass]="{ 'disabled': page === 1 || loading }">< Previous</button>
<button class="button-pages" *ngFor="let pageNum of getPages()"
(click)="onPage(pageNum)"
[ngClass]="{'active': pageNum === page, 'disabled': loading}">
{{ pageNum }}
</button>
<!--<button class="button-pages"
(click)="onPage(pageNum + 5)"
[ngClass]="{'active': pageNum + 5, 'disabled': loading}">
...
</button>-->
<button class="button-pages" (click)="onNext(true)" [disabled]="lastPage() || loading" [ngClass]="{ 'disabled': lastPage() || loading }">Next ></button>
<!--<button class="button-pages" (click)="onPage(getMax())" [hidden]="lastPage(getMax()) || loading" [ngClass]="{ 'disabled': lastPage() || loading }">Last</button>-->
</div>
这就是我如何在我的页面上调用分页。
<my-pagination (goPage)="goToPage($event)"
(goNext)="onNext()"
(goPrev)="onPrev()"
[pagesToShow]="10"
[page]="page"
[perPage]="limit"
[count]="total"></my-pagination>
每当在我的搜索栏上搜索时,我都会遇到重置到默认页面 1 的问题。如果我将单击搜索按钮并显示新的数据集,如何重置此分页?
解决方案
推荐阅读
- html - 如何将视频元素内的 div 重叠为 9 部分响应?
- flutter - 如何将 StatefulWidget 的构造函数参数传递给 State?
- react-native - 将本机使用效果滚动到选定的项目对象
- api - 如果电子邮件(或其他)存在,我应该返回哪个 HTTP 响应?
- asp.net - 验证 1000 个外部 URL 的 ASP.Net 问题
- unity3d - LineRenderer 绘制的线即使在应用 Rigidbody2d 后也不会下降
- laravel-8 - Fontawesome 显示为不透明度 = 0
- ruby-on-rails - 伪造跨站请求伪造 (CSRF) 令牌
- swift - 使用 UICollectionViewCompositionalLayout 时如何修改特定组的项目形状
- python-3.x - 我可以使用没有图像字段的 Django truncatewords 过滤器吗?