vue實現(xiàn)選擇商品規(guī)格功能
本文實例為大家分享了vue實現(xiàn)選擇商品規(guī)格功能的具體代碼,供大家參考,具體內(nèi)容如下

要實現(xiàn)上圖的效果,代碼如下:
<template>
?? ??? ?<div class="popupSub">已選規(guī)格:{{showSelectSpec}}</div>
? ? ? ? ? <div class="subItem" v-for="(item,index) in subItemList" :key="index">
? ? ? ? ? ? <div class="itemTitle">{{item.itemTitle}}</div>
? ? ? ? ? ? <div class="itemContent">
? ? ? ? ? ? ? <ul>
? ? ? ? ? ? ? ? <li
? ? ? ? ? ? ? ? ? v-for="(res,resIndex) in item.itemContent"
? ? ? ? ? ? ? ? ? :key="res"
? ? ? ? ? ? ? ? ? @click="selectItem(res,index,$event,resIndex)"
? ? ? ? ? ? ? ? ? :class="subIndex[index] == resIndex?'selectActive':'itemLi'"
? ? ? ? ? ? ? ? >{{res}}</li>
? ? ? ? ? ? ? </ul>
? ? ? ? ? ? </div>
? ? ? ? ? </div>
</template>
<script>
export default {
?? ?data() {
?? ??? ?return {
?? ??? ? ?showSelectSpec: "",
?? ? ? ? ?subItemList: [
?? ? ? ? ? ?{
?? ? ? ? ? ? ?itemTitle: "味道",
?? ? ? ? ? ? ?itemContent: ["原味", "獼猴桃味", "橙子味", "蘋果味", "菠蘿味"]
?? ? ? ? ? ?},
?? ? ? ? ? ?{
?? ? ? ? ? ? ?itemTitle: "容量",
?? ? ? ? ? ? ?itemContent: ["300ML", "400ML", "500ML", "1000ML"]
?? ? ? ? ? ?}
?? ? ? ? ?],
?? ? ? ? ?selectArr: [], // 存放被選中的值
?? ? ? ? ?subIndex: [] // 是否選中 因為不確定是多規(guī)格還是但規(guī)格,所以這里定義數(shù)組來判斷
?? ??? ?}
?? ?},
?? ?methods: {
?? ??? ?selectSpec(index) {
?? ? ? ? ?let t = this;
?? ? ? ? ?t.showSpec = true;
?? ? ? ?},
?? ? ? ?selectItem(res, index, enevt, resIndex) {
?? ? ? ? ?let t = this;
?? ? ? ? ?if (t.selectArr[index] !== res) {
?? ? ? ? ? ?t.selectArr[index] = res;
?? ? ? ? ? ?t.subIndex[index] = resIndex;
?? ? ? ? ?} else {
?? ? ? ? ? ?t.selectArr[index] = "";
?? ? ? ? ? ?t.subIndex[index] = -1; // 去掉選中的顏色
?? ? ? ? ?}
?? ? ? ? ?t.checkItem();
?? ? ? ?},
?? ? ? ?checkItem: function() {
?? ? ? ? ?var self = this;
?? ? ? ? ?var option = self.subItemList;
?? ? ? ? ?var result = []; // 定義數(shù)組存儲被選中的值
?? ? ? ? ?console.log(JSON.parse(JSON.stringify(self.selectArr)));
?? ? ? ? ?for (let i in option) {
?? ? ? ? ? ?result[i] = self.selectArr[i] ? self.selectArr[i] : "";
?? ? ? ? ?}
?? ? ? ? ?for (let i in option) {
?? ? ? ? ? ?var last = result[i]; // 把選中的值存放到字符串last去
?? ? ? ? ? ?for (let k in option[i].item) {
?? ? ? ? ? ? ?result[i] = option[i].item[k].name; // 賦值,存在直接覆蓋,不存在往里面添加name值
?? ? ? ? ? ? ?console.log("這里:", JSON.parse(JSON.stringify(result)));
?? ? ? ? ? ?}
?? ? ? ? ? ?result[i] = last; // 還原,目的是記錄點下去那個值,避免下一次執(zhí)行循環(huán)時避免被覆蓋
?? ? ? ? ?}
?? ? ? ? ?self.$forceUpdate(); // 重繪
?? ? ? ? ?self.showSelectSpec = self.selectArr.join("、");
?? ? ? ? ?console.log(self.showSelectSpec);
?? ? ? ?}
?? ?}
}
</script>
<style>
.popupSub {
? color: #aaaaaa;
? text-align: center;
? font-size: 0.8em;
? margin-top: 5px;
? letter-spacing: 2px;
}
.subItem {
? font-size: 0.8em;
? margin-top: 10px;
}
.itemContent ul,
li {
? display: flex;
? flex-wrap: wrap;
}
.itemContent ul li {
? padding: 0 10px;
? border-radius: 10px;
? margin-right: 10px;
? margin-top: 10px;
? height: 28px;
? line-height: 28px;
}
.itemLi {
? border: 1px solid #b3b3b3;
}
.selectActive {
? border: 1px solid #1697db;
? color: #1697db;
}
</style>這只是簡單的實現(xiàn)選擇規(guī)格,如果一開始后臺有返回數(shù)據(jù),還需要定義一個接收數(shù)據(jù)的對象。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
三分鐘讓你快速學會axios在vue項目中的基本用法(推薦!)
Axios是一個基于Promise用于瀏覽器和nodejs的HTTP客戶端,下面這篇文章主要給大家介紹了如何通過三分鐘讓你快速學會axios在vue項目中的基本用法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-04-04
el-table表頭使用el-dropdown出現(xiàn)兩個下拉框的問題及解決方法
本文給大家分享el-table在固定右邊列時,表頭使用el-dropdown會出現(xiàn)兩個下拉框的解決方法,感興趣的朋友跟隨小編一起看看吧2024-07-07
Vue Element前端應用開發(fā)之根據(jù)ABP后端接口實現(xiàn)前端展示
本篇著重介紹基于ABP后端接口信息,實現(xiàn)對前端界面的開發(fā)工作。2021-05-05
Vue中使用matomo進行訪問流量統(tǒng)計的實現(xiàn)
這篇文章主要介紹了Vue中使用matomo進行訪問流量統(tǒng)計的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11
Vue3之路由的query參數(shù)和params參數(shù)用法
這篇文章主要介紹了Vue3之路由的query參數(shù)和params參數(shù)用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
Vue?報錯-4058?ENOENT:no?such?file?or?directory的原因及解決方法
Vue?報錯-4058?ENOENT:?no?such?file?or?directory的原因和解決辦法,關于為什么為會報這個錯誤,按照字面意思的理解就是沒有找到這個文件或這個路徑,說明是路徑不對,本文給大家分享解決方案,感興趣的朋友一起看看吧2023-10-10

