一, 前台 , views/main/index.html , 通过 get 传送给后台
思路 : 将栏目 ID 传递给后台, 后台根据 栏目的 ID 返回相应的数据
- {
- % if category == '' %
- }
- <li class="active"><a href="/"> 首页 <span class="sr-only">(current)</span></a></li>
- {
- % else %
- }
- <li><a href="/"> 首页 <span class="sr-only">(current)</span></a></li>
- {
- % endif %
- }
- {
- % for cate in categories %
- }
- <!-- cate 和 category 名称不可相等 -->
- {
- % if category == cate.id %
- }
- <li class="active"><a href="/?category={{cate.id}}">{
- {
- cate.category_name
- }
- }</a></li>
- {
- % else %
- }
- <li><a href="/?category={{cate.id}}">{
- {
- cate.category_name
- }
- }</a></li>
- {
- % endif %
- }
- {
- % endfor %
- }
二, 后台 , /router/main.JS
思路 : 增加 where 查询条件
- var data = {
- userInfo:req.userInfo,
- categories:[], // 所有分类的信息
- category :req.query.category || '',
- page : Number(req.query.page || 1),
- limit : 2,
- pages : 1,
- count: 0
- }
- var where = {}
- if(data.category){
- where.category = data.category
- }
- /** 读取内容的总记录数 */
- return Content.where(where).countDocuments()
- return Content.where(where).find().sort({_id:-1}).limit(data.limit).skip(skip).populate(['category','user'])
来源: http://www.bubuko.com/infodetail-3098652.html