关于node.js:多人后台博客管理DAY08

9次阅读

共计 2088 个字符,预计需要花费 6 分钟才能阅读完成。

另一种分页形式: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>
            <!-- / 分页 -->
正文完
 0