使用vue實(shí)現(xiàn)多規(guī)格選擇實(shí)例(SKU)
做過商城項(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吧。
首先邏輯得清晰
- 定義一個(gè)數(shù)組把選中的值存儲(chǔ)起來。
- 定義一個(gè)對(duì)象存儲(chǔ)要匹配的數(shù)據(jù)。
- 把選中的值與存儲(chǔ)的數(shù)據(jù)進(jìn)行遍歷查找與之匹配的值的庫(kù)存,若庫(kù)存為0按鈕為灰色不能選擇。
上代碼 秒懂 哈哈
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ù)組來判斷
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來判斷是否可以選擇
}
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>
3.css
<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)然是上效果圖了

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
VUE如何實(shí)現(xiàn)點(diǎn)擊文字添加顏色(動(dòng)態(tài)修改class)
這篇文章主要介紹了VUE如何實(shí)現(xiàn)點(diǎn)擊文字添加顏色(動(dòng)態(tài)修改class),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Vue 計(jì)數(shù)器的實(shí)現(xiàn)
這篇文章主要介紹了Vue 計(jì)數(shù)器的實(shí)現(xiàn),主要利用HTML實(shí)現(xiàn)步驟現(xiàn)在頁面上簡(jiǎn)單實(shí)現(xiàn)一個(gè)計(jì)數(shù)器,內(nèi)容簡(jiǎn)單且詳細(xì),需要的朋友可以參考一下2021-10-10
淺談Vue3 Composition API如何替換Vue Mixins
這篇文章主要介紹了淺談Vue3 Composition API如何替換Vue Mixins,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Vue+Openlayer批量設(shè)置閃爍點(diǎn)的實(shí)現(xiàn)代碼(基于postrender機(jī)制)
本篇文章給大家介紹基于postrender機(jī)制使用Vue+Openlayer批量設(shè)置閃爍點(diǎn)的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-09-09
vue+ElementPlus框架Container 布局容器不能鋪滿整個(gè)屏幕的解決方案
這篇文章主要介紹了vue+ElementPlus框架Container 布局容器不能鋪滿整個(gè)屏幕的解決方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01
Vue3使用Swiper實(shí)現(xiàn)輪播圖示例詳解
這篇文章主要為大家介紹了Vue3使用Swiper實(shí)現(xiàn)輪播圖示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02

