不想使用第三方库,只想使用一个分页器,那么就简单的实现一个,效果如下:

1.使用方式:


<custom-pagination *ngIf="enterpriseList.length"
[fastTurnBtn]="false"
[totalPage]="paginationParams.totalPages"
[maxSize]="paginationParams.maxSize"
[(ngModel)]="paginationParams.currentPage"
(changePage)="changePage($event)">
</custom-pagination>

2.可配置项:


  • fsatTurnBtn:是否显示首页和末页
  • turnBtn:是否显示上下翻页
  • maxSize:最多显示几页
  • totalPage:总页数
  • moreBtn:是否显示省略号提示更多分页

3.实现方案:


先上代码,这里使用啦ngModel实现自定义组件的数据双向绑定,可以看上一篇随记的介绍:

// custom-pagination.ts

import { Component, OnInit, Input, Output, EventEmitter, OnChanges, forwardRef  } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; @Component({
selector: 'custom-pagination',
templateUrl: './pagination.component.html',
styleUrls: ['./pagination.component.css'],
providers: [{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => PaginationComponent),
multi: true
}]
})
export class PaginationComponent implements OnInit, OnChanges { constructor() { }
@Input() totalPage: any;
@Input() maxSize: any = 5;
@Input() moreBtn: Boolean = true;
@Input() turnBtn: Boolean = true;
@Input() fastTurnBtn: Boolean = true;
@Output() currentPageChange: EventEmitter<Number> = new EventEmitter;
@Output() changePage: EventEmitter<Number> = new EventEmitter;
private currentPage = 1;
showPageList: Array<number> = [];
showEndPage = 0;
showBeginPage = 0;
showLeftMoreStatus = false;
showRightMoreStatus = false;
ngOnInit() {
}
ngOnChanges () { // 异步获取的数据,在ngOnChange里监听数据变化后再处理分页
this.initPages();
}
currentChange() {
this.currentPageChange.emit(this.currentPage);
}
goToPage (page) {
if (page && this.currentPage !== page) {
this.currentPage = page;
this.changePageHandle();
}
}
prevNextPage (page) {
console.log(this.currentPage)
if (this.totalPage < 2) {
return;
}
let pageNum;
if (page === '上一页') {
pageNum = this.currentPage === 1 ? this.currentPage : this.currentPage - 1;
} else {
pageNum = this.currentPage === this.totalPage ? this.currentPage : this.currentPage + 1;
}
if (pageNum !== this.currentPage) {
this.currentPage = pageNum;
this.changePageHandle();
}
}
leftMoreClick () { // 左更多按钮点击后处理当前显示的分页
const startPage = this.showBeginPage - this.maxSize;
const endPage = startPage + this.maxSize;
this.currentPage -= Math.ceil((endPage - startPage) / 2);
this.changePageHandle()
}
rightMoreClick () { // 右更多分页按钮点击后处理当前显示的分页
let startPage;
if ((this.showEndPage + this.maxSize) < this.totalPage) {
startPage = this.showEndPage + this.maxSize;
} else {
startPage = this.totalPage - this.maxSize;
}
const endPage = startPage + this.maxSize;
this.currentPage += Math.ceil((endPage - startPage) / 2);
this.changePageHandle()
}
formatPages () { // 操作页码后处理需要显示的新页码数据
if (this.totalPage > this.maxSize) {
const formatRightPage = this.showEndPage - Math.ceil(this.maxSize / 2); // 需要向后处理显示分页数据的分界点
const formatLeftPage = this.showBeginPage + Math.floor(this.maxSize / 2); // 需要向前处理显示分页数据的分界点
let startPage; // 需要显示的开始页码
if (this.currentPage > formatRightPage || this.currentPage < formatLeftPage) {
startPage = this.currentPage - Math.floor(this.maxSize / 2) > 0 ? this.currentPage - Math.floor(this.maxSize / 2) : 1;
this.showBeginPage = startPage;
this.showEndPage = (startPage + this.maxSize) < this.totalPage ? (startPage + this.maxSize) : this.totalPage;
if (this.showEndPage - this.showBeginPage <= this.maxSize) { // 如果处理后显示的分页数量少于maxSize,处理需要显示的开始页码满足maxSize
startPage = this.showEndPage - this.maxSize;
this.showBeginPage = startPage;
}
this.handlePagesData(startPage, this.showEndPage);
}
}
console.log(this.showPageList)
}
initPages () { // 根据传入的参数初始化页码
if (this.totalPage > this.maxSize) {
this.maxSize--;
const startPage = this.currentPage;
this.showBeginPage = startPage;
this.showEndPage = startPage + this.maxSize;
this.handlePagesData(startPage, this.showEndPage);
} else {
this.showBeginPage = this.currentPage;
this.showEndPage = this.totalPage;
for (let i = 1; i <= this.totalPage; i++) {
this.showPageList.push(i)
}
}
this.showPagesMore();
}
handlePagesData (begin, end) { // 循环生成要显示的页码数据
this.showPageList = [];
for (let i = begin; i <= end; i++) {
this.showPageList.push(i)
}
}
showPagesMore () { // 判断是否满足显示向左向右更多分页按钮的条件
if (this.currentPage > this.maxSize * 2) {
this.showLeftMoreStatus = true;
} else {
this.showLeftMoreStatus = false;
}
if (this.showEndPage < this.totalPage) {
this.showRightMoreStatus = true;
} else {
this.showRightMoreStatus = false;
}
}
changePageHandle () { // 翻页后触发方法
this.formatPages();
this.showPagesMore();
this.onModelChange(this.currentPage); // 触发ngModel绑定的数据更新
this.changePage.emit(this.currentPage); // 向外触发自定义方法,并传值
}
onModelChange: Function = () => { };
// 页面的值改变,调用改方法,并调用onModelChange传入改变后的值,实现值得回传
writeValue(val): void {
// 页面初始化时时,调用该方法,传入初始值
if (val) {
this.currentPage = val;
}
}
registerOnChange(fn: any): void {
// 页面值改变时,调用该方法,传入新值实现回传
this.onModelChange = fn;
}
registerOnTouched(fn: any): void {
} }
<!--custom-pagination.html-->

