pagination.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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="{\'disabled\':curPage==1}">\
  11. <a href="javascript:;" @click="goPage(curPage==1?1:curPage-1)" aria-label="Previous">\
  12. <span aria-hidden="true">&laquo;</span>\
  13. </a>\
  14. </li>\
  15. <li v-for="page in showPageBtn" :class="{\'active\':page==curPage}">\
  16. <a href="javascript:;" v-if="page" @click="goPage(page)">{{page}}</a>\
  17. <a href="javascript:;" v-else>···</a>\
  18. </li>\
  19. <li :class="{\'disabled\':curPage==pages}">\
  20. <a href="javascript:;" @click="goPage(curPage==pages?pages:curPage+1)" aria-label="Next">\
  21. <span aria-hidden="true">&raquo;</span>\
  22. </a>\
  23. </li>\
  24. </ul>\
  25. </nav>',
  26. props: {
  27. pages: {
  28. type: Number,
  29. default: 1
  30. },
  31. current: {
  32. type: Number,
  33. default: 1
  34. }
  35. },
  36. data(){
  37. return{
  38. curPage:1
  39. }
  40. },
  41. computed: {
  42. showPageBtn() {
  43. let pageNum = this.pages;
  44. let index = this.curPage;
  45. let arr = [];
  46. if (pageNum <= 5) {
  47. for (let i = 1; i <= pageNum; i++) {
  48. arr.push(i)
  49. }
  50. return arr
  51. }
  52. if (index <= 2) return [1, 2, 3, 0, pageNum];
  53. if (index >= pageNum - 1) return [1, 0, pageNum - 2, pageNum - 1, pageNum];
  54. if (index === 3) return [1, 2, 3, 4, 0, pageNum];
  55. if (index === pageNum - 2) return [1, 0, pageNum - 3, pageNum - 2, pageNum - 1, pageNum];
  56. return [1, 0, index - 1, index, index + 1, 0, pageNum];
  57. }
  58. },
  59. methods: {
  60. goPage(page) {
  61. if (page != this.curPage) {
  62. console.log(page);
  63. this.curPage = page;
  64. this.$emit('navpage', this.curPage);
  65. }else{
  66. console.log('Already in the current page');
  67. }
  68. }
  69. }
  70. });
  71. Vue.component('navigation', pageComponent);