likes
comments
collection
share

多人后台博客管理DAY08

作者站长头像
站长
· 阅读数 26

另一种分页方式: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

    • 多人后台博客管理DAY08
    • 多人后台博客管理DAY08
  • 接受客户端传递的页码
// 将文章集合的构造函数导入到当前文件中
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>
            <!-- /分页 -->