<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 21:14:28 浏览: 86
在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方法更新当前页数据。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>图书商城-图书列表</title> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <script src="https://cdn.bootcss.com/vue/2.5.16/vue.min.js"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> </head> <body> 0">
{{bk.bookName}} ¥{{bk.bookPrice}} <iframe src="footer.html" frameborder="0" scrolling="no" width="100%" height="245px"></iframe> <script src="/BookShopSystem_war/layui/layui.js"></script>这是完整的html代码,请结合你给出的分页功能整合出完整的代码
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)