<div class="layui-container"> <div class="hot-sell" v-if="bookAll.length > 0"> <div class="layui-row layui-col-space20"> <table> <tr> <td v-for="(bk,index) in bookAll" :key="index" class="layui-col-xs2" style="margin-left: 30px"> <a :href="'detail?id='+bk.bookId" class="text"> <div><img :src="'/BookShopSystem_war/' + bk.image1"></div> <p>{{bk.bookName}}</p> <p class="price">¥{{bk.bookPrice}}</p> </a> </td> </tr> </table> </div> </div> </div> <div id="houseList"></div> //列表页——分页 layui.use(['laypage'], function () { var laypage = layui.laypage; laypage.render({ elem: 'houseList' , count: 25 , limit: 5 , theme: '#daba91' , layout: ['page', 'next'] }); }); Vue.config.productionTip = false //阻止vue在启动时生成生产提示 new Vue({ el: "#app", data() { return { bookAll: [] } }, mounted() { var _this = this; axios({ method: 'get', url: 'http://localhost:8080/BookShopSystem_war/getBookAll' }).then(function (result) { var code = result.data.code; var data = result.data.data; if (code == 0) { _this.bookAll = data; } console.log(data) }) } })已省略部分代码,数据都可遍历出来,如何实现分页功能
时间: 2024-01-03 14:14:28 浏览: 130
在Vue中实现分页功能可以使用第三方库,比如element-ui和iview等,也可以使用layui等前端框架的自带分页组件。下面给出使用layui的示例代码:
HTML代码:
```
<div class="layui-container">
<div class="hot-sell" v-if="bookAll.length > 0">
<div class="layui-row layui-col-space20">
<table>
<tr>
<td v-for="(bk,index) in currentBookAll" :key="index" class="layui-col-xs2" style="margin-left: 30px">
<a :href="'detail?id='+bk.bookId" class="text">
<div><img :src="'/BookShopSystem_war/' + bk.image1"></div>
<p>{{bk.bookName}}</p>
<p class="price">¥{{bk.bookPrice}}</p>
</a>
</td>
</tr>
</table>
</div>
</div>
</div>
<div id="houseList"></div>
```
Vue代码:
```
new Vue({
el: "#app",
data() {
return {
bookAll: [],
currentBookAll: [],
page: {
count: 0,
limit: 5,
curr: 1
}
}
},
mounted() {
var _this = this;
axios({
method: 'get',
url: 'http://localhost:8080/BookShopSystem_war/getBookAll'
}).then(function (result) {
var code = result.data.code;
var data = result.data.data;
if (code == 0) {
_this.bookAll = data;
_this.page.count = Math.ceil(_this.bookAll.length / _this.page.limit);
_this.changePage(1);
}
})
},
methods: {
changePage(curr) {
var start = (curr - 1) * this.page.limit;
var end = curr * this.page.limit;
this.currentBookAll = this.bookAll.slice(start, end);
this.page.curr = curr;
layui.use(['laypage'], function () {
var laypage = layui.laypage;
laypage.render({
elem: 'houseList',
count: this.page.count,
limit: this.page.limit,
curr: this.page.curr,
theme: '#daba91',
layout: ['page', 'next'],
jump: function (obj, first) {
if (!first) {
_this.changePage(obj.curr);
}
}
});
});
}
}
})
```
上述代码中,通过计算总页数和当前页数据的起止索引,实现了分页功能。在changePage方法中,使用slice方法截取当前页的数据,并通过laypage组件渲染分页控件。在laypage的jump回调函数中,调用changePage方法更新当前页数据。
阅读全文