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 ...
随机推荐
- nginx (待更新)
install apt install nginx 默认在 /etc/nginx 目录下 一个 master 以及多个 worker 3504 root 20 0 141M 7196 5552 S 0 ...
- HDU-6217 BBP Formula 脑洞
题目链接:https://cn.vjudge.net/problem/HDU-6217 题意 已知: \[ \pi = \sum_{k=0}^{\infty }\frac{1}{16^{k}}(\fr ...
- nyoj256-C小加之级数求和
C小加 之 级数求和 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 最近,C小加 又遇到难题了,正寻求你的帮助. 已知:Sn= 1+1/2+1/3+-+1/n. 显然对 ...
- 编写 Node.js Rest API 的 10 个最佳实践
Node.js 除了用来编写 WEB 应用之外,还可以用来编写 API 服务,我们在本文中会介绍编写 Node.js Rest API 的最佳实践,包括如何命名路由.进行认证和测试等话题,内容摘要如下 ...
- InfoSYS-20170114
1.描述Spring的事务机制 2.描述并发脏数据,如何避免 3.如何防止同一个请求重复提交(重复付款) 4.如何监控程序性能 5.CPU过高说明什么问题 通常是程序中有死循环, 参考 http:// ...
- mysql 插入更新在一条sql ON DUPLICATE KEY UPDATE
有时候需要进行数据操作的,如果有数据则更新数据, 没有数据则插入. 以往的做法是先查询,再根据查询结果进行判断,执行插入或更新操作 其实 有一种 ON DUPLICATE KEY UPDATE 语法, ...
- auto-boxing, uboxing,以及缓存问题
package chengbaoDemo; public class Test02 { public static void main(String[] args) { Integ ...
- CAD教程--嵌入表格
1.第一步,打开excel复制一下表格 2.第二步,打开CAD,选择编辑->选择性粘贴->autocad图元,左键点击一下图就行了,找找图,放大到适合的比例就行了.
- json_encode把中文字符的数组转为json格式
function ch_json_encode($data) { /** * 将中文编码 * @param array $data * @returnstring */ function ch_url ...
- 数据库连接池和connection的理解
数据库连接池Data Source Pool的理解 1.数据库连接池允许应用程序重复使用一个现有的数据库连接,而不是再重新建立一个连接,避免了每个方法里new connection的耗费资源和时间. ...