egg(60,61,62)--商品類型屬性的curd,商品類型和商品類型屬性關聯

model

app/model/goods_type_attribute.js

goods_type_attribute經過cate_id關聯goods_typehtml

module.exports = app => {
    const mongoose = app.mongoose;
    const Schema = mongoose.Schema;
    var d=new Date();
   
    const GoodsTypeAttributeSchema = new Schema({
      cate_id:{ type:Schema.Types.ObjectId },
      title: { type: String  },
      attr_type: { type: String  },      //類型  1 input    2  textarea    三、select
      attr_value:{ type: String  },      //默認值: input  textarea默認值是空     select框有默認值  多個默認值以回車隔開
      status: { type: Number,default:1  },    
      add_time: {           
        type:Number,        
        default: d.getTime()    
      }
    }); 
    return mongoose.model('GoodsTypeAttribute', GoodsTypeAttributeSchema,'goods_type_attribute');
  }

router.js

router.get('/admin/goodsTypeAttribute', controller.admin.goodsTypeAttribute.index);
    router.get('/admin/goodsTypeAttribute/add', controller.admin.goodsTypeAttribute.add);
    router.get('/admin/goodsTypeAttribute/edit', controller.admin.goodsTypeAttribute.edit); 
    router.post('/admin/goodsTypeAttribute/doEdit', controller.admin.goodsTypeAttribute.doEdit);
    router.post('/admin/goodsTypeAttribute/doAdd', controller.admin.goodsTypeAttribute.doAdd);

查找

controller

app/controller/admin/goodsTypeAttribute.js

goods_type_attribute`經過`cate_id`關聯`goods_typeapp

async index() {
        var cate_id = this.ctx.request.query.id;
        var goodsType=await this.ctx.model.GoodsType.find({"_id":cate_id})
        var result = await this.ctx.model.GoodsTypeAttribute.aggregate([
            {
            $lookup: {
                from: 'goods_type',
                localField: 'cate_id',
                foreignField: '_id',
                as: 'goods_type'
            }
        },{
            $match:{
                "cate_id":this.app.mongoose.Types.ObjectId(cate_id)
            }
        }])
        await this.ctx.render('admin/goodsTypeAttribute/index', {
            list:result,
            cate_id: cate_id,
            goodsType:goodsType[0]
        });
    }

view

app/view/admin/goodsTypeAttribute/index.html
<%- include ../public/page_header.html %>


            <div class="panel panel-default">              
                    <div class="panel-heading clear">
                       <span>商品類型屬性----<%=goodsType.title%></span> <a  href="/admin/goodsTypeAttribute/add?id=<%=cate_id%>" 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>增長時間</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].goods_type[0].title%></td>
                                            <td>                                                
                                                <%if(list[i].attr_type==1){%>
                                                    單行文本框
                                                <%}else if(list[i].attr_type==2){%>
                                                    多行文本框                                                 
                                                <%}else if(list[i].attr_type==3){%>
                                                    select下拉框   
                                                <%}%>                                                
                                            </td>
                                            <td><%=list[i].attr_value%></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,'GoodsTypeAttribute','status','<%=list[i]._id%>')" />
                                                    <%}else{%>
                                                            <img src="/public/admin/images/no.gif" onclick="app.changeStatus(this,'GoodsTypeAttribute','status','<%=list[i]._id%>')" />
                                                    <%}%>
                                            </td>                 
                                            <td class="text-center"><a href="/admin/goodsTypeAttribute/edit?id=<%=list[i]._id%>">修改</a> <a  class="delete" href="/admin/delete?model=GoodsTypeAttribute&id=<%=list[i]._id%>">刪除</a></td>
                                        </tr>
                            <%}%>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
</body>
</html>

效果

  1. 在商品類型列表中,點擊查看手機的屬性列表
  2. 看到手機的屬性的尺寸,大小,顏色

clipboard.png

clipboard.png

增長

controller

app/controller/admin/goodsTypeAttribute.js
async add() {
        // 獲取商品類型數據
        var cate_id = this.ctx.request.query.id;
        var goodsTypes = await this.ctx.model.GoodsType.find({});
        await this.ctx.render('admin/goodsTypeAttribute/add', {
            cate_id: cate_id,
            goodsTypes: goodsTypes
        })
    }

    async doAdd() {

        var res = new this.ctx.model.GoodsTypeAttribute(this.ctx.request.body);
        await res.save();
        await this.success('/admin/goodsTypeAttribute?id=' + this.ctx.request.body.cate_id, '增長商品類型屬性成功');

    }

view

