做過商城項(xiàng)目的小伙伴們,相信大家多多少少都會(huì)接觸到規(guī)格選擇這個(gè)模塊,也就是所說的SKU。
公司最近在做一個(gè)下單系統(tǒng),這里面就涉及到這個(gè)SKU,說實(shí)話之前我是沒有寫過這個(gè)的,剛開始也是有點(diǎn)迷茫把,不知道該如何下手,因?yàn)橐紤]到后端那邊返回的數(shù)據(jù)結(jié)構(gòu)、庫(kù)存、多規(guī)格等等問題,然后各種百度,各種搜集資料,才慢慢懂了其中的邏輯,下面我就簡(jiǎn)單寫個(gè)demo吧。
首先邏輯得清晰
上代碼 秒懂 哈哈
1.html
<template> <div class="wrap wrap-sku"> <div class="product-box"> <div class="product-content"> <div class="product-delcom" v-for="(ProductItem,n) in simulatedDATA.specifications"> <p>{{ProductItem.name}}</p> <ul class="product-footerlist clearfix"> <li v-for="(oItem,index) in ProductItem.item" v-on:click="specificationBtn(oItem.name,n,$event,index)" v-bind:class="[oItem.isShow?'':'noneActive',subIndex[n] == index?'productActive':'']"> {{oItem.name}} </li> </ul> </div> <p v-if="price" class="price">¥{{price}}</p> </div> <div class="product-footer"> <a href="javascript:" rel="external nofollow" >立即購(gòu)買</a> </div> </div> </div></template>2.js
<script> export default { data() { return { simulatedDATA: { //模擬后臺(tái)返回的數(shù)據(jù) 多規(guī)格 "difference": [ { //所有的規(guī)格可能情況都在這個(gè)數(shù)組里 "id": "19", "price": "200.00", "stock": "19", "difference": "100,白色" }, { "id": "20", "price": "100.00", "stock": "29", "difference": "200,白色" }, { "id": "21", "price": "300.00", "stock": "10", "difference": "100,黑色" }, { "id": "22", "price": "900.00", "stock": "0", "difference": "200,黑色" }, { "id": "23", "price": "600.00", "stock": "48", "difference": "100,綠色" }, { "id": "24", "price": "500.00", "stock": "40", "difference": "200,綠色" }, { "id": "25", "price": "90.00", "stock": "0", "difference": "100,藍(lán)色" }, { "id": "26", "price": "40.00", "stock": "20", "difference": "200,藍(lán)色" } ], "specifications": [ { //這里是要被渲染字段 "name": "尺寸", "item": [ { "name": "100", }, { "name": "200", } ] }, { "name": "顏色", "item": [ { "name": "白色", }, { "name": "藍(lán)色", }, { "name": "黑色", }, { "name": "綠色", } ] } ] }, selectArr: [], //存放被選中的值 shopItemInfo: {}, //存放要和選中的值進(jìn)行匹配的數(shù)據(jù) subIndex: [], //是否選中 因?yàn)椴淮_定是多規(guī)格還是單規(guī)格,所以這里定義數(shù)組來(lái)判斷 price:'' //選中規(guī)格的價(jià)錢 } }, methods: { specificationBtn: function (item, n, event, index) { var self = this; if (self.selectArr[n] != item) { self.selectArr[n] = item; self.subIndex[n] = index; } else { self.selectArr[n] = ""; self.subIndex[n] = -1; //去掉選中的顏色 } self.checkItem(); }, checkItem: function () { var self = this; var option = self.simulatedDATA.specifications; var result = []; //定義數(shù)組儲(chǔ)存被選中的值 for(var i in option){ result[i] = self.selectArr[i] ? self.selectArr[i] : ''; } for (var i in option) { var last = result[i]; //把選中的值存放到字符串last去 for (var k in option[i].item) { result[i] = option[i].item[k].name; //賦值,存在直接覆蓋,不存在往里面添加name值 option[i].item[k].isShow = self.isMay(result); //在數(shù)據(jù)里面添加字段isShow來(lái)判斷是否可以選擇 } result[i] = last; //還原,目的是記錄點(diǎn)下去那個(gè)值,避免下一次執(zhí)行循環(huán)時(shí)被覆蓋 } if(this.shopItemInfo[result]){ this.price = this.shopItemInfo[result].price || '' } self.$forceUpdate(); //重繪 }, isMay: function (result) { for (var i in result) { if (result[i] == '') { return true; //如果數(shù)組里有為空的值,那直接返回true } } return this.shopItemInfo[result].stock == 0 ? false : true; //匹配選中的數(shù)據(jù)的庫(kù)存,若不為空返回true反之返回false } }, created: function () { var self = this; for (var i in self.simulatedDATA.difference) { self.shopItemInfo[self.simulatedDATA.difference[i].difference] = self.simulatedDATA.difference[i]; //修改數(shù)據(jù)結(jié)構(gòu)格式,改成鍵值對(duì)的方式,以方便和選中之后的值進(jìn)行匹配 } self.checkItem(); } }</script>
新聞熱點(diǎn)
疑難解答
圖片精選