使用 knex js 创建分页的最佳方法是什么?我真的不知道一般如何进行分页。在我的代码下方:
router.get("/", (req, res) => {
db.select("*")
.from("site.site_product")
.offset(0)
.limit(10)
.then(data => {
res.render("inventory/site_product", { data: data });
});
});
任何帮助将不胜感激。谢谢
最佳答案
试试这个
router.get("/", (req, res) => {
var reqData = req.query;
var pagination = {};
var per_page = reqData.per_page || 10;
var page = reqData.current_page || 1;
if (page < 1) page = 1;
var offset = (page - 1) * per_page;
return Promise.all([
db.count('* as count').from("site.site_product").first(),
db.select("*").from("site.site_product").offset(offset).limit(per_page)
]).then(([total, rows]) => {
var count = total.count;
var rows = rows;
pagination.total = count;
pagination.per_page = per_page;
pagination.offset = offset;
pagination.to = offset + rows.length;
pagination.last_page = Math.ceil(count / per_page);
pagination.current_page = page;
pagination.from = offset;
pagination.data = rows;
res.render("inventory/site_product", {
data: pagination
});
});
});
关于node.js - 使用 KnexJS + PG 创建分页,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51732870/