app/view/admin/goodsTypeAttribute/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/goodsTypeAttribute/doAdd" method="post">
                <ul>
                    <input type="hidden" name="_csrf" value="<%=csrf%>">
                    <li>  屬性名稱: <input type="text" name="title" /></li>
                    <li>  所屬類型:
                        <select name="cate_id" id="cate_id">
                            <%for(var i=0;i<goodsTypes.length;i++){%>
                            <option <%if(cate_id.toString()==goodsTypes[i]._id.toString()){%>selected
                                <%}%> value="
                                <%=goodsTypes[i]._id%>" >
                                <%=goodsTypes[i].title%>
                            </option>
                            <%}%>
                        </select>
                    </li>
                    <li>  錄入方式:
                        <input type="radio" name="attr_type" value="1" checked="true" id="text" /><label for="text">單行文本框</label>                        
                        <input type="radio" name="attr_type" value="2" id="textarea" /><label for="textarea">多行文本框</label>                        
                        <input type="radio" name="attr_type" value="3" id="select" /><label for="select">從下面的列表中選擇(一行表明一個可選值)</label> 
                    </li>
                    <li>
                        可選值列表:
                        <textarea name="attr_value" id="attr_value" cols="60" rows="8" disabled="disabled"></textarea>
                    </li>
                    <li>
                        <br />
                        <button type="submit" class="btn btn-primary">提交</button>
                    </li>
                </ul>
            </form>
        </div>
    </div>
</div>

<script>
    $(function () {
        $("input[name='attr_type']").change(function () {
            console.log($(this).val());
            if ($(this).val() == 3) {
                $('#attr_value').attr('disabled', false)
            } else {
                $('#attr_value').attr('disabled', true)
            }
        });
    })
</script>
</body>
</html>

效果

  1. 所屬類型,獲取商品類型的全部數據
  2. 錄入方式,只有是列表,textarea才能夠輸入

clipboard.png

編輯

controller

app/controller/admin/goodsTypeAttribute.js
async edit() {
        var id=this.ctx.query.id;

        var result=await this.ctx.model.GoodsTypeAttribute.find({"_id":id});

        var goodsTypes=await this.ctx.model.GoodsType.find({});

        await this.ctx.render('admin/goodsTypeAttribute/edit',{

            list:result[0],
            goodsTypes:goodsTypes
        });
    }

    async doEdit() {
        var _id=this.ctx.request.body._id;
        await this.ctx.model.GoodsTypeAttribute.updateOne({"_id":_id},this.ctx.request.body);
        await this.success('/admin/goodsTypeAttribute?id='+this.ctx.request.body.cate_id,'修改商品類型屬性成功');
    }

view

app/view/admin/goodsTypeAttribute/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/goodsTypeAttribute/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>  所屬類型:
                        <select name="cate_id" id="cate_id">
                            <%for(var i=0;i<goodsTypes.length;i++){%>
                            <option <%if(list.cate_id.toString()==goodsTypes[i]._id.toString()){%>selected
                                <%}%> value="
                                <%=goodsTypes[i]._id%>" >
                                <%=goodsTypes[i].title%>
                            </option>
                            <%}%>
                        </select>
                    </li>
                    <li>  錄入方式:
                        <input type="radio" name="attr_type" value="1" <%if(list.attr_type==1){%> checked="true"
                        <%}%> id="text" /><label for="text">單行文本框</label>  
                        <input type="radio" name="attr_type" value="2" <%if(list.attr_type==2){%> checked="true"
                        <%}%> id="textarea" /><label for="textarea">多行文本框</label>  
                        <input type="radio" name="attr_type" value="3" <%if(list.attr_type==3){%> checked="true"
                        <%}%> id="select" /><label for="select">從下面的列表中選擇(一行表明一個可選值)</label> 
                    </li>
                    <li>
                        可選值列表:
                        <textarea name="attr_value" id="attr_value" cols="60" rows="8" <%if(list.attr_type!=3){%>disabled="disabled <%}%>"><%=list.attr_value%></textarea>
                    </li>
                    <li>
                        <br />
                        <button type="submit" class="btn btn-primary">提交</button>
                    </li>
                </ul>
            </form>
        </div>
    </div>
</div>
<script>
    $(function () {
        $("input[name='attr_type']").change(function () {
            console.log($(this).val());
            if ($(this).val() == 3) {
                $('#attr_value').attr('disabled', false)
            } else {
                $('#attr_value').attr('disabled', true)
            }
        });
    })
</script>

</body>

</html>

效果

所屬類型,默認選中
clipboard.pngasync

相關文章
相關標籤/搜索