First, the front display /views/admin/category.html
{% extends 'layout.html' %}
{% block main %}
<ol class="breadcrumb">
<li><a href="/admin/category">分类首页</a></li>
<Li> <span> Category List </ span> </ li>
</ol>
Category List <h3> </ h3>
<table class="table table-bordered">
<tr>
<th>ID</th>
<Th> category name </ th>
<Th> operation </ th>
</tr>
<! -. Id is an object cycle of the output array, to be converted into a string ->
{% for category in categories %}
<tr>
<td> {{category._id.toString()}}</td>
<td> {{category.category_name}}</td>
<td>
<! - [Error cases: forget to add routing] <a href = "/ category / edit id = {{category._id.toString ()}}?"> Modify </a> | ->
<a href="/admin/category/edit?id={{category._id.toString()}}">修改</a> |
<a href="/admin/category/delete?id={{category._id.toString()}}">删除</a>
</td>
{% endfor %}
</tr>
</table>
<div class="btn-group" role="group" aria-label="...">
<a href="/admin/category?page={{page-1}}" class="btn btn-default">上一页</a>
<a href="/admin/category?page={{page+1}}" class="btn btn-default">下一页</a>
<Li> a total of {{count}} of data </ li>
<Li> Showing {{limit}} of data </ li>
<Li> a total of {{pages}} p </ li>
<Li> currently in the page {{page}} </ li>
</div>
{% endblock %}
Second, the background /router/admin.js
/**
* Category management, displays information about all classifications
*/
router.get('/category',(req,res)=>{
var page = Number (req.query.page || 1) // if not pass, default is 1
was Limit = 10
var pages = 1
Category.count().then((count)=>{
// calculate the total number of pages, rounded up to the maximum
pages = Math.ceil(count / limit)
// The value can not exceed the total number of pages Pages values
page = Math.min(page,pages)
// The value can not be less than 1
page = Math.max(page,1)
var skip = (page-1)*limit
Category.find().limit(limit).skip(skip).then((categories) => {
res.render('admin/category',{
userInfo: req.userInfo,
categories:categories,
page:page,
pages:pages,
count:count,
limit:limit
})
})
})
})
/**
* Classification changes, the first show, and then modify
*
* 1, obtaining [Display] (GET)
*/
router.get('/category/edit',(req,res)=>{
// Get the information you want to modify the classification, showing the form of the form
// Get the id
where id = req.query.id || ''
// Find according to id
Category.findOne({
_id: id // [to MongoDB database field _id as a standard, not the id, video bug]
}).then((category)=>{
if(!category){
res.render('admin/error',{
user:req.userInfo,
message: 'To modify the category does not exist'
})
return Promise.reject()
}else{
res.render('admin/category_edit',{
user:req.userInfo,
category:category
})
}
})
})
/**
* Modify Category
*
* 2, [Save] modified data (POST)
*/
router.post('/category/edit',(req,res)=>{
// Get the id
where id = req.query.id || ''
// Get the name of the submission to be modified
var category_name = req.body.category_name || ''
console.log('category_name :'+category_name)
// database query whether you want to modify this data exists
Category.findOne({
_id:id,
}).then((rs)=>{
if(!rs){
res.render('admin/error',{
user:req.userInfo,
message: 'To modify the category does not exist'
})
return Promise.reject()
}else{
// When the user does not have to make any changes
if(rs.category_name==category_name){
res.render('admin/error',{
user:req.userInfo,
message: 'modification is successful - (do not make any changes, this article is false information)'
})
return Promise.reject()
}else{
// determine whether to modify the existing data in the database, an error message is returned, it does not exist save
/**
* Return value would go below then, the return of the same name category name
*/
return Category.findOne({
/**
* 1, {$ ne: id}: is not equal to the current id wider.
* 2, query the database id not want to modify the same record but the name id
* 3, if there Description: There are repeated. Reject changes
*/
_id: {$ not: id}
category_name:category_name
})
}
}
}).then((sameCategory)=>{
if(sameCategory){
res.render('admin/error',{
user:req.userInfo,
message: 'the name of the same name already exists in the database'
})
return Promise.reject()
}else{
/**
* Updates
* The first parameter: Conditions
* The second argument: Modified field
* Return lost out to then use
*/
return Category.update({
_id:id
},{
category_name:category_name
})
}
}).then((rs)=>{
if(rs){
res.render('admin/success',{
user:req.userInfo,
message: 'category name changed successfully'
})
}
})
})
/**
* Categories Clear
*/
router.get('/category/delete',(req,res)=>{
// Get To delete classified id
where id = req.query.id || ''
// delete
Category.remove({
_id:id
}).then((rs)=>{
if(rs){
res.render('admin/success',{
user:req.userInfo,
message: 'Category deleted successfully'
})
}
})
})