封装分页 不能直接到达很大页号的最后一页(比如1万条数据)

  因为后端不能做到直接到很大数据的最后几页或者最后一页,element的都是自带最后一页的,所以自己封装了一个

  1 <template>
  2   <div class="jspagination flex">
  3     <p class="total">共&nbsp;{{ total }}&nbsp;条</p>
  4     <el-select v-model="value" @change="valueChange">
  5       <el-option
  6         v-for="item in options"
  7         :key="item.value"
  8         :label="item.label"
  9         :value="item.value"
 10       >
 11       </el-option>
 12     </el-select>
 13     <div class="m-l20 total cursor" @click="goOne" v-if="currentTotal > 9">
 14       首页
 15     </div>
 16     <div class="flex m-l10">
 17       <span
 18         :class="
 19           currentPage === 1 || currentbox.length === 1
 20             ? 'cur_disable'
 21             : 'cursor'
 22         "
 23         @click="prev"
 24       >
 25         <i class="el-icon-arrow-left bold"></i
 26       ></span>
 27       <div class="numbox">
 28         <div class="flex">
 29           <p
 30             v-for="(item, index) in currentbox"
 31             :key="index"
 32             class="num cursor"
 33             :class="currentPage == item ? 'active' : ''"
 34             @click="number(item)"
 35           >
 36             {{ item }}
 37           </p>
 38         </div>
 39       </div>
 40       <span
 41         class="cursor"
 42         @click="next"
 43         :class="
 44           total === 0 ||
 45           currentbox.length === 1 ||
 46           currentPage === currentbox.length
 47             ? 'cur_disable'
 48             : 'cursor'
 49         "
 50       >
 51         <i class="el-icon-arrow-right bold"></i
 52       ></span>
 53     </div>
 54     <p class="m-l10 dqdjy">
 55       当前第&nbsp;&nbsp;<i style="font-size:20px;color: #000">{{
 56         currentPage
 57       }}</i
 58       >&nbsp;&nbsp;页&nbsp;&nbsp;&nbsp;
 59     </p>
 60   </div>
 61 </template>
 62 
 63 <script>
 64 import { forEach } from "jszip";
 65 export default {
 66   name: "Jspagination",
 67   components: {},
 68   props: ["total"],
 69   data() {
 70     return {
 71       options: [
 72         {
 73           value: 10,
 74           label: "10条/页"
 75         },
 76         {
 77           value: 20,
 78           label: "20条/页"
 79         },
 80         {
 81           value: 30,
 82           label: "30条/页"
 83         }
 84       ],
 85       value: 10,
 86       currentPage: 1,
 87       currentbox: [],
 88       totalbox: [],
 89       currentTotal: 0,
 90       num: 0
 91     };
 92   },
 93   computed: {},
 94   watch: {
 95     total() {
 96       if (this.total) {
 97         // 向上取整
 98         this.currentTotal = Math.ceil(this.total / this.value);
 99         this.totalbox = [];
100         this.currentbox = [];
101         this.currentPage = 1;
102         if (this.currentTotal > 9) {
103           for (let i = 1; i < this.currentTotal + 1; i++) {
104             this.totalbox.push(i);
105           }
106           this.currentbox = this.totalbox.slice(0, 9);
107         } else if (this.currentTotal < 9) {
108           for (let i = 1; i < this.currentTotal + 1; i++) {
109             this.currentbox.push(i);
110             this.totalbox.push(i);
111           }
112         }
113       } else {
114         this.totalbox = [];
115         this.currentbox = [];
116         this.currentPage = 1;
117         this.currentbox.push(1);
118         this.totalbox.push(1);
119       }
120     }
121   },
122   created() {
123     if (this.total) {
124       // 向上取整
125       this.currentTotal = Math.ceil(this.total / this.value);
126       this.totalbox = [];
127       this.currentbox = [];
128       this.currentPage = 1;
129       if (this.currentTotal > 9) {
130         for (let i = 1; i < this.currentTotal + 1; i++) {
131           this.totalbox.push(i);
132         }
133         this.currentbox = this.totalbox.slice(0, 9);
134       } else if (this.currentTotal < 9) {
135         for (let i = 1; i < this.currentTotal + 1; i++) {
136           this.currentbox.push(i);
137           this.totalbox.push(i);
138         }
139       }
140     } else {
141       this.totalbox = [];
142       this.currentbox = [];
143       this.currentPage = 1;
144       this.currentbox.push(1);
145       this.totalbox.push(1);
146     }
147   },
148   mounted() {},
149   beforeDestroy() {},
150   methods: {
151     checkData() {
152       // 如果选中的页号大于5,总页数大于9
153       if (this.currentPage > 5 && this.totalbox.length > 9) {
154         this.currentbox = this.totalbox.slice(
155           this.currentPage - 5,
156           this.currentPage + 4
157         );
158       } else if (this.currentPage > 5 && this.totalbox.length <= 9) {
159         // 如果选中的页号大于5,总页数小于等于9
160         this.currentbox = this.totalbox.slice(0, this.totalbox.length);
161       } else if (this.currentPage <= 5 && this.totalbox.length > 9) {
162         // 如果选中的页号下于5,总页数大于等于9
163         this.currentbox = this.totalbox.slice(0, 9);
164       } else if (this.currentPage <= 5 && this.totalbox.length <= 9) {
165         // 如果选中的页号大于5,总页数小于等于9
166         this.currentbox = this.totalbox.slice(0, this.totalbox.length);
167       }
168       this.$emit("handleCurrentChange", this.currentPage);
169     },
170     goOne() {
171       this.number(1);
172     },
173     number(item) {
174       this.currentPage = item;
175       this.checkData();
176     },
177     // 上一页
178     prev() {
179       if (this.currentPage > 1) {
180         this.currentPage -= 1;
181       }
182       if (this.currentPage === 1) {
183         return;
184       }
185       this.checkData();
186     },
187     // 下一页
188     next() {
189       if (this.currentPage < this.currentTotal) {
190         this.currentPage += 1;
191       }
192       if (this.currentPage === this.totalbox.length) {
193         return;
194       }
195       this.checkData();
196     },
197     valueChange() {
198       this.currentTotal = Math.ceil(this.total / this.value);
199       this.totalbox = [];
200       this.currentbox = [];
201       if (this.currentTotal > 9) {
202         for (let i = 1; i < this.currentTotal + 1; i++) {
203           this.totalbox.push(i);
204         }
205         this.currentbox = this.totalbox.slice(0, 9);
206       } else {
207         for (let i = 1; i < this.currentTotal + 1; i++) {
208           this.currentbox.push(i);
209           this.totalbox.push(i);
210         }
211       }
212       this.currentPage = 1;
213       this.$emit("handleSizeChange", this.value);
214     }
215   }
216 };
217 </script>
218 
219 <style scoped lang="scss">
220 .cur_disable {
221   color: #c0c4cc;
222   cursor: not-allowed;
223 }
224 .bold {
225   font-weight: bold;
226 }
227 .active {
228   color: #409eff;
229 }
230 .jspagination {
231   line-height: 32px;
232   .total {
233     margin-right: 10px;
234     font-weight: 400;
235     color: #606266;
236   }
237   .numbox {
238     margin: 0px 15px;
239     max-width: 274px;
240     overflow: hidden;
241   }
242   .num {
243     display: inline-block;
244     margin: 0px 9px;
245     font-weight: bold;
246   }
247 
248   .dqdjy {
249     line-height: 25px;
250     margin-right: 10px;
251     font-weight: 400;
252     color: #606266;
253   }
254   /deep/ .el-select {
255     width: 106px;
256   }
257 }
258 </style>

 

 

 

posted @ 2021-11-08 14:56  想吃水煮麻辣鱼  阅读(288)  评论(0)    收藏  举报