后台商品屬性組合小例子


     今天2016.12.23馬上聖誕節了,先祝大家聖誕快樂。最近,一直在忙公司購物webApp項目,用到技術vue vue-router等(等到項目上線后拿出來分享下)。這兩天穿插着后台讓幫忙做一個關於商品屬性組合方面東西。今天交付給后台了,距離下班的這段時間時間把寫的東西分享出來。當然代碼里邊還有很多可以優化的地方,后期如果有時間再拿出來說。

    話不多說,先把結果圖拿出來看下,知道是個什么東西。當然可能有些我說的不好,可以再詳細的看下代碼。

     

    哎呦我去,公司這網速傳了老半天。

    直接看效果 https://huashuaipeng.github.io/productdemo.io/demo.html

    大體操作流程是這樣的

     1.點擊“添加多級型號”按鈕,彈出層選擇商品的默認型號“顏色或者尺碼”(也可以新建)

     2.點擊彈出層確定后,商品規格的一些默認選項 如:顏色:::xxl,xxxl,m,s

     3.選擇完對應規格后會進行組合,動態生成表格

     知識點

     1.用到的js庫(jquery,layer.js)jq不說了,layer應該大家也不知道 不知道的同學點這里 http://www.layui.com/doc/modules/layer.html 

     2.面向對象編程

     3.js內部的一些知識 數組 循環等等

    這塊核心代碼 是兩個類 我大概貼出來(當然個人習慣不是說寫的很好,請大家指正)類1 :Skumodel 生成規格 類2:SkuCell動態產生表格內容類

    

  1 /***
  2              * Skumodel 為生成規格類
  3              * title為規格名字  string  例:尺碼
  4              * times將要生成的規格項目  Array  例如:尺碼:xxl,xl,m,s
  5              * dataid為產生型號的標識最外層元素上的id   string
  6              * **/
  7             var Skumodel = function(title, items, dataid) {
  8                 //最外層div+標題欄
  9                 this.title = title || "";
 10                 this.items = items || [];
 11                 this.container = $('<div class="sku_container" id="' + dataid + '"><div class="sku_modellist_title">' + this.title + ':</div></div>');
 12                 //模型列表
 13                 this.skumodels = $('<div class="sku_models"></div>');
 14                 this.skumlist = $('<div class="sku_list"></div>')
 15                 this.skuinputcon = $('<div class="sku_add"></div>');
 16                 //輸入框
 17                 this.skuinput = $('<input type="text" placeholder="請輸入型號屬性">');
 18                 //新建按鈕
 19                 this.addbtn = $('<a>新建</a>');
 20                 this.init(this.items)
 21             }
 22             Skumodel.prototype = {
 23                 //初始化顯示組件
 24                 init: function(items) {
 25                     var html = ""
 26                     for(var i = 0; i < items.length; i++) {
 27                         html += '<span class="sku_item"><a data-id="' + getIndex() + '">' + items[i] + '</a><i class="sku_item_close">×</i></span>';
 28                     }
 29                     //獲取所有生成按鈕
 30                     this.skumlist.append($(html))
 31                     this.skumodels.append(this.skumlist)
 32                     this.container.append(this.skumodels)
 33                     this.skuinputcon.append(this.skuinput)
 34                     this.skuinputcon.append(this.addbtn)
 35                     this.skumodels.append(this.skuinputcon)
 36                     $(".sku_modellist").append(this.container);
 37                     this.bindEvent()
 38                 },
 39                 bindEvent: function() {
 40                     var self = this;
 41                     //點擊新建按鈕產生
 42                     this.addbtn.click(function() {
 43                         self.createItem();
 44                     });
 45                     this.activeItem();
 46                     //點擊刪除按鈕刪除
 47                     this.deleteItem();
 48                     //控制刪除符號
 49                     this.toggleCloseEle();
 50                 },
 51                 //創建sku子元素
 52                 createItem: function() {
 53                     var value=$.trim(this.skuinput.val())
 54                     if(value.length <= 0) {
 55                         layer.alert("請輸入內容");
 56                         return
 57                     }
 58                     if(sizes[this.title].indexOf(value)>=0){
 59                         layer.msg("請勿重復創建")
 60                         return;
 61                     }
 62                     sizes[this.title].push(this.skuinput.val())
 63                     this.skumlist.append($('<span class="sku_item"><a data-id="' + getIndex() + '">' + value + '</a><i class="sku_item_close">×</i></span>'))
 64                     this.skuinput.val("")
 65                 },
 66                 //子元素是否選中事件
 67                 activeItem: function() {
 68                     this.skumlist.on("click", "a", function() {
 69                         $(this).toggleClass("itemactive");
 70                         tableContent()
 71                     });
 72                 },
 73                 //監聽刪除元素
 74                 deleteItem: function() {
 75                     var self=this;
 76                     this.skumlist.on("click", ".sku_item_close", function() {
 77                         $(this).parent().remove();
 78                         var text = $(this).parent().find("a").text();
 79                         var textarr = sizes[self.title];
 80                         textarr.splice(textarr.indexOf(text), 1)
 81                         tableContent();
 82                     });
 83                 },
 84                 //控制刪除符號的顯示
 85                 toggleCloseEle: function() {
 86                     //顯示刪除符號
 87                     this.skumlist.on("mouseover", ".sku_item", function() {
 88                         $(this).find(".sku_item_close").css({
 89                             display: "inline-block"
 90                         })
 91                     });
 92                     //顯示刪除符號
 93                     this.skumlist.on("mouseout", ".sku_item", function() {
 94                         $(this).find(".sku_item_close").css({
 95                             display: "none"
 96                         })
 97                     });
 98                 }
 99             };
