vue實(shí)現(xiàn)選擇商品規(guī)格功能
本文實(shí)例為大家分享了vue實(shí)現(xiàn)選擇商品規(guī)格功能的具體代碼,供大家參考,具體內(nèi)容如下
要實(shí)現(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: [] // 是否選中 因?yàn)椴淮_定是多規(guī)格還是但規(guī)格,所以這里定義數(shù)組來(lái)判斷 ?? ??? ?} ?? ?}, ?? ?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ù)組存儲(chǔ)被選中的值 ?? ? ? ? ?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; // 還原,目的是記錄點(diǎn)下去那個(gè)值,避免下一次執(zhí)行循環(huán)時(shí)避免被覆蓋 ?? ? ? ? ?} ?? ? ? ? ?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>
這只是簡(jiǎn)單的實(shí)現(xiàn)選擇規(guī)格,如果一開始后臺(tái)有返回?cái)?shù)據(jù),還需要定義一個(gè)接收數(shù)據(jù)的對(duì)象。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue使用html2canvas和jspdf將html轉(zhuǎn)成pdf
在前端開發(fā)中, html轉(zhuǎn)pdf是最常見的需求,下面這篇文章主要給大家介紹了關(guān)于vue如何使用html2canvas和jspdf將html轉(zhuǎn)成pdf的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03項(xiàng)目中Axios二次封裝實(shí)例Demo
vue項(xiàng)目經(jīng)常會(huì)用到axios來(lái)請(qǐng)求數(shù)據(jù),那么首先肯定需要對(duì)這個(gè)請(qǐng)求方法進(jìn)行一個(gè)二次封裝,這篇文章主要給大家介紹了關(guān)于項(xiàng)目中Axios二次封裝的相關(guān)資料,需要的朋友可以參考下2021-06-06ant design中upload組件上傳大文件,顯示進(jìn)度條進(jìn)度的實(shí)例
這篇文章主要介紹了ant design中upload組件上傳大文件,顯示進(jìn)度條進(jìn)度的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-10-10Vue設(shè)置select下拉框的默認(rèn)選項(xiàng)詳解(select空白bug解決)
最近開始學(xué)習(xí)vue,在學(xué)習(xí)的過程中遇到的問題將記錄在這里,下面這篇文章主要給大家介紹了關(guān)于Vue設(shè)置select下拉框的默認(rèn)選項(xiàng)(select空白bug解決)的相關(guān)資料,需要的朋友可以參考下2022-12-12vue實(shí)現(xiàn)導(dǎo)航標(biāo)題欄隨頁(yè)面滾動(dòng)漸隱漸顯效果
這篇文章主要介紹了vue實(shí)現(xiàn)導(dǎo)航標(biāo)題欄隨頁(yè)面滾動(dòng)漸隱漸顯效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03