2019-08-23 14:03:29 3515瀏覽
今天千鋒扣丁學(xué)堂HTML5培訓(xùn)老師給大家分享一篇關(guān)于使用vue實(shí)現(xiàn)多規(guī)格選擇實(shí)例(SKU)的詳細(xì)介紹,首先邏輯得清晰定義一個(gè)數(shù)組把選中的值存儲(chǔ)起來(lái)。定義一個(gè)對(duì)象存儲(chǔ)要匹配的數(shù)據(jù),把選中的值與存儲(chǔ)的數(shù)據(jù)進(jìn)行遍歷查找與之匹配的值的庫(kù)存,若庫(kù)存為0按鈕為灰色不能選擇。
<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>
<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>
<style lang="scss" rel="stylesheet"> .wrap-sku { .product-box { width: 1200px; display: block; margin: 0 auto; } .product-content { margin-bottom: 100px; } .product-delcom { color: #323232; font-size: 26px; border-bottom: 1px solid #EEEEEE; padding: 30px 0; } .product-footerlist { margin-top: 10px; } .product-footerlist li { border: 1px solid #606060; border-radius: 5px; color: #606060; text-align: center; padding: 10px 30px; float: left; margin-right: 20px; cursor: pointer; } .product-footerlist li.productActive { background-color: #1A1A29; color: #fff; border: 1px solid #1A1A29; } .product-footerlist li.noneActive { background-color: #ccc; opacity: 0.4; color: #000; pointer-events: none; } .product-footer { background-color: #1A1A29; text-align: center; } .product-footer a { color: #fff; text-decoration: none; height: 88px; line-height: 88px; font-size: 28px; } .price{ font-size: 30px; height: 60px; line-height: 60px; } } </style>
4.最后當(dāng)然是上效果圖了
【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】 【掃碼進(jìn)入HTML5前端開發(fā)VIP免費(fèi)公開課】
查看更多關(guān)于“HTML5開發(fā)技術(shù)資訊”的相關(guān)文章>>