1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
| <script> import { range } from "lodash-es";
export default { name: "Book", data() { return { msg: "hello vue!", total: null, // 总数据数 totalPageNum: 1, //总页数 pageSize: 5, // 每页显示数量 currentPageNum: 1, // 当前页数,默认当前显示第一页 firstValue: "", // 分页器用到的参数,表示点击"...."会跳转到哪一页 secendValue: "", // 分页器用到的参数,表示点击"..."会跳转到哪一页 // 制造一些假数据,前端来拿测试用 books: [ { id: 1, name: "1984", author: "乔治奥威尔", pubTime: "2001-01-05", }, { id: 2, name: "计算机零基础实战", author: "张三", pubTime: "2022-04-25", }, ], }; }, methods: { setTotalPageNum() { this.totalPageNum = Math.ceil(this.total / this.pageSize) || 1; console.log("更改this.totalPageNum:" + this.totalPageNum); }, // 通过点击页码进行修改currentPageNum的值 jumpToPage(i) { // currentPageNum变更,请求数据 if (i == "...") { i = this.secendValue; } if (i == "....") { i = this.firstValue; } this.currentPageNum = i; this.getDataByPage(this.currentPageNum); console.log(this.currentPageNum); }, getDataByPage(page) { console.log("传进来的page是:" + page); const _this = this; const axios = this.axios; axios .get( "http://localhost:8081/book/findAll/" + page + "/" + _this.pageSize // "http://localhost:8081/book/findAll/1/5" ) .then(function (response) { _this.books[0].author = _this.books[0].author + page; _this.books[0].name = _this.books[0].name + page; console.log("_this.books[0].author:" + _this.books[0].author); }) .catch(function (error) { console.log(error); }); }, /** * 正常返回:输入一个1到totalPageNum之间的值 * 规整化: 输入小于1的值,则返回1 * 输入大于totalPageNum则返回totalPageNum * @param {number} value 要规整化的值 */ numericalRegularization(value) { if (Number.isFinite(value)) { if (value < 1) return 1; if (value > this.totalPageNum) return this.totalPageNum; return value; } return null; }, pageItems(totalPageNum, currentPageNum) { const pageItems = this.pageItemsInternal(totalPageNum, currentPageNum); console.log("结果的pageItems是:" + pageItems); return pageItems; },
/** * 返回分页器中间的页数序列,例如: 1 2 3 ... 20 * * @param {number} lastPage 最大页数 * @param {number} page 当前页数 */ pageItemsInternal(lastPage, page) { /** * make all page items. * use range method from lodash. */ const allPageItems = range(1, lastPage + 1);
/** * range item number. */ const pageItemRange = 2;
/** * page items after current item. */ const pageItemAfter = allPageItems.slice(page, page + pageItemRange); console.log("after: ", pageItemAfter);
const tempAfter = pageItemAfter.at(pageItemAfter.length - 1) + 1; this.secendValue = this.numericalRegularization(tempAfter);
/** * page items before current item. */ const pageItemBefore = allPageItems.slice( page - 1 - lastPage - pageItemRange, page - 1 ); console.log("before: ", pageItemBefore);
const tempBefore = pageItemBefore.at(0) - 1; this.firstValue = this.numericalRegularization(tempBefore);
/** * base page items */ let pageItems = [...pageItemBefore, page, ...pageItemAfter];
console.log("pageItems: ", pageItems);
/** * first and last item */ let firstItem = [1]; let lastItem = [lastPage];
if (pageItemRange + 2 < page) { firstItem = [...firstItem, "...."]; }
if (lastPage - page - 1 > pageItemRange) { lastItem = ["...", ...lastItem]; }
if (pageItemRange + 1 < page) { pageItems = [...firstItem, ...pageItems]; }
if (lastPage - page > pageItemRange) { pageItems = [...pageItems, ...lastItem]; }
return pageItems; }, // 下一页 nextPage() { console.log("未判断前" + this.currentPageNum); if (this.currentPageNum === this.totalPageNum) return; // currentPageNum变更,请求数据 ++this.currentPageNum; this.getDataByPage(this.currentPageNum); console.log(this.currentPageNum); }, // 上一页 prePage() { console.log("未判断前" + this.currentPageNum); if (this.currentPageNum === 0) return; // currentPageNum变更,请求数据 --this.currentPageNum; this.getDataByPage(this.currentPageNum); console.log(this.currentPageNum); }, }, watch: { secendValue(newValue, oldValue) { console.log("this.secendValue is: " + newValue); //console.log(this.secendValue); }, firstValue(newValue, oldValue) { console.log("this.firstValue is: " + newValue); //console.log(this.firstValue); }, }, computed: {}, mounted() {}, created() { const _this = this; const axios = this.axios; axios .get( "http://localhost:8081/book/findAll/" + _this.currentPageNum + "/" + _this.pageSize ) .then(function (response) { // 数据总数 _this.total = response.data.total; console.log("response.data.total:" + response.data.total); console.log("_this.total:" + _this.total); // 设置总页数 _this.setTotalPageNum(); // 更改数据 _this.totalPageNum = Math.ceil(_this.total / _this.pageSize) || 1; console.log("_this.books[0].author:" + _this.books[0].author); }) .catch(function (error) { console.log(error); }); }, }; </script>
<template> <div class="greetings"> <table> <tr> <th>aaaa</th> <th>aaaa</th> <th>aaaa</th> </tr>
<tr v-for="item in books" :key="item.id"> <td>{{ item.id }}</td> <td>{{ item.name }}</td> <td>{{ item.author }}</td> <td>{{ item.pubTime }}</td> </tr> </table>
<input type="button" value="上一页" @click="prePage" /> <span v-for="item in pageItems(totalPageNum, currentPageNum)" @click="jumpToPage(item)" :style="{ cursor: 'pointer', margin: '10px' }" > {{ item }} </span> <input type="button" value="下一页" @click="nextPage" /> </div> </template>
<style scoped> h1 { font-weight: 500; font-size: 2.6rem; top: -10px; }
h3 { font-size: 1.2rem; }
.greetings h1, .greetings h3 { text-align: center; }
@media (min-width: 1024px) { .greetings h1, .greetings h3 { text-align: left; } } </style>
|