model
app/model/goods_type.js
module.exports = app => {
const mongoose = app.mongoose;
const Schema = mongoose.Schema;
var d=new Date();
const GoodsTypeSchema = new Schema({
title: { type: String },
description: { type: String },
status:{
type:Number,
default:1
},
add_time:{
type:Number,
default:d.getTime()
}
});
return mongoose.model('GoodsType', GoodsTypeSchema,'goods_type');
}
router.js
router.get('/admin/goodsType', controller.admin.goodsType.index);
router.get('/admin/goodsType/add', controller.admin.goodsType.add);
router.get('/admin/goodsType/edit', controller.admin.goodsType.edit);
router.post('/admin/goodsType/doEdit', controller.admin.goodsType.doEdit);
router.post('/admin/goodsType/doAdd', controller.admin.goodsType.doAdd);
controller
app/controller/admin/goodsType.js
'use strict';
var BaseController =require('./base.js');
class GoodsTypeController extends BaseController {
async index() {
var result = await this.ctx.model.GoodsType.find({});
await this.ctx.render('admin/goodsType/index',{
list:result
})
}
async add() {
await this.ctx.render('admin/goodsType/add')
}
async doAdd() {
var res = new this.ctx.model.GoodsType(this.ctx.request.body);
await res.save();
await this.success('/admin/goodsType','增加类型成功');
}
async edit() {
var id = this.ctx.query.id;
var result = await this.ctx.model.GoodsType.find({"_id":id});
await this.ctx.render('admin/goodsType/edit',{
list:result[0]
})
}
async doEdit() {
var _id = this.ctx.request.body._id;
var title = this.ctx.request.body.title;
var description = this.ctx.request.body.description;
await this.ctx.model.GoodsType.updateOne({"_id":_id},{
title,description
})
await this.success('/admin/goodsType','编辑类型成功');
}
}
module.exports = GoodsTypeController;
增加权限,显示左侧导航

view
查找
app/view/admin/goodsType/index.html
<%- include ../public/page_header.html %>
<div class="panel panel-default">
<div class="panel-heading">
<a href="/admin/goodsType/add" class="btn btn-primary">增加商品类型</a>
</div>
<div class="panel-body">
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr class="th">
<th>商品类型名称</th>
<th>描述</th>
<th>增加时间</th>
<th class="text-center">状态</th>
<th class="text-center">操作</th>
</tr>
</thead>
<tbody>
<%for(var i=0;i<list.length;i++){%>
<tr>
<td>
<%=list[i].title%>
</td>
<td>
<%=list[i].description%>
</td>
<td>
<%=helper.formatTime(list[i].add_time)%>
</td>
<td class="text-center">
<%if(list[i].status==1){%>
<img src="/public/admin/images/yes.gif" onclick="app.changeStatus(this,'GoodsType','status','<%=list[i]._id%>')" />
<%}else{%>
<img src="/public/admin/images/no.gif" onclick="app.changeStatus(this,'GoodsType','status','<%=list[i]._id%>')" />
<%}%>
</td>
<td class="text-center"><a href="/admin/goodsTypeAttribute?id=<%=list[i]._id%>">属性列表</a> <a
href="/admin/goodsType/edit?id=<%=list[i]._id%>">修改</a> <a class="delete" href="/admin/delete?model=GoodsType&id=<%=list[i]._id%>">删除</a></td>
</tr>
<%}%>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>

增加
app/view/admin/goodsType/add.html
<form action="/admin/goodsType/doAdd" method="post">
<ul>
<input type="hidden" name="_csrf" value="<%=csrf%>">
<li> 商品类型名称: <input type="text" name="title"/></li>
<li>
商品类型描述:
<textarea name="description" id="" cols="60" rows="8"></textarea>
</li>
<li>
<br/>
<button type="submit" class="btn btn-primary">提交</button>
</li>
</ul>
</form>

编辑
app/view/admin/goodsType/edit.html
<form action="/admin/goodsType/doEdit" method="post">
<ul>
<input type="hidden" name="_csrf" value="<%=csrf%>">
<input type="hidden" name="_id" value="<%=list._id%>">
<li> 商品类型名称: <input type="text" name="title" value="<%=list.title%>"/></li>
<li>
商品类型描述:
<textarea name="description" id="" cols="60" rows="8"><%=list.description%></textarea>
</li>
<li>
<br/>
<button type="submit" class="btn btn-primary">提交</button>
</li>
</ul>
</form>

**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。