基于vue2.0的一个分页组件
来源:互联网 发布:摄影网络教学 编辑:程序博客网 时间:2024/06/01 07:29
基于vue2.0的一个分页组件
分页组件在项目中经常要用到之前一直都是在网上找些jq的控件来用(逃..),最近几个项目用上vue了项目又刚好需要一个分页的功能。于是百度发现几篇文章介绍的实在方式有点复杂,
没耐心看自己动手造轮子写了一个,用vue的计算属性去实现真正的逻辑代码大约在20行左右相当好理解现实原理简单没什么好介绍的直接上代码。
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>vue-router</title> <script src="./vue.js" ></script> <style> body{ font-family:"Segoe UI"; } li{ list-style:none; } a{ text-decoration:none; } .pagination { position: relative; } .pagination li{ display: inline-block; margin:0 5px; } .pagination li a{ padding:.5rem 1rem; display:inline-block; border:1px solid #ddd; background:#fff; color:#0E90D2; } .pagination li a:hover{ background:#eee; } .pagination li.active a{ background:#0E90D2; color:#fff; } </style></head><body> <script type="text/x-template" id="page"> <ul class="pagination" > <li v-show="current != 1" @click="current-- && goto(current)" ><a href="#">上一页</a></li> <li v-for="index in pages" @click="goto(index)" :class="{'active':current == index}" :key="index"> <a href="#" >{{index}}</a> </li> <li v-show="allpage != current && allpage != 0 " @click="current++ && goto(current++)"><a href="#" >下一页</a></li> </ul> </script> <div id="app"> <page></page> </div><script> Vue.component("page",{ template:"#page", data:function(){ return{ current:1, showItem:5, allpage:13 } }, computed:{ pages:function(){ var pag = []; if( this.current < this.showItem ){ //如果当前的激活的项 小于要显示的条数 //总页数和要显示的条数那个大就显示多少条 var i = Math.min(this.showItem,this.allpage); while(i){ pag.unshift(i--); } }else{ //当前页数大于显示页数了 var middle = this.current - Math.floor(this.showItem / 2 ),//从哪里开始 i = this.showItem; if( middle > (this.allpage - this.showItem) ){ middle = (this.allpage - this.showItem) + 1 } while(i--){ pag.push( middle++ ); } } return pag } }, methods:{ goto:function(index){ if(index == this.current) return; this.current = index; //这里可以发送ajax请求 } } })var vm = new Vue({ el:'#app',})</script></body></html>
效果图
在线demo地址
github地址
文章如有误之处烦请指正谢谢
by 冷无缺
标
阅读全文
0 0
- 基于vue2.0的一个分页组件
- 基于Vue2的分页按钮组件
- 基于vue2的table分页组件
- 基于vue2.0+vuex的日期选择组件
- 基于vue2.0+vuex的日期选择组件
- Vue2.0初学之分页组件
- vue2.0初始化一个头部组件
- Vue2.0+ElementUi封装table组件实现分页功能
- 基于SmartGwt的分页组件
- 基于velocity的分页组件
- vue2.0实现父子通信(基于之前我写的复选框组件)
- 基于 Vue2.0 的移动端 / PC 端验证码输入组件.
- Vue.js 学习10 Element基于Vue2.0的组件库
- Vue.js 学习11 Mint UI基于Vue2.0的组件库
- 基于 vue2 的响应式基础组件(滚动条)
- vue2.0 组件之间的数据传递
- vue2.0 组件之间的数据传递
- 不容错过的Vue2.0组件开发
- 次小生成树
- debug有感
- Python网络爬虫1
- vue.js 2.0实现的简单分页
- React实现按钮控制倒计时
- 基于vue2.0的一个分页组件
- dns-prefetch对网站速度能提升有多少?详解dns-prefetch
- Hibernate的入门
- Practical Microservices-Packt Publishing(2017) 读书笔记
- Python标准异常总结
- 基于vuejs菜单实现
- Android中的数据库应用小结
- Java Class.forName()
- Java 进程的退出机制与Shutdown hook