【问题标题】:Pagination in mongoose and handlebars using node js使用节点 js 在猫鼬和车把中进行分页
【发布时间】:2020-05-19 13:11:44
【问题描述】:

我不知道如何在猫鼬中对我的帖子进行分页以及如何在前端实现它。我正在使用车把模板。我的模型名称是 Post。我需要你的帮助

【问题讨论】:

  • 好吧,我等一下
  • 你已经尝试了什么?

标签: javascript node.js mongoose


【解决方案1】:

好的,首先我们来看看我们的 app.js。在 mongoose 文档中,您可以在互联网上找到这些 .limit() 和其他值。谷歌一下就行了。

app.get('/', function (req, res) {
  var perPage = 5
  var page = req.query.page

  Post
  .find({})
  .skip((perPage * page) - perPage)
  .limit(perPage)
  .exec(function(err, posts) {
      Post.count().exec(function(err, count) {
          if (err) return next(err)
          res.render('index', {
              current: page,
              pages: Math.ceil(count / perPage),
              posts: posts,
          })
      })
  })
});

index.pug:

extends layout

    block content
      body
        br 
        br
        br
        .container
          ul.list-group
            each post, i in posts
              li.list-group-item
                a(href="/posts/" + posts._id)= posts.title
    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

参考文献

https://www.makeschool.com/academy/track/standalone/pete-s-pet-emporium---advanced-web-recipes/pagination

Nodejs Pagination

https://evdokimovm.github.io/javascript/nodejs/mongodb/pagination/expressjs/ejs/bootstrap/2017/08/20/create-pagination-with-nodejs-mongodb-express-and-ejs-step-by-step-from-scratch.html

https://mongoosejs.com/docs/2.7.x/docs/finding-documents.html

我们所做的是:首先我们设置perpage和page。每页意味着将显示在您的一页中的帖子。页面是你知道的只是普通的网站页面。

然后我们找到()。发布并限制它们,通过跳过我们跳过旧的**(我不确定)**。

之后我们设置当前页面和页面。在 pug(index) 中,我们使用 pug 和 javascript 设置它们。

【讨论】:

  • 但是我使用车把作为我的模板
  • 主要思想很重要。你可以转换它
  • 您只是想获得解决方案。但是你没有得到信息。
  • 我也不知道车把模板
  • 我发现很难将当前页面和上一页实现到车把
猜你喜欢
  • 2021-06-24
  • 2017-12-11
  • 2012-09-14
  • 1970-01-01
  • 2020-06-03
  • 2022-01-26
  • 2015-11-19
  • 2018-12-29
  • 1970-01-01
相关资源
最近更新 更多