vue实现选择商品规格功能
作者:carrie~苗 时间:2024-05-13 09:37:55
本文实例为大家分享了vue实现选择商品规格功能的具体代码,供大家参考,具体内容如下
要实现上图的效果,代码如下:
<template>
?? ??? ?<div class="popupSub">已选规格:{{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: [] // 是否选中 因为不确定是多规格还是但规格,所以这里定义数组来判断
?? ??? ?}
?? ?},
?? ?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 = []; // 定义数组存储被选中的值
?? ? ? ? ?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; // 还原,目的是记录点下去那个值,避免下一次执行循环时避免被覆盖
?? ? ? ? ?}
?? ? ? ? ?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>
这只是简单的实现选择规格,如果一开始后台有返回数据,还需要定义一个接收数据的对象。
来源:https://blog.csdn.net/qq_43146962/article/details/104845904
标签:vue,选择,商品
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅谈django 模型类使用save()方法的好处与注意事项
2021-05-17 10:43:59
Python使用pickle模块报错EOFError Ran out of input的解决方法
2021-06-03 02:56:57
tensorflow实现残差网络方式(mnist数据集)
2023-08-10 08:33:31
![](https://img.aspxhome.com/file/2023/5/66915_0s.jpg)
Python实现Kerberos用户的增删改查操作
2023-07-29 09:39:52
![](https://img.aspxhome.com/file/2023/3/61483_0s.jpg)
页面表达常用方式
2010-05-27 12:42:00
![](https://img.aspxhome.com/file/UploadPic/20105/27/01-93s.jpg)
WEB2.0网页制作标准教程(3)定义语言编码
2007-11-13 13:23:00
windows下python虚拟环境virtualenv安装和使用详解
2023-08-30 11:06:39
pytorch实现Tensor变量之间的转换
2023-06-28 15:41:31
ASP.NET对SQLServer的通用数据库访问类
2024-01-23 19:21:20
python查询MySQL将数据写入Excel
2023-12-15 13:48:11
![](https://img.aspxhome.com/file/2023/8/70868_0s.png)
浅谈vue websocket nodeJS 进行实时通信踩到的坑
2024-04-10 13:46:38
![](https://img.aspxhome.com/file/2023/2/139692_0s.jpg)
Node.js(安装,启动,测试)
2024-05-13 09:29:28
![](https://img.aspxhome.com/file/2023/9/125729_0s.png)
Python绘制雷达图时遇到的坑的解决
2021-05-18 00:09:03
![](https://img.aspxhome.com/file/2023/2/99192_0s.jpg)
python黑魔法之编码转换
2022-08-11 10:03:41
django ModelForm修改显示缩略图 imagefield类型的实例
2021-04-28 16:16:37
用WEB(ASP)方式实现SQL SERVER 数据库的备份和恢复
2010-05-11 20:12:00
Python使用爬虫猜密码
2023-03-21 12:38:39
![](https://img.aspxhome.com/file/2023/6/67766_0s.png)
python中urlparse模块介绍与使用示例
2021-08-08 10:13:18
JS小游戏之仙剑翻牌源码详解
2024-04-17 09:42:04
![](https://img.aspxhome.com/file/2023/4/136774_0s.jpg)
python字符串切割:str.split()与re.split()的对比分析
2022-08-09 20:57:13