国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

使用vue實現(xiàn)多規(guī)格選擇實例(SKU)

2019-11-19 10:59:31
字體:
供稿:網(wǎng)友

做過商城項目的小伙伴們,相信大家多多少少都會接觸到規(guī)格選擇這個模塊,也就是所說的SKU。

公司最近在做一個下單系統(tǒng),這里面就涉及到這個SKU,說實話之前我是沒有寫過這個的,剛開始也是有點迷茫把,不知道該如何下手,因為要考慮到后端那邊返回的數(shù)據(jù)結(jié)構(gòu)、庫存、多規(guī)格等等問題,然后各種百度,各種搜集資料,才慢慢懂了其中的邏輯,下面我就簡單寫個demo吧。

首先邏輯得清晰

  1. 定義一個數(shù)組把選中的值存儲起來。
  2. 定義一個對象存儲要匹配的數(shù)據(jù)。
  3. 把選中的值與存儲的數(shù)據(jù)進行遍歷查找與之匹配的值的庫存,若庫存為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" >立即購買</a>      </div>    </div>  </div></template>

2.js

<script>  export default {    data() {      return {        simulatedDATA: { //模擬后臺返回的數(shù)據(jù) 多規(guī)格          "difference": [            { //所有的規(guī)格可能情況都在這個數(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,藍色"            },            {              "id": "26",              "price": "40.00",              "stock": "20",              "difference": "200,藍色"            }          ],          "specifications": [            { //這里是要被渲染字段              "name": "尺寸",              "item": [                {                  "name": "100",                },                {                  "name": "200",                }              ]            },            {              "name": "顏色",              "item": [                {                  "name": "白色",                },                {                  "name": "藍色",                },                {                  "name": "黑色",                },                {                  "name": "綠色",                }              ]            }          ]        },        selectArr: [], //存放被選中的值        shopItemInfo: {}, //存放要和選中的值進行匹配的數(shù)據(jù)        subIndex: [], //是否選中 因為不確定是多規(guī)格還是單規(guī)格,所以這里定義數(shù)組來判斷        price:'' //選中規(guī)格的價錢      }    },    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ù)組儲存被選中的值        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; //還原,目的是記錄點下去那個值,避免下一次執(zhí)行循環(huán)時被覆蓋        }        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ù)的庫存,若不為空返回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)格式,改成鍵值對的方式,以方便和選中之后的值進行匹配      }      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.最后當然是上效果圖了

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 许昌市| 扬中市| 阳城县| 绥棱县| 岳西县| 崇义县| 苏尼特左旗| 自贡市| 山丹县| 伊通| 大邑县| 景德镇市| 谷城县| 兴国县| 乡宁县| 永春县| 顺平县| 黔江区| 五常市| 巴林左旗| 休宁县| 夹江县| 富裕县| 商河县| 上杭县| 邹平县| 凤山县| 永州市| 义马市| 曲周县| 托克逊县| 南和县| 蒲江县| 开鲁县| 文山县| 沭阳县| 兴和县| 定州市| 都江堰市| 铁力市| 德惠市|