<ul class="custom-pagination">
<li class="page-item first-page" *ngIf="fastTurnBtn" [ngClass]="{'disabled': currentPage === 1}" (click)="goToPage(1)"><span><<</span></li>
<li class="page-item prev-page" *ngIf="turnBtn" [ngClass]="{'disabled': currentPage === 1}" (click)="prevNextPage('上一页')"><span><</span></li>
<li class="page-item left-more-page" *ngIf="showLeftMoreStatus && moreBtn" (click)="leftMoreClick()" title="查看更多"><span></span></li>
<li class="page-item" *ngFor="let item of showPageList" [ngClass]="{'active': currentPage === item}" (click)="goToPage(item)">{{item}}</li>
<li class="page-item right-more-page" *ngIf="showRightMoreStatus && moreBtn" (click)="rightMoreClick()" title="查看更多"><span></span></li>
<li class="page-item next-page" *ngIf="turnBtn" [ngClass]="{'disabled': currentPage === totalPage}" (click)="prevNextPage('下一页')"><span>></span></li>
<li class="page-item last-page" *ngIf="fastTurnBtn" [ngClass]="{'disabled': currentPage === totalPage}" (click)="goToPage(totalPage)"><span>>></span></li>
</ul>
// custom-pagination.css

.custom-pagination{
overflow: hidden;
margin: 10px 0;
text-align: center;
}
.page-item{
display: inline-block;
width: 25px;
height: 25px;
line-height: 23px;
border: 1px solid #06a0e7;
color: #06a0e7;
text-align: center;
border-radius: 3px;
margin: 0 2px;
cursor: pointer;
user-select: none;
vertical-align: middle;
}
.prev-page,.next-page{
width: auto;
padding: 0 2px;
}
.page-item.active{
border-color: #06a0e7;
background: #06a0e7;
color: #fff;
}
.disabled{
cursor: not-allowed;
border-color: #d9d9d9;
color: #00000040;
}
.prev-page span,.next-page span,.first-page span,.last-page span{
display: inline-block;
transform: scale(.5, 1.2) translateY(-1px);
min-width: 20px;
}
.left-more-page span,.right-more-page span{
position: relative;
display: inline-block;
width: 100%;
height: 100%;
}
.left-more-page span:after,.right-more-page span:after{
position: absolute;
content: '•••';
width: 100%;
height: 100%;
left:;
top:;
font-size: 12px;
}
.left-more-page:hover span:after{
content: '<<';
transform: scale(.5, 1.2);
}
.right-more-page:hover span:after{
content: '>>';
transform: scale(.5, 1.2);
}

以上方案可实现简单的分页器组件,有可以更好的实现方案或者优化实现的,希望指出。

