egg(75)–egg之前端jqPaginator分页插件

33次阅读

共计 836 个字符,预计需要花费 3 分钟才能阅读完成。

jqPaginator.js

controller
app/controller/admin/goods.js

page 当前页
pageSize 每页多少条数据
totalPages 总页数
totalPages:Math.ceil(totalNum/pageSize) 向上取整, 如 3.2 => 4

async index() {
var page=this.ctx.request.query.page || 1;
var pageSize=2;
// 获取当前数据表的总数量
var totalNum=await this.ctx.model.Goods.find({}).count();
var goodsResult=await this.ctx.model.Goods.find({}).skip((page-1)*pageSize).limit(pageSize);
await this.ctx.render(‘admin/goods/index’,{
list:goodsResult,
totalPages:Math.ceil(totalNum/pageSize),
page:page
});
}
view
app/view/admin/goods/index.html
<script src=”/public/admin/js/jqPaginator.js”></script>

<div id=”page” class=”pagination”></div>

<script>
$(‘#page’).jqPaginator({
totalPages: <%=totalPages%>,
visiblePages: 8,
currentPage: <%=page%>,
onPageChange: function (num, type) {
console.log(‘ 当前第 ’ + num + ‘ 页 ’,type);
if(type==’change’){
location.href=”/admin/goods?page=”+num;
}
}
});
</script>
效果

正文完
 0