vue商品多规格sku前端选择逻辑算法

先看看sku的多规格选择演示。

我们需要的是再后台上传库存规格和所属规格来进行判断,需要再不同属性间来回切换。

全幼儿园最聪明我在这借鉴全幼儿园最聪明sku的算法并加以更改,以自己的理解来记录一下。

我的理解是先创建一个空白的矩阵,x轴和y轴是各个规格按顺序排列。

initEmptyAdjMatrix() {
				this.productSpecValueVo.forEach((prop) => {
					prop.mainImgSpecsVOS.forEach((attr) => {
						this.list.push(attr.specsValueName)
						attr.isActive = false
						attr.isDisabled = false
					})
				})
				for (let i = 0; i < this.list.length; i++) {
					this.listindex[i] = new Array(this.list.length).fill(0);
				}
				console.log(this.listindex);
			}

 我在其中还绑定了相应的状态维护字段,用来判断状态,这是其结果表格:

红色绿色11寸12寸13寸
红色00000
绿色00000
11寸00000
12寸00000
13寸00000

在此基础上,比如红色,11寸,我会将其id填入对应的表格,以下是我排列的函数和结果:

 以下是具体方法:

// 将规格属性组中的属性在无向图中联系起来
			associateAttributes(attributes, skuId) {
				attributes.forEach((attr1) => {
					attributes.forEach((attr2) => {
						// 因 spec 与 sku 数据结构不一致,需作处理
						if (attr1 !== attr2 || attr1.specsValueName !== attr2.specsValueName) {
							if (attr1.specsValueName && attr2.specsValueName) {
								attr1 = attr1.specsValueName;
								attr2 = attr2.specsValueName;
							}
							//对三个以上的规格进行处理
							const value1 = attr1.specsValueName ? attr1.specsValueName : attr1;
							const value2 = attr2.specsValueName ? attr2.specsValueName : attr2;
							const index1 = this.list.indexOf(value1);
							const index2 = this.list.indexOf(value2);
							if (index1 > -1 && index2 > -1) {
								if (this.listindex[index1][index2]) {
									this.listindex[index1][index2].add(skuId);
								} else {
									this.listindex[index1][index2] = new Set([skuId]);
								}
							}
						}
					});
				});
			},
			setAdjMatrixValue() {
				this.productSkuVOS.forEach((sku) => {
					this.associateAttributes(sku.skuAttribute, sku.skuId);
				});
				this.productSpecValueVo.forEach((prop) => {
					this.associateAttributes(prop.mainImgSpecsVOS, '99');
				});
			},

数据填入后我们需要将点击的逻辑写入:

// 触发函数,传入规格类下标和规格下标
			choose(propertyIndex, attributeIndex) {
				const attr = this.productSpecValueVo[propertyIndex].mainImgSpecsVOS[attributeIndex]
				// 重置每个规格的 isActive 状态
				const isActive = !attr.isActive;
				this.productSpecValueVo[propertyIndex].mainImgSpecsVOS[attributeIndex].isActive =
					isActive;
				if (isActive) {
					this.productSpecValueVo[propertyIndex].mainImgSpecsVOS.forEach((attr, index) => {
						if (index !== attributeIndex) {
							attr.isActive = false;
						}
					});
				}

				// 维护当前已选的规格列表
				this.selected = [];
				this.productSpecValueVo.forEach((prop) => {
					prop.mainImgSpecsVOS.forEach((attr) => {
						if (attr.isActive) {
							this.selected.push(attr.specsValueName);
						}
					});
				});

				// 重置每个规格的禁用状态
				this.productSpecValueVo.forEach((prop) => {
					prop.mainImgSpecsVOS.forEach((attr) => {
						attr.isDisabled = !this.canAttributeSelect(attr);
					});
				});

				//判断选择是否是主键并更新图片

				this.productSpecValueVo[0].mainImgSpecsVOS.map((item1, index1) => {
					if (Object.is(item1.specsValueName, this.selected[0])) {
						this.default_data = item1
					}
				})

				//判断是否选择完全
				if (this.selected.length == this.productSpecValueVo.length) {
					this.productSkuVOS.map((item, index) => {
						if (JSON.stringify(item.skuAttribute) === JSON.stringify(this.selected)) {
							this.selected_id = item.skuId
							this.judge = false
							this.tiptitle = this.shopCarJudge ? '添加购物车' : '下单'
							this.price = item.price
						}
					})

				} else {
					this.judge = true
					this.tiptitle = '请选择规格'
				}
			},
            canAttributeSelect(attribute) {
				//判断选择情况
				if (!this.selected || !this.selected.length || attribute.isActive) {
					return true;
				}
				let res = [];
				this.selected.forEach((value) => {
					const index1 = this.list.indexOf(value);
					const index2 = this.list.indexOf(attribute.specsValueName);
					res.push(this.listindex[index1][index2]);
				});
				if (res.some((item) => (item === 0))) {
					return false;
				} else if (res.some((item) => (item.has('99')))) {
					return true;
				} else {
					const first = res[0];
					const others = res.slice(1);
					return Array.from(first).some((skuId) => (others.every((item) => (item.has(skuId)))));
				}
			},

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
根据引用内容,可以得知在Vue3中处理商品规格SKU的问题时,首先需要考虑将商品的所有规格渲染上去,并且可以根据数据得知该商品有三种规格:颜色、运行内存和存储。 在判断商品规格SKU的库存时,需要注意每点击一个规格属性,都要去判断其他规格属性是否有库存。这可以通过选中完整的规格信息来向父组件传递有效的数据,包括SKU的ID、价格、原价格、库存以及商品的说明等。 因此,在Vue3中处理商品规格SKU的方式可以是通过渲染所有规格属性,并在选中完整的规格信息后,通过传递有效数据来判断SKU的库存情况。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [基于Vue3 Sku的设计](https://blog.csdn.net/qq_43817005/article/details/121889677)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [vue电商项目sku 规格 详细步骤](https://blog.csdn.net/m0_46846526/article/details/119142417)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值