pagination.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * Create with WebStorm
  3. * Author: Daxiu Huang
  4. * CreateTime: 2017/9/6 10:21
  5. */
  6. //分页组件
  7. var pageComponent = Vue.extend({
  8. template: '<nav v-if="pages>0" aria-label="Page navigation" class="text-center">\
  9. <ul class="pagination">\
  10. <li class="fl" style="line-height:30px;"><div v-if="total" class="c-909090 pr10 fl">共{{total}}条记录</div><div v-if="total" class="c-333333 pr10 fl">每页{{size}}条</div></li>\
  11. <li :class="{\'disabled\':curPage==1}">\
  12. <a href="javascript:;" @click="goPage(curPage==1?1:curPage-1)" aria-label="Previous">\
  13. <span aria-hidden="true">&laquo;</span>\
  14. </a>\
  15. </li>\
  16. <li v-for="page in showPageBtn" :class="{\'active\':page==curPage}">\
  17. <a href="javascript:;" v-if="page" @click="goPage(page)">{{page}}</a>\
  18. <a href="javascript:;" v-else>···</a>\
  19. </li>\
  20. <li :class="{\'disabled\':curPage==pages}">\
  21. <a href="javascript:;" @click="goPage(curPage==pages?pages:curPage+1)" aria-label="Next">\
  22. <span aria-hidden="true">&raquo;</span>\
  23. </a>\
  24. </li>\
  25. </ul>\
  26. </nav>',
  27. props: {
  28. pages: {
  29. type: Number,
  30. default: 1
  31. },
  32. current: {
  33. type: Number,
  34. default: 1
  35. },
  36. total: {
  37. type: Number,
  38. default: false
  39. },
  40. size: {
  41. type: Number,
  42. default: false
  43. }
  44. },
  45. data: function(){
  46. return{
  47. curPage:1
  48. }
  49. },
  50. computed: {
  51. showPageBtn: function() {
  52. var pageNum = this.pages;
  53. var index = this.curPage;
  54. var arr = [];
  55. if (pageNum <= 5) {
  56. for (var i = 1; i <= pageNum; i++) {
  57. arr.push(i)
  58. }
  59. return arr
  60. }
  61. if (index <= 2) return [1, 2, 3, 0, pageNum];
  62. if (index >= pageNum - 1) return [1, 0, pageNum - 2, pageNum - 1, pageNum];
  63. if (index === 3) return [1, 2, 3, 4, 0, pageNum];
  64. if (index === pageNum - 2) return [1, 0, pageNum - 3, pageNum - 2, pageNum - 1, pageNum];
  65. return [1, 0, index - 1, index, index + 1, 0, pageNum];
  66. }
  67. },
  68. methods: {
  69. goPage: function(page) {
  70. if (page != this.curPage) {
  71. console.log(page);
  72. this.curPage = page;
  73. this.$emit('navpage', this.curPage);
  74. }else{
  75. console.log('Already in the current page');
  76. }
  77. }
  78. }
  79. });
  80. Vue.component('navigation', pageComponent);