vue实现分页器(仿element)
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)的更多相关文章
- [Vue warn]: Cannot find element: #main
使用vue框架的时候,如果页面提示如下错误,则说明new Vue的时候没有传入 el 的值: [Vue warn]: Cannot find element: #main 我们传入el 既可以,如: ...
- vue按需引入element或mint
vue按需引入element或mint需要添加 babel-preset-es2015 和babel-plugin-component
- 使用Vue.js制作仿Metronic高级表格(一)静态设计
Metronic高级表格是Metonic框架中自行实现的表格,其底层是Datatables.本教程将主要使用Vue实现交互部分,使用Bootstrap做样式库.jQuery做部分用户交互(弹窗). 使 ...
- Vue报错 [Vue warn]: Cannot find element
在前端开发全面进入前端的时代 作为一个合格的前端开发工作者 框架是不可或缺的Vue React Anguar 作为前端小白,追随大佬的脚步来到来到博客园,更新现在正在学习的Vue 注 : 相信学习Vu ...
- 用vue写一个仿简书的轮播图
原文地址:用vue写一个仿简书的轮播图 先展示最终效果: Vue的理念是以数据驱动视图,所以拒绝通过改变元素的margin-top来实现滚动效果.写好css样式,只需改变每张图片的class即可实现轮 ...
- 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 ...
- Vue. 之 npm安装Element
Vue. 之 npm安装Element 前提: 相关运行环境以搭建完成,例如:Node.Npm等. 假如我的项目目录如下: D:\DISK WORKSPACE\VSCODE\CDS\cds-ap ...
- vue components registration & vue error & Unknown custom element
vue components registration & vue error & Unknown custom element vue.esm.js:629 [Vue warn]: ...
- [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 ...
随机推荐
- Iterator和for...of
Iterator和for...of 什么是Iterator ES6中的Map 和 Set ,再加上之前的数组和对象,这样就有了四种数据集合,用户可以组合使用它们,定义自己的数据结构.这时,我们就需要一 ...
- 3.2、使用Flask-Bootstrap集成Twitter Bootstrap
Bootstrap(http://getbootstrap.com/)是 Twitter 开发的一个开源框架,它提供的用户界面组件可用于创建整洁且具有吸引力的网页,而且这些网页还能兼容所有现代 Web ...
- 2、Ansible配置文件详解
0.配置文件 两个核心文件:ansible.cfg和hosts文件,默认都存放在/etc/ansible目录下. ansible.cfg:主要设置一些ansible初始化的信息,比如日志存放路径.模块 ...
- Linux磁盘分区--GPT分区
MBR分区表有一定的局限性,最大支持2.1tb硬盘,单块硬盘最多4个主分区. 这里就要引入GPT分区表,可以支持最大18EB的卷,最多支持128个主分区,所以如果使用大于2tb的卷,就必须使用GTP分 ...
- 一个简单的 PC端与移动端的适配(通过UA)
只需在header引用个js文件, 原理就是判断UA里面的标识. 加下面代码添加到js文件,在头文件引用即可 var Pc_url = 'http://www.baidu.com'; //PC端网址 ...
- 百度url 参数详解全
百度url解析Joe.Smith整理大全 百度url解析Joe.Smith整理大全...1 本文链接:http://blog.csdn.net/qq_26816591/article/details/ ...
- java使用Thumbnailator处理图片
Thumbnailator是一款不可多得的处理图片的第三方工具包,它写法简单到让人无法相信,Java本身也有处理图片压缩的方法,但是代码冗长到让人痛不欲生,在篇末会给出Java本身的实现方式,做下对比 ...
- six.moves的用法
six是用来兼容python 2 和 3的,我猜名字就是用的2和3的最小公倍数. six.moves 是用来处理那些在2 和 3里面函数的位置有变化的,直接用six.moves就可以屏蔽掉这些变化 P ...
- HttpClient 4.4 请求
4.2 版本 /** * url 请求 paramUrl * * @time 2015年11月10日下午4:40:22 * @packageName com.rom.utils * @param ur ...
- POJ - 3257 Cow Roller Coaster (背包)
题目大意:要用N种材料建一条长为L的路,如今给出每种材料的长度w.起始地点x.发费c和耐久度f 问:在预算为B的情况下,建好这条路的最大耐久度是多少 解题思路:背包问题 dp[i][j]表示起始地点为 ...