1.起因

  今日看完element中分页器的源码实现,比较简单,遂自己按着理解实现了一个简单的分页器,记录下来,以便日后温习.

2.实现难点

  分页器的实现难点主要是什么时候显示分页器的省略, 我的思路是: 规定一个值foldPage, 意为当前最多显示的标签数,当总页数超过即显示省略.省略分为左边省略(folder1)和

右边省略(folder2),布局代码如下:

<div class="pagination" @click="pageClick">
<button class="pre">上一页</button>
<ul class="pages">
<li :class="['first', { 'active' : current == 1 }]" v-if="total">
1
</li>
<li :class="[ testLeft,goback]"
v-show="showPreMore"
@mouseenter="testLeft='more-left'"
@mouseleave="testLeft='more'"></li> <li :class="['page-item', { 'active' : current == item }]" v-for="item in $pages">
{{ item }}
</li> <li :class="[ testRight,gogo]"
v-show="showNextMore"
@mouseenter="testRight='more-right'"
@mouseleave="testRight='more'"></li> <li :class="['last', { 'active' : current == $last }]" v-if="total">
{{ $last }}
</li>
</ul>
<button class="next">下一页</button> </div>

$pages是一个计算属性,用于动态生成中间的页码,以及控制folder1和folder2的显示,代码如下:

computed:{
// 中间页数组
$pages(){
const foldPage = this.foldPage
const current = Number(this.current)
const halfFoldPage = Math.floor((foldPage-2)/2) if (this.$last > foldPage){
if (current - halfFoldPage > 2){
this.showPreMore = true
}else {
this.showPreMore = false
} if (current + halfFoldPage < this.$last){
this.showNextMore = true
}else {
this.showNextMore = false
}
} let array = [] // folder1显示
if (this.showNextMore && !this.showPreMore){
for(let i = 2; i < foldPage; i++){
array.push(i)
}
// folder1 和 folder2都显示
}else if ( this.showPreMore && this.showNextMore ){
for(let i = current - halfFoldPage; i <= current + halfFoldPage; i++ ){
array.push(i)
}
// folder2显示
}else if (!this.showNextMore && this.showPreMore){
// 当folder2显示的时候,页码不能大于$last,需要往前多显示差额
let dis = current + halfFoldPage - this.$last + 1; for(let i = current - halfFoldPage - dis ; i < this.$last; i++){
array.push(i)
}
// 都不显示
}else {
for(let i = 2; i < this.$last; i++){
array.push(i)
}
}
return array
},
// 总页数
$last(){
return Math.ceil(this.total/this.size)
}
}

所有的点击都用一个函数处理, 根据e.target判断点击的目标.从而做出相应的逻辑:

methods:{
pageClick(e){
let newPage = Number(e.target.textContent)
this.current = Number(this.current); if (!isNaN(newPage) && newPage){
this.current = newPage
}else {
// 下一页
if (e.target.className.indexOf('next') != -1){
if (this.current == this.$last){
return;
}
this.current ++
}
// 上一页
else if (e.target.className.indexOf('pre') != -1){
if (this.current == 1){
return
}
this.current --
}
// 省略向左
else if (e.target.className.indexOf('left') != -1){
this.current -= this.foldPage - 2 if (this.current <= 1){
this.current = 1
return
}
}
// 省略向右
else if(e.target.className.indexOf('right') != -1){
this.current += this.foldPage - 2 if (this.current >= this.$last){
this.current = this.$last
return
}
} }
}
},

3.总结

  pagination组件在element中算是一个很简单的组件,静下心来看不是很复杂,理解其思路以后可以自己尝试去写出来,细节可以无需在意.

  