100 
101             /****
102              * SkuCell動態產生表格內容類
103              * cellist為表格內部元素    Array   如["紅色","xxl"]
104              * dataid為行表格id 產生元素的唯一標識   string
105              * ***/
106             var SkuCell = function(celllist, dataid) {
107                 //每行表格的父元素
108                 this.cellcon = $('<div id="' + dataid + '" class="sku_cell clearfix"></div>');
109                 //價格輸入
110                 this.moneyInput = $('<div class="sku_t_title"><input /></div>');
111                 //庫存輸入
112                 this.leftInput = $('<div class="sku_t_title"><input /></div>');
113                 this.init(celllist)
114             };
115             SkuCell.prototype = {
116                 constructor: SkuCell,
117                 init: function(celllist) {
118                     var html = "";
119                     for(var i = 0; i < celllist.length; i++) {
120                         html += '<div class="sku_t_title">' + celllist[i] + '</div>'
121                     }
122                     this.cellcon.append($(html));
123                     this.cellcon.append(this.moneyInput);
124                     this.cellcon.append(this.leftInput);
125                     $('.sku_tablecell').append(this.cellcon)
126                 }
127             };

接下來就是組合部分

            /***
             * 排列組合計算出選擇的規格型號的組合方式
             * 
             * */
            function getResult() {
                var head = arguments[0][0];
                for(var i in arguments[0]) {
                    if(i != 0) {
                        head = group(head, arguments[0][i])
                    }
                }
                tabledata = [];
                $(".sku_cell").each(function(index) {
                    tabledata.push($(this).attr("id"))
                }).hide()
                for(var j = 0, len = head.length; j < len; j++) {
                    var newcell = head[j]["datatext"].split(',')
                    var dataid = head[j]["dataid"];
                    if(tabledata.indexOf(dataid) < 0) {
                        new SkuCell(newcell, dataid)
                    } else {
                        $("#" + dataid).show()
                    }
                }
            };
            //組合前兩個數據
            function group(first, second) {
                var result = [];
                for(var i = 0, leni = first.length; i < leni; i++) {
                    for(var j = 0, len = second.length; j < len; j++) {
                        result.push({
                            dataid: first[i]["dataid"] + "-" + second[j]["dataid"],
                            datatext: first[i]["datatext"] + "," + second[j]["datatext"]
                        })
                    }
                }
                return result
            }

詳細部分大家下載demo看下,地址:https://github.com/huashuaipeng/productdemo

    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM