Skip to content

Commit ab1120b

Browse files
author
puhui999
committed
商品管理: 完成商品属性排列组合算法生成对应表数据
1 parent 35c3545 commit ab1120b

File tree

1 file changed

+67
-30
lines changed
  • src/views/mall/product/management/components/SkuList

1 file changed

+67
-30
lines changed

src/views/mall/product/management/components/SkuList/index.vue

Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@
55
<UploadImg v-model="row.picUrl" height="80px" width="100%" />
66
</template>
77
</el-table-column>
8-
<template v-if="formData.specType">
8+
<template v-if="formData.specType && !isBatch">
99
<!-- 根据商品属性动态添加 -->
1010
<el-table-column
1111
v-for="(item, index) in tableHeaderList"
1212
:key="index"
1313
:label="item.label"
14-
:prop="item.prop"
1514
align="center"
1615
min-width="120"
17-
/>
16+
>
17+
<template #default="{ row }">
18+
{{ row.properties[index].value }}
19+
</template>
20+
</el-table-column>
1821
</template>
1922
<el-table-column align="center" label="商品条码" min-width="120">
2023
<template #default="{ row }">
@@ -143,17 +146,77 @@ watch(
143146
immediate: true
144147
}
145148
)
149+
/** 生成表数据 */
150+
const generateTableData = (data: any[]) => {
151+
// 构建数据结构
152+
const propertiesItemList = []
153+
for (const item of data) {
154+
const objList = []
155+
for (const v of item.values) {
156+
const obj = { propertyId: 0, valueId: 0, value: '' }
157+
obj.propertyId = item.id
158+
obj.valueId = v.id
159+
obj.value = v.name
160+
objList.push(obj)
161+
}
162+
propertiesItemList.push(objList)
163+
}
164+
build(propertiesItemList).forEach((item) => {
165+
const row = {
166+
properties: [],
167+
price: 0,
168+
marketPrice: 0,
169+
costPrice: 0,
170+
barCode: '',
171+
picUrl: '',
172+
stock: 0,
173+
weight: 0,
174+
volume: 0
175+
}
176+
if (Array.isArray(item)) {
177+
row.properties = item
178+
} else {
179+
row.properties.push(item)
180+
}
181+
formData.value.skus.push(row)
182+
})
183+
}
184+
/** 构建所有排列组合 */
185+
const build = (list: any[]) => {
186+
if (list.length === 0) {
187+
return []
188+
} else if (list.length === 1) {
189+
return list[0]
190+
} else {
191+
const result = []
192+
const rest = build(list.slice(1))
193+
for (let i = 0; i < list[0].length; i++) {
194+
for (let j = 0; j < rest.length; j++) {
195+
// 第一次不是数组结构,后面的都是数组结构
196+
if (Array.isArray(rest[j])) {
197+
result.push([list[0][i], ...rest[j]])
198+
} else {
199+
result.push([list[0][i], rest[j]])
200+
}
201+
}
202+
}
203+
return result
204+
}
205+
}
146206
/** 监听属性列表生成相关参数和表头 */
147207
watch(
148208
() => props.attributeList,
149209
(data) => {
210+
// 如果不是多规格则结束
211+
if (!formData.value.specType) return
212+
// 如果当前组件作为批量添加数据使用则结束
213+
if (props.isBatch) return
150214
// 判断代理对象是否为空
151215
if (JSON.stringify(data) === '[]') return
152216
// 重置表头
153217
tableHeaderList.value = []
154218
// 重置表数据
155219
formData.value!.skus = []
156-
SkuData.value = []
157220
// 生成表头
158221
data.forEach((item, index) => {
159222
// name加属性项index区分属性值
@@ -166,30 +229,4 @@ watch(
166229
immediate: true
167230
}
168231
)
169-
/** 生成表数据 */
170-
const generateTableData = (data: any[]) => {
171-
// const row = {
172-
// price: 0,
173-
// marketPrice: 0,
174-
// costPrice: 0,
175-
// barCode: '',
176-
// picUrl: '',
177-
// stock: 0,
178-
// weight: 0,
179-
// volume: 0
180-
// }
181-
// 先把所有的属性值取出来
182-
const newDataList: any[] = []
183-
for (const index in data) {
184-
newDataList.push(data[index].values)
185-
}
186-
console.log(newDataList)
187-
}
188-
// const buildRow = (list: any[]) => {
189-
// for (const index in data) {
190-
// for (const index1 of data[index].values) {
191-
// row[`name${index1}`] = data[index].values[index1]
192-
// }
193-
// }
194-
// }
195232
</script>

0 commit comments

Comments
 (0)