demo.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>vue-列表分页</title>
  8. <script src="vue.js"></script>
  9. <script src="../jquery/jquery.min.js"></script>
  10. <style>
  11. body {
  12. font-family: "Segoe UI";
  13. }
  14. li {
  15. list-style: none;
  16. }
  17. a {
  18. text-decoration: none;
  19. }
  20. .pagination {
  21. position: relative;
  22. }
  23. .pagination li {
  24. display: inline-block;
  25. margin: 0 5px;
  26. }
  27. .pagination li a {
  28. padding: .5rem 1rem;
  29. display: inline-block;
  30. border: 1px solid #ddd;
  31. background: #fff;
  32. color: #0E90D2;
  33. }
  34. .pagination li a:hover {
  35. background: #eee;
  36. }
  37. .pagination li.active a {
  38. background: #0E90D2;
  39. color: #fff;
  40. }
  41. table,
  42. td {
  43. border: 1px solid #cccccc;
  44. border-collapse: collapse;
  45. }
  46. table {
  47. width: 1090px;
  48. margin: 20px auto;
  49. }
  50. tr {
  51. line-height: 30px;
  52. }
  53. td {
  54. text-align: center;
  55. }
  56. </style>
  57. </head>
  58. <body>
  59. <script type="text/x-template" id="page">
  60. <ul class="pagination">
  61. <li v-show="current != 1" @click="current-- && goto(current--)">
  62. <a href="#">上一页</a>
  63. </li>
  64. <li v-for="index in pages" @click="goto(index)" :class="{'active':current == index}" :key="index">
  65. <a href="#">{{index}}</a>
  66. </li>
  67. <li v-show="allpage != current && allpage != 0 " @click="current++ && goto(current++)">
  68. <a href="#">下一页</a>
  69. </li>
  70. </ul>
  71. </script>
  72. <div id="app">
  73. <table border="1">
  74. <tr>
  75. <th>ID</th>
  76. <th>书名</th>
  77. <th>作者</th>
  78. <th>价格</th>
  79. </tr>
  80. <tr v-for="book in books">
  81. <td>{{book.id}}</td>
  82. <td>{{book.name}}</td>
  83. <td>{{book.author}}</td>
  84. <td>{{book.price}}</td>
  85. </tr>
  86. </table>
  87. <page></page>
  88. </div>
  89. <script>
  90. Vue.component("page", {
  91. template: "#page",
  92. data: function () {
  93. return {
  94. current: 1,
  95. showItem: 5,
  96. allpage: 20
  97. }
  98. },
  99. computed: {
  100. pages: function () {
  101. var pag = [];
  102. if (this.current < this.showItem) { //如果当前的激活的项 小于要显示的条数
  103. //总页数和要显示的条数那个大就显示多少条
  104. var i = Math.min(this.showItem, this.allpage);
  105. while (i) {
  106. pag.unshift(i--);
  107. }
  108. } else { //当前页数大于显示页数了
  109. var middle = this.current - Math.floor(this.showItem / 2), //从哪里开始
  110. i = this.showItem;
  111. if (middle > (this.allpage - this.showItem)) {
  112. middle = (this.allpage - this.showItem) + 1
  113. }
  114. while (i--) {
  115. pag.push(middle++);
  116. }
  117. }
  118. return pag
  119. }
  120. },
  121. methods: {
  122. goto: function (index) {
  123. if (index == this.current) return;
  124. this.current = index;
  125. //这里可以发送ajax请求
  126. ajaxGetBookList(index);
  127. }
  128. },
  129. mounted: function () {
  130. var index = 1;
  131. ajaxGetBookList(index);
  132. }
  133. })
  134. function ajaxGetBookList(pageIndex) {
  135. setTimeout(function () {
  136. var books = [ ];
  137. for (var i = 0; i < 15; i++) {
  138. books.push({ id: 'id' + i + '_' + pageIndex, name: 'name' + i + '_' + pageIndex, author: 'author' + i + '_' + pageIndex, price: 'price' + i +'_' + pageIndex });
  139. }
  140. vm.books = books;
  141. }, 50);
  142. }
  143. var vm = new Vue({
  144. el: '#app',
  145. data: {
  146. books: ''
  147. }
  148. })
  149. </script>
  150. </body>
  151. </html>