import { PaginationDto } from './../../shared-model'; import { Component, OnInit, Input, Output, EventEmitter, OnChanges, SimpleChanges } from '@angular/core'; @Component({ selector: 'rh-basic-pagination', templateUrl: './basic-pagination.component.html', styleUrls: ['./basic-pagination.component.less'] }) export class RhBasicPaginationComponent implements OnInit, OnChanges { @Input() rhDataLength = 0; /** 数据量 */ // nzCount = 0; /** 每个的数据量 */ @Input() rhPageSize = 10; @Input() rhPageIndex = 1; @Output() rhPageIndexChange = new EventEmitter(); @Output() rhClose = new EventEmitter(); /** 要显示的页码集合 */ pages: number[] = []; /** 分页器模型 */ pager!: PaginationDto; constructor() { // } ngOnInit() { // this.nzCount = this.nzData.length; } ngOnChanges(changes: SimpleChanges) { for (const key in changes) { // eslint-disable-next-line no-prototype-builtins if (changes.hasOwnProperty(key) && key === 'rhDataLength' && changes[key].currentValue) { // const element = changes[key]; this.pager = this.getPager(this.rhDataLength, this.rhPageIndex, this.rhPageSize); // console.log("变化", this.pager) // this.rhPageIndexChange.emit(this.pager); } } } back() { this.rhClose.emit(); } /** 跳转到指定页码 */ skipToPage(pageIndex: number) { // console.log("变化", pageIndex) this.pager = this.getPager(this.rhDataLength, pageIndex, this.rhPageSize); this.rhPageIndexChange.emit(this.pager); } getPager(totalItems: number, pageIndex: number, pageSize: number): PaginationDto { // calculate total pages const totalPages = Math.ceil(totalItems / pageSize); // ensure current page isn't out of range if (pageIndex < 1) { pageIndex = 1; } else if (pageIndex > totalPages) { pageIndex = totalPages; } let startPage: number; let endPage: number; if (totalPages <= 10) { // less than 10 total pages so show all startPage = 1; endPage = totalPages; } else { // more than 10 total pages so calculate start and end pages if (pageIndex <= 6) { startPage = 1; endPage = 10; } else if (pageIndex + 4 >= totalPages) { startPage = totalPages - 9; endPage = totalPages; } else { startPage = pageIndex - 5; endPage = pageIndex + 4; } } // calculate start and end item indexes const startIndex = (pageIndex - 1) * pageSize; const endIndex = Math.min(startIndex + pageSize, totalItems); // create an array of pages to ng-repeat in the pager control this.pages = Array.from(Array(endPage + 1 - startPage).keys()).map((i) => startPage + i); return new PaginationDto(totalItems, totalPages, pageIndex, pageSize, startPage, endPage, startIndex, endIndex, this.pages); } }