Nodejs分页 [英] Nodejs Pagination
问题描述
我想对Nodejs和Mongoose进行分页.我可以限制帖子,但无法处理前端到后端的连接.
I want to make a pagination with Nodejs and Mongoose. I can limit the posts but I couldn't handle the connection of front-end to back-end.
这是app.js:
app.get('/', function (req, res) {
var perPage = 2
var page = req.params.page || 1
Metin
.find({})
.skip((perPage * page) - perPage)
.limit(perPage)
.exec(function(err, metins) {
Metin.count().exec(function(err, count) {
if (err) return next(err)
res.render('index', {
metins: metins,
current: page,
pages: Math.ceil(count / perPage)
})
})
})
});
这是我的index.pug
extends layout
block content
body
br
br
br
.container
ul.list-group
each metin, i in metins
li.list-group-item
a(href="/metin/" + metin._id)= metin.baslik
我想添加可以传递到相关分页网页的按钮.你能帮我吗?
I want to add buttons that can pass to related paginated webpage. Can you please help me?
此行下方的内容可以解决吗?
Something like below this line can be solution ?
a(href='?page=' + pages)= length.pages.
推荐答案
您可以使用 pages
变量,从而为您的每个页面添加一个数字.并且根据current
变量有条件地显示/隐藏上一个/下一个链接.如果您没有大量的页面,这应该可以工作:
You can use while
iterator to loop over the pages
variable to make a number for each of your pages. And conditionally show/hide Previous/Next links based on current
variable. This should work if you don't have a large amount of pages:
ul
if current > 1
li
a(href=`?page=${parseInt(current) - 1}`) Previous
- var i = 1
while i <= pages
li
a(href=`?page=${i}`)= i++
if current < pages
li
a(href=`?page=${parseInt(current) + 1}`) Next
此外,请参见 查看全文