乐趣区

egg(74)–egg之商品的图库关联颜色表,以及图库的curd

model
app/model/goods_image.js
module.exports = app => {
const mongoose = app.mongoose;
const Schema = mongoose.Schema;

var d=new Date();
const GoodsImageSchema = new Schema({
goods_id: {type:Schema.Types.ObjectId},
img_url: {type: String},
color_id:{
type:Schema.Types.Mixed, // 混合类型
default: ”
},
status: {type: Number,default:1},
add_time: {
type:Number,
default: d.getTime()
}

});

return mongoose.model(‘GoodsImage’, GoodsImageSchema,’goods_image’);
}
app/model/goods_color.js

module.exports = app => {
const mongoose = app.mongoose;
const Schema = mongoose.Schema;

const GoodsColorSchema = new Schema({
color_name:{type:String},
color_value: {type: String},
status: {type: Number,default:1},
});
return mongoose.model(‘GoodsColor’, GoodsColorSchema,’goods_color’);
}
router.js
router.post(‘/admin/goods/changeGoodsImageColor’, controller.admin.goods.changeGoodsImageColor);
router.post(‘/admin/goods/goodsImageRemove’, controller.admin.goods.goodsImageRemove);
controller
app/controller/admin/goods.js

async changeGoodsImageColor() {

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

var goods_image_id=this.ctx.request.body.goods_image_id;
console.log(this.ctx.request.body);
if(color_id){
color_id=this.app.mongoose.Types.ObjectId(color_id);
}

var result= await this.ctx.model.GoodsImage.updateOne({“_id”:goods_image_id},{
color_id:color_id
})
if(result){

this.ctx.body={‘success’: true,’message’:’ 更新数据成功 ’};
}else{

this.ctx.body={‘success’: false,’message’:’ 更新数据失败 ’};
}

}
view
app/view/admin/goods/edit.html
<div role=”tabpanel” class=”tab-pane” id=”photo”>

<div id=”goods_image”>
<ul id=”goods_image_list” class=”goods_image_list clear”>

<%for(var i=0;i<goodsImage.length;i++){%>
<li>
<img class=”pic” src=”<%=goodsImage[i].img_url%>” />

<div class=”color_list”>

<select class=”relation_goods_color” goods_image_id=”<%=goodsImage[i]._id%>”>
<option value=”0″> 关联颜色 </option>

<%for(var j=0;j<goodsColor.length;j++){%>

<option value=”<%=goodsColor[j]._id%>” <%if(goodsImage[i].color_id.toString()==goodsColor[j]._id){%>selected<%}%> ><%=goodsColor[j].color_name%></option>
<%}%>
</select>
</div>

<div class=”goods_image_delete” goods_image_id=”<%=goodsImage[i]._id%>”></div>
</li>
<%}%>
</ul>
</div>

<div id=”photoLib” class=”photoLib”></div>

<div id=”photoList”>

</div>
</div>
$(‘.relation_goods_color’).change(function(){

var color_id=$(this).val();
var goods_image_id=$(this).attr(‘goods_image_id’);

console.log(color_id,goods_image_id);

$.post(‘/admin/goods/changeGoodsImageColor?_csrf=<%=csrf%>’,{color_id:color_id,goods_image_id:goods_image_id},function(response){

console.log(response);
})

})
效果

退出移动版