分页,是在业务中经常要用到,为了节省用户流量和提升用户体验

讲一下思路:

首先是定义页号currentPage 和 页大小pagesize,用一个数组保存总数据;

用一个计算属性page_arrs,作用是 让页面展示的是我们所需要的页面

而我们在page_arrs中要分割原数组,用一个slice()方法进行分割;

在控件button上绑定点击方法,对页号currentPage进行修改,从而修改整个页面展示

具体看以下代码

代码:

data () {
return {
arrs : [
{name:'Otto',id:1},
{name:'Jacob',id:2},
{name:'Larry',id:3},
{name:'Tim',id:4},
{name:'Tom',id:5},
{name:'Jack',id:6},
{name:'Otto',id:1},
{name:'Jacob',id:2},
{name:'Larry',id:3},
{name:'Tim',id:4},
{name:'Tom',id:5},
{name:'Jack',id:6},
{name:'Otto',id:1},
{name:'Jacob',id:2},
{name:'Larry',id:3},
{name:'Tim',id:4},
{name:'Tom',id:5},
{name:'Jack',id:6}
],
currentPage : 1,//当前页号
pagesize :10 //每页大小
}
}
<table class="table table-hover ">
<thead>
<tr>
  <th class="number">序号</th>
  <th >题目</th>
   <th class="del">删除</th>
</tr>
</thead>
<tbody>
<tr class="tr" v-for="(item,index) in page_arrs" :key="index">
<th>{{index+1}}</th>
<td>{{item.name}}</td>
<td><a href="">删除</a></td>
</tr>
</tbody>
</table>
<div class="page">
<button class="btn btn-default" type="submit" @click="primaryPage">首页</button>
<button class="btn btn-default" type="submit" @click="prePage">上页</button>
<button class="btn btn-default" type="submit">{{current_page}}/{{Math.ceil(arrs.length/pagesize)}}</button>
<button class="btn btn-default" type="submit" @click="nextPage">下页</button>
<button class="btn btn-default" type="submit" @click="lastPage">尾页</button>
</div>
computed:{
page_arrs(){
let {currentPage,pagesize} = this
return this.arrs.slice((currentPage-1)*pagesize,currentPage*pagesize)
},
current_page(){
return this.currentPage
}
},
methods: {
primaryPage(){
this.currentPage = 1
},
prePage(){
if(this.currentPage == 1){
return
}
this.currentPage = this.currentPage - 1
},
nextPage(){
if(this.currentPage == Math.ceil(this.arrs.length/this.pagesize)){
return
}
this.currentPage = this.currentPage + 1
},
lastPage(){
this.currentPage = Math.ceil(this.arrs.length/this.pagesize)
}
},

最新文章

  1. Android开发3:Intent、Bundle的使用和ListView的应用 、RelativeLayout(相对布局)简述(简单通讯录的实现)
  2. JavaWeb Chapter 7 监听器
  3. java--遍历自定义数组
  4. Eclipse 常用最新插件.标记
  5. .Net程序员学习Linux(二)
  6. java 去除重复项
  7. HUST 1358 Uiwurerirexb jeqvad(模拟解密)
  8. 主流IOC框架测验(.NET)
  9. markdown常用语法教程
  10. jsonViewer json格式化工具
  11. 翻译:delete语句(已提交到MariaDB官方手册)
  12. Python_datetime模块
  13. Python 文件 fileno() 方法
  14. STM32的TAMPER-RTC管脚作为Tamper的使用[转]
  15. iOS多线程编程技术之NSThread、Cocoa NSOperation、GCD
  16. poj-1112 (二分图染色+dp分组)
  17. 利用python 下paramiko模块无密码登录
  18. [Cocos2dx] CCCamera照相机 详解
  19. shiro实战整合
  20. 团队计划会议(二)——WBS

热门文章

  1. 3000 - No Mycat Database selected
  2. Git基本指令
  3. springcloud-zuul初级篇
  4. 「JSOI2014」矩形并
  5. 「AHOI2014/JSOI2014」拼图
  6. SpringBoot简要介绍
  7. HDU1029 简单DP
  8. Elasticsearch 如何使用RESTful API
  9. Codeforces 601A:The Two Routes 宽搜最短路径
  10. Mongodb - 解决 ( aggregate聚合管道 ) $match 根据 id 匹配 返回 [ ] 的问题