import {Component, Input, Output, OnInit, OnDestroy, EventEmitter, OnChanges} from '@angular/core'; import {ActivatedRoute, Router} from '@angular/router'; import config from "@common/config"; import StorageService from "@common/services/storage"; import UtilService from "@common/services/util"; @Component({ selector: 'pagination', templateUrl: './template.html', styleUrls: ['./style.less'] }) export default class Pagination implements OnInit, OnDestroy, OnChanges { @Input() pageTotal: number; @Input() pageNum: any; @Input() totalItems: any; @Output() pageChange: EventEmitter = new EventEmitter(); pageCurrent = 1; pagesLength = 9; pageList = []; private paramsSub; pageSize = 10; constructor(private activatedRoute: ActivatedRoute, private storageService: StorageService, private router: Router) { } ngOnInit(): void { this.pageSize = this.storageService.get('pageSize') || config.limit; if (this.pageChange.observers.length > 0) { this.pageCurrent = this.pageNum; } else { this.paramsSub = this.activatedRoute.params.subscribe(params => { this.pageCurrent = (params).pageNum; this.getPageList() }) } } ngOnDestroy(): void { this.paramsSub && this.paramsSub.unsubscribe(); } ngOnChanges(changes: any): void { if (changes.pageNum) { this.pageCurrent = changes.pageNum.currentValue; } this.getPageList() } pageSizeChange(value) { this.storageService.set('pageSize', value); this.router.navigate(['../', 1, {timestamp: (new Date()).valueOf()}], { relativeTo: this.activatedRoute, queryParamsHandling: 'preserve', replaceUrl: true }) } goTo() { if (this.pageChange.observers.length > 0) { this.getPageList(); this.pageChange.emit(this.pageCurrent); } else { this.router.navigate(['../', this.pageCurrent], { relativeTo: this.activatedRoute, queryParamsHandling: 'preserve' }) } } prevPage() { if (this.pageCurrent > 1) { this.pageCurrent -= 1; this.goTo(); } } nextPage() { if (this.pageCurrent < this.pageTotal) { this.pageCurrent += 1; this.goTo(); } } changePage(item) { if (item == '...') { return; } else { this.pageCurrent = item; } this.goTo(); } private getPageList() { this.pageCurrent *= 1; let pageList = []; if (this.pageTotal <= this.pagesLength) { for (let i = 1; i <= this.pageTotal; i++) { pageList.push(i); } } else { let offset = (this.pagesLength - 1) / 2; if (this.pageCurrent <= offset) { // 左边没有... for (let i = 1; i <= offset + 1; i++) { pageList.push(i); } pageList.push('...'); pageList.push(this.pageTotal); } else if (this.pageCurrent > this.pageTotal - offset) { // 右边没有... pageList.push(1); pageList.push('...'); for (let i = offset + 1; i >= 1; i--) { pageList.push(this.pageTotal - i); } pageList.push(this.pageTotal); } else { // 两边都有... pageList.push(1); pageList.push('...'); for (let i = Math.ceil(offset / 2); i >= 1; i--) { pageList.push(this.pageCurrent - i); } pageList.push(this.pageCurrent); for (let i = 1; i <= offset / 2; i++) { pageList.push(this.pageCurrent + i); } pageList.push('...'); pageList.push(this.pageTotal); } } this.pageList = pageList; } }