angular实现简单的pagination分页组件的更多相关文章

  1. 手把手教你使用Vue/React/Angular三大框架开发Pagination分页组件

    DevUI是一支兼具设计视角和工程视角的团队,服务于华为云DevCloud平台和华为内部数个中后台系统,服务于设计师和前端工程师.官方网站:devui.designNg组件库:ng-devui(欢迎S ...

  2. 第二百零九节,jQuery EasyUI,Pagination(分页)组件

    jQuery EasyUI,Pagination(分页)组件 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI 中 Pagination(分页)组件的使 ...

  3. Vue 2.0 pagination分页组件

    最近写了一个分页组件,效果如下图: f-pagination.js文件 Vue.component('f-pagination',{ template:'<div class="fPa ...

  4. EasyUI - pagination 分页组件

    总页数是手动填写,后续进行更改……………… 效果: html代码: <!--使用标签创建--> <%--<div id="pp" class="e ...

  5. Angular4.+ ngx-bootstrap Pagination 自定义分页组件

    Angular4 随笔(二)  ——自定义分页组件 1.简介 本组件主要是实现了分页组件显示功能,通过使用 ngx-bootstrap Pagination分页组件实现. 基本逻辑: 1.创建一个分页 ...

  6. 基于Vue.js的表格分页组件

    有一段时间没更新文章了,主要是因为自己一直在忙着学习新的东西而忘记分享了,实在惭愧. 这不,大半夜发文更一篇文章,分享一个自己编写的一个Vue的小组件,名叫BootPage. 不了解Vue.js的童鞋 ...

  7. angular-ui分页组件

    http://angular-ui.github.io/bootstrap/#/pagination 分页组件只提供生成分页按钮,数据的显示需要使用ng-repeat, 注意设置 items-per- ...

  8. 基于Vue封装分页组件

    使用Vue做双向绑定的时候,可能经常会用到分页功能 接下来我们来封装一个分页组件 先定义样式文件 pagination.css ul, li { margin: 0px; padding: 0px;} ...

  9. Vue.js的表格分页组件

    转自:http://www.cnblogs.com/Leo_wl/p/5522299.html 有一段时间没更新文章了,主要是因为自己一直在忙着学习新的东西而忘记分享了,实在惭愧. 这不,大半夜发文更 ...

随机推荐

  1. LeetCode——Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 写一个函数找出字符串数组中 ...

  2. 福利贴——爬取美女图片的Java爬虫小程序代码

    自己做的一个Java爬虫小程序 废话不多说.先上图. 目录命名是用标签缩写,假设大家看得不顺眼能够等完成下载后手动改一下,比方像有强迫症的我一样... 这是挂了一个晚上下载的总大小,只是还有非常多由于 ...

  3. Hadoop - YARN NodeManager 剖析

    一 概述         NodeManager是执行在单个节点上的代理,它管理Hadoop集群中单个计算节点,功能包含与ResourceManager保持通信,管理Container的生命周期.监控 ...

  4. ASMlib操作系统包安装与配置asm disk磁盘

    1.加入6块硬盘,每块100g.不管是热加还是冷加.不管是加硬盘,用san存储划lun,或者再加上多路径,都是能够这么做的. 在操作系统层,能识别这种lun.以下的sdb就是一个刚划分的300g的lu ...

  5. eclipse:报错信息The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path

    JavaWeb: 报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Bui ...

  6. Excel操作之VLOOKUP

    https://support.office.com/en-us/article/VLOOKUP-function-0bbc8083-26fe-4963-8ab8-93a18ad188a1 Use V ...

  7. javascript中如何获取对象名

    javascript中如何获取对象名 一.总结 一句话总结:将对象传入参数,看参数是否为函数(js中的对象和函数是一个意思么(函数肯定是对象)),对象参数.name属性即可获得 //版本4 funct ...

  8. Spring MVC模式示例(采用解耦控制器+校验器)

    Product package com.mstf.bean; import java.io.Serializable; /** * Product类,封装了一些信息,包含三个属性 * @author ...

  9. shell call python

    python -c "import os; p=os.getcwd(); print(p);print(p);print(p);print('test over')"

  10. UVa 140 Bandwidth【枚举排列】

    题意:给出n个节点的图,和一个节点的排列,定义节点i的带宽b[i]为i和其相邻节点在排列中的最远的距离,所有的b[i]的最大值为这个图的带宽,给一个图,求出带宽最小的节点排列 看的紫书,紫书上说得很详 ...