21 ~ express ~ 前台内容分类展示

一,前台 , 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'])

猜你喜欢

转载自www.cnblogs.com/500m/p/11061260.html
21
21)