egg(76)–egg之商品导航curd

49次阅读

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

效果

model
app/model/nav.js
module.exports = app => {
const mongoose = app.mongoose;
const Schema = mongoose.Schema;
var d=new Date();
const NavSchema = new Schema({
title: {type: String},
link: {type: String},
position: {
type:Number,
default:2 // 1 最顶部 2 中间 3 底部
},
is_opennew:{
type:Number,
default:1 //1、本窗口 2、新窗口
},
sort:{
type:Number,
default:100
},
relation:{// 1,2,3
type:String,
default:”
},
status: {type: Number,default:1},
add_time: {
type:Number,
default: d.getTime()
}

});

return mongoose.model(‘Nav’, NavSchema,’nav’);
}
router.js
router.get(‘/admin/nav’, controller.admin.nav.index);
router.get(‘/admin/nav/add’, controller.admin.nav.add);
router.get(‘/admin/nav/edit’, controller.admin.nav.edit);
router.post(‘/admin/nav/doEdit’, controller.admin.nav.doEdit);
router.post(‘/admin/nav/doAdd’, controller.admin.nav.doAdd);
controller
app/controller/admin/nav.js
‘use strict’;

var BaseController =require(‘./base.js’);

class NavController extends BaseController {
async index() {

var page=this.ctx.request.query.page || 1;
var pageSize=5;
// 获取当前数据表的总数量
var totalNum=await this.ctx.model.Nav.find({}).count();
// 分页查询
var result=await this.ctx.model.Nav.find({}).skip((page-1)*pageSize).limit(pageSize);

await this.ctx.render(‘admin/nav/index’,{
list:result,
totalPages:Math.ceil(totalNum/pageSize),
page:page
});

}

async add() {

await this.ctx.render(‘admin/nav/add’);

}

async doAdd() {

// console.log();

var nav=new this.ctx.model.Nav(this.ctx.request.body)

await nav.save(); // 注意

await this.success(‘/admin/nav’,’ 增加导航成功 ’);

}

async edit() {

var id=this.ctx.query.id;

var result=await this.ctx.model.Nav.find({“_id”:id});

await this.ctx.render(‘admin/nav/edit’,{

list:result[0]
});

}

async doEdit() {

var _id=this.ctx.request.body._id;

await this.ctx.model.Nav.updateOne({“_id”:_id},this.ctx.request.body)
await this.success(‘/admin/nav’,’ 编辑导航成功 ’);

}
}

module.exports = NavController;

view
查找
app/view/admin/nav/index.html
<%- include ../public/page_header.html %>

<!– 引入分页插件 –>

<script src=”/public/admin/js/jqPaginator.js”></script>

<div class=”panel panel-default”>
<div class=”panel-heading clear”>
<span> 网站导航 </span> <a href=”/admin/nav/add” class=”btn btn-primary fr”> 增加导航 </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> 位置 </th>
<th class=”text-center”> 排序 </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].relation%></td>

<td><%=list[i].link%></td>

<td>

<%if(list[i].position==1){%>

顶部

<%}else if(list[i].position==2){%>
中间

<%}else{%>
底部
<%}%>

</td>