vue实现分页器(仿element)的更多相关文章

  1. [Vue warn]: Cannot find element: #main

    使用vue框架的时候,如果页面提示如下错误,则说明new Vue的时候没有传入 el 的值: [Vue warn]: Cannot find element: #main 我们传入el 既可以,如: ...

  2. vue按需引入element或mint

    vue按需引入element或mint需要添加 babel-preset-es2015 和babel-plugin-component

  3. 使用Vue.js制作仿Metronic高级表格(一)静态设计

    Metronic高级表格是Metonic框架中自行实现的表格,其底层是Datatables.本教程将主要使用Vue实现交互部分,使用Bootstrap做样式库.jQuery做部分用户交互(弹窗). 使 ...

  4. Vue报错 [Vue warn]: Cannot find element

    在前端开发全面进入前端的时代 作为一个合格的前端开发工作者 框架是不可或缺的Vue React Anguar 作为前端小白,追随大佬的脚步来到来到博客园,更新现在正在学习的Vue 注 : 相信学习Vu ...

  5. 用vue写一个仿简书的轮播图

    原文地址:用vue写一个仿简书的轮播图 先展示最终效果: Vue的理念是以数据驱动视图,所以拒绝通过改变元素的margin-top来实现滚动效果.写好css样式,只需改变每张图片的class即可实现轮 ...

  6. vue报错[Vue warn]: Unknown custom element: <router-Link> - did you register the component correctly? For recursive components, make sure to provide the "name" option.

    vue浏览器报错,如下 vue.runtime.esm.js?2b0e:619 [Vue warn]: Unknown custom element: <router-Link> - di ...

  7. Vue. 之 npm安装Element

    Vue. 之 npm安装Element 前提: 相关运行环境以搭建完成,例如:Node.Npm等.    假如我的项目目录如下: D:\DISK WORKSPACE\VSCODE\CDS\cds-ap ...

  8. vue components registration & vue error & Unknown custom element

    vue components registration & vue error & Unknown custom element vue.esm.js:629 [Vue warn]: ...

  9. [Vue warn]: Unknown custom element: <sapn> - did you register the component correctly? For recursive components, make sure to provide the "name" option. found in ---> <Evaluate> at src/views/index/

    关于vue报错: [Vue warn]: Unknown custom element: <sapn> - did you register the component correctly ...

随机推荐

  1. Cocos2d切换场景出现的问题-error C2653: “***”不是类或命名空间名称

    1,在开头引入头文件 2,在要引入的头文件中,去除以下代码: #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #endif ...

  2. HDU 3756

    很容易就想到把三维转化成了二维,求出点离Z轴的距离,把这个距离当成X坐标,Z轴当Y坐标,然后就变成了求一个直角三角形覆盖这些点 像上一题一样,确定斜率直线的时候,必定是有一点在线上的.于是,可以把直线 ...

  3. hdu 4717 The Moving Points(三分)

    http://acm.hdu.edu.cn/showproblem.php?pid=4717 大致题意:给出每一个点的坐标以及每一个点移动的速度和方向. 问在那一时刻点集中最远的距离在全部时刻的最远距 ...

  4. Linux Kernel(Android) 加密算法总结(一)(cipher、compress、digest)

    1. Linux内核支持哪些加密算法 ? 内核支持的加密算法非常多,包含: 对称加密算法.如AES,3DES. 对称password体制的发展趋势将以分组password为重点. 分组password ...

  5. 树莓派 rtl8188eu 芯片wifi驱动

    总算是找到了.现拿出来分享.參考地址:https://www.raspberrypi.org/forums/viewtopic.php? p=462982#p462982 下载的地址是:https:/ ...

  6. Milk(杭电1070)

    Milk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  7. Fatal error: Incompatible file format: The encoded file has format major ID 1...解决方式

    申请好域名和空间后.将站点源代码上传到空间,解析好域名后.在地址栏输入域名出现以下错误: Fatal error: Incompatible file format: The encoded file ...

  8. insmod hello.ko -1 Invalid module format最简单的解决的方法

    在下也是从网上搜索到的这样的解决的方法. 遇到这样的情况后,通过dmesg看一下内核日志. 假设发现有例如以下日志.那就好办了. hello: version magic '2.6.33.3 ' sh ...

  9. 读配置文件能够保持顺序的 Java Properties 类

    序 前几天,公司项目中有一个需求是读取配置文件的.并且最好可以保证载入到内存中的顺序可以和配置文件里的顺序一致,可是.假设使用 jdk 中提供的 Properties 类的话,读取配置文件后.载入到内 ...

  10. lightoj--1354-- IP Checking(水题)

    IP Checking Time Limit: 2000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu Submit Sta ...