另一种分页形式:mongoose-sex-page(第三方模块)

BLOG -- 源码目录  └── model -- 数据库操作  ├──  public -- 动态资源  └──  route -- 路由        └──  admin --博客治理                             └── article.js --文章列表页 └── views -- 模板      └── admin --博客治理页面art模板             └── article.art --文章列表模板 └── app.js -- 创立网站服务

article.js

  • 导入mongoose-sex-page(第三方模块)来实现分页 npm install mongoose-sex-page

  • 承受客户端传递的页码
// 将文章汇合的构造函数导入到以后文件中const { Article } = require('../../model/article');// 导入mongoose-sex-page模块const pagination = require('mongoose-sex-page');module.exports = async (req, res) => {    // 接管客户端传递过去的页码    const page = req.query.page;    // 标识 标识以后拜访的是文章治理页面    req.app.locals.currentLink = 'article';    // page 指定当前页    // suze 指定每页显示的数据条数    // display 指定客户端要显示的页码数量    // exec 向数据库中发送查问申请    // 查问所有文章数据    let articles = await pagination(Article).find().page(page).size(2).display(3).populate('author').exec();    // res.send(articles);    // 渲染文章列表页面模板    res.render('admin/article.art', {        articles: articles    });}

article.art

  • 因为pagination是对象,所以each应该改为.record
  • 对href也要改为?page的款式进行转换
  • 对于上一页和下一页也和之前的user相似的解决,不过用的是模板语法
{{extend './common/layout.art'}}{{block 'main'}}    {{include './common/header.art'}}    <!-- 主体内容 -->    <div class="content">        {{include './common/aside.art'}}        <div class="main">            <!-- 分类题目 -->            <div class="title">                <h4>文章</h4>                <span>找到1篇文章</span>                <a href="/admin/article-edit" class="btn btn-primary new">公布新文章</a>            </div>            <!-- /分类题目 -->            <!-- 内容列表 -->            <table class="table table-striped table-bordered table-hover custom-table">                <thead>                    <tr>                        <th>ID</th>                        <th>题目</th>                        <th>公布工夫</th>                        <th>作者</th>                        <th>操作</th>                    </tr>                </thead>                <tbody>                    {{each articles.records}}                    <tr>                        <td>{{@$value._id}}</td>                        <td>{{$value.title}}</td>                        <td>{{dateFormat($value.publishDate, 'yyyy-mm-dd')}}</td>                        <td>{{$value.author.username}}</td>                        <td>                            <a href="article-edit.html" class="glyphicon glyphicon-edit"></a>                            <i class="glyphicon glyphicon-remove" data-toggle="modal" data-target=".confirm-modal"></i>                        </td>                    </tr>                    {{/each}}                </tbody>            </table><!-- 分页 -->            <ul class="pagination">                {{if articles.page > 1}}                <li>                    <a href="/admin/article?page={{articles.page - 1}}">                    <span>&laquo;</span>                  </a>                </li>                {{/if}}                                {{each articles.display}}                <li><a href="/admin/article?page={{$value}}">{{$value}}</a></li>                {{/each}}                {{if articles.page < articles.pages}}                <li>                    <a href="/admin/article?page={{articles.page - 0 + 1}}">                    <span>&raquo;</span>                  </a>                </li>                {{/if}}            </ul>            <!-- /分页 -->