<td class=”text-center”><span onclick=”app.editNum(this,’Nav’,’sort’,'<%=list[i]._id%>’)”><%=list[i].sort%></span></td>
<td class=”text-center”>

<%if(list[i].status==1){%>
<img src=”/public/admin/images/yes.gif” onclick=”app.changeStatus(this,’Nav’,’status’,'<%=list[i]._id%>’)” />

<%}else{%>
<img src=”/public/admin/images/no.gif” onclick=”app.changeStatus(this,’Nav’,’status’,'<%=list[i]._id%>’)” />
<%}%>

</td>
<td class=”text-center”> <a href=”/admin/nav/edit?id=<%=list[i]._id%>”> 修改 </a> <a class=”delete” href=”/admin/delete?model=Nav&id=<%=list[i]._id%>”> 删除 </a></td>
</tr>

<%}%>

</tbody>
</table>

<div id=”page” class=”pagination fr”></div>
</div>
</div>
</div>

<script>

$(‘#page’).jqPaginator({
totalPages: <%=totalPages%>,
visiblePages: 8,
currentPage: <%=page%>,
onPageChange: function (num, type) {
console.log(‘ 当前第 ’ + num + ‘ 页 ’,type);

if(type==’change’){

location.href=”/admin/nav?page=”+num;
}
}
});

</script>
</body>
</html>

增加
app/view/admin/nav/add.html
<%- include ../public/page_header.html %>

<div class=”panel panel-default”>
<div class=”panel-heading”>
增加导航
</div>
<div class=”panel-body”>
<div class=”table-responsive input-form”>
<form action=”/admin/nav/doAdd” method=”post” >
<ul class=”form_input”>
<input type=”hidden” name=”_csrf” value=”<%=csrf%>”>
<li> <span> 导航名称:</span> <input type=”text” name=”title”/></li>

<li> <span> 导航位置: </span>

<select name=”positon” id=”positon”>
<option value=”1″> 顶部 </option>
<option value=”2″ selected> 中间 </option>
<option value=”3″> 底部 </option>
</select>
</li>

<li> <span> 关联商品:</span> <input type=”text” name=”relation”/></li>

<li> <span> 导航连接地址:</span> <input type=”text” name=”link”/></li>

<li> <span> 新窗口打开: </span>
<select name=”is_opennew” id=”is_opennew”>
<option value=”1″> 否 </option>
<option value=”2″> 是 </option>
</select>

<li> <span> 排序:</span> <input type=”text” name=”sort” value=”10″/></li>

<li> <span> 状态: </span> <input type=”radio” name=”status” checked value=”1″ id=”a”/> <label for=”a”> 显示 </label> <input type=”radio” name=”status” value=”0″ id=”b”/><label for=”b”> 隐藏 </label> </li>

<li>
<br/>
<button type=”submit” class=”btn btn-primary”> 提交 </button>
</li>

</ul>

</form>
</div>
</div>

</div>

</body>
</html>

编辑
app/view/admin/nav/edit.html
<%- include ../public/page_header.html %>

<div class=”panel panel-default”>
<div class=”panel-heading”>
修改导航
</div>
<div class=”panel-body”>
<div class=”table-responsive input-form”>
<form action=”/admin/nav/doEdit” method=”post” >
<ul class=”form_input”>
<input type=”hidden” name=”_csrf” value=”<%=csrf%>”>

<input type=”hidden” name=”_id” value=”<%=list._id%>”/>
<li> <span> 导航名称:</span> <input type=”text” name=”title” value=”<%=list.title%>”/></li>

<li> <span> 导航位置: </span>

<select name=”position” id=”position”>
<option value=”1″ <%if(list.position==1){%>selected<%}%>> 顶部 </option>
<option value=”2″ <%if(list.position==2){%>selected<%}%>> 中间 </option>
<option value=”3″ <%if(list.position==3){%>selected<%}%>> 底部 </option>
</select>
</li>

<li> <span> 关联商品:</span> <input type=”text” name=”relation” value=”<%=list.relation%>”/></li>

<li> <span> 导航连接地址:</span> <input type=”text” name=”link” value=”<%=list.link%>”/></li>

<li> <span> 新窗口打开: </span>
<select name=”is_opennew” id=”is_opennew”>
<option value=”2″ <%if(list.is_opennew==1){%>selected<%}%>> 否 </option>
<option value=”1″ <%if(list.is_opennew==2){%>selected<%}%>> 是 </option>
</select>

<li> <span> 排序:</span> <input type=”text” name=”sort” value=”10″ value=”<%=list.sort%>”/></li>

<li> <span> 状 态:</span> <input type=”radio” name=”status” <%if(list.status==1){%> checked <%}%> value=”1″ id=”a”/> <label for=”a”> 显示 </label> <input type=”radio” <%if(list.status==0){%> checked <%}%> name=”status” value=”0″ id=”b”/><label for=”b”> 隐藏 </label> </li>

<li>
<br/>
<button type=”submit” class=”btn btn-primary”> 提交 </button>
</li>

</ul>

</form>
</div>
</div>

</div>

</body>
</html>

正文完
 0