简体   繁体   English

如何添加分页来浏览博客文章页面(节点/快速)

[英]How to add pagination to navigate blog post pages (Node/Express)

I am building a blog site and trying to use pagination to navigate the pages of blog posts when there are more than 3 posts stored in the database. 我正在构建一个博客站点,并且当数据库中存储了3个以上的帖子时,尝试使用分页导航博客帖子的页面。 I'm trying to pass data from the front-end when clicking "newer" or "previous" buttons to the back-end to control the pages. 我试图在单击“较新”或“上一个”按钮到后端以控制页面时从前端传递数据。 How can I go about doing that? 我该怎么做呢?

I feel like I have to use the POST method somehow. 我觉得我必须以某种方式使用POST方法。

Here is my front-end code in EJS. 这是我在EJS中的前端代码。 These are the buttons that control the pages of posts. 这些是控制帖子页面的按钮。

  <% if(totalItems > 3){ %>
  <div class="page-buttons">
    <form action="/">
      <button type="submit" class="btn btn-outline-info newer-posts">Next</button>

    <button type="submit" class="btn btn-outline-info older-posts">Previous</button>
    </form>

  </div>
  <% } %>

Here is the route on the back-end. 这是后端的路由。

app.get("/", (req, res) => {
  let currentPage = req.query.page || 1;
  let perPage = 3;
  let totalItems;

  Post.find()
    .countDocuments()
    .then(count => {
      totalItems = count;
      Post.find()
        .skip((currentPage - 1) * perPage)
        .limit(perPage)
        .then(posts => {
            res.render("home", {
              posts: posts,
              totalItems: totalItems
            });
        });
    })
});

I expect clicking the buttons will increment or decrement the currentPage variable and render the proper pages of blog posts from the back-end. 我希望单击按钮将增加或减少currentPage变量,并从后端呈现博客文章的正确页面。 But instead, it does nothing. 但是,它什么也不做。

Any help would be appreciated. 任何帮助,将不胜感激。

Since you know the total amount of items (blog posts), you can do some simple math to know what the last page would be. 由于您知道项目的总数(博客文章),因此您可以做一些简单的数学运算来知道最后一页。

const lastPage = Math.ceil(totalItems / perPage);

Just like you're passing posts and totalItems , I would also pass currentPage and lastPage . 就像您传递poststotalItems ,我也将传递currentPagelastPage We could essentially do the above math in the template, but why would we? 我们基本上可以在模板中进行上述数学运算,但是为什么要这么做呢?

res.render("home", {
    posts,
    currentPage,
    totalItems,
    lastPage
});
// The above notation is shorthand for prop: value

Now, in the template, we can decide whether to show the previous and next page button based on the variables we passed. 现在,在模板中,我们可以根据传递的变量决定是否显示上一页和下一页按钮。

If currentPage is higher than 1, we must be on the second page or beyond, so show a link to the current page ?page=<%= currentPage - 1 %> . 如果currentPage大于1,那么我们必须位于第二页或之后的页面上,因此请显示指向当前页面的链接?page=<%= currentPage - 1 %>

If currentPage is lower than lastPage , we have not reached the end yet, so we show the link to the next page. 如果currentPage低于lastPage ,那么我们还没有结束,所以我们显示指向下一页的链接。 Which would be ?page=<%= currentPage + 1 %> 这将是?page=<%= currentPage + 1 %>

There is no need to use a form here. 此处无需使用表格。 Simple <a> tags will suffice. 简单的<a>标签就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM