1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
<template>
<div class="goods-specifications">
<div class="spe-con" v-for="(speItem,index) in speData" :key="index">
<div class="el-form-item-h" >
<label class="el-form-item-h__label">规格名称:</label>
<div class="el-form-item-h__content">
<div>
<el-select
:ref="'speNameDom' + index "
v-model="speItem.spec_name"
size="small"
style="width: 300px"
filterable
default-first-option
:clearable="true"
@change="speNameChange(speItem.spec_name,index)"
@visible-change='bv=> visibleChange(bv,"speNameDom",index)'
>
<el-option
v-for="(itemSN,indexSN) in speOptions"
:key="itemSN.id"
:label="itemSN.name"
:value="itemSN.name"
>
<span style="float: left" class="span-style">{{ itemSN.name }}</span>
<div class="flag">
<svg-icon icon="edit" iconClass="template_edit_style" @click="addSpeName(index)"/>
</div>
</el-option>
</el-select>
<!--<span>数据填写不完整</span>-->
</div>
<div class="spe-params-con">
<div style="display: inline-block" v-for="(itemSV,indexSV) in speItem.spec_value" :key="indexSV">
<el-input v-model="itemSV.value" size="small" placeholder="请输入规格参数" class="spe-params-input-item" @blur="paramNameInputBlur(index,indexSV)">
<i v-if="indexSV !== 0" slot="suffix" class="el-input__icon el-icon-delete el-icon-delete-h" @click="deleteSpeParam(index,indexSV)"></i>
</el-input>
</div>
<el-button type="primary" plain class="el-icon-plus" size="mini" @click="addSpeParam(index)">添加规格参数</el-button>
</div>
</div>
</div>
<div style="margin-right:auto;" v-if="(speData.length > 1) && speData.length !== 1">
<el-button type="danger" plain icon="el-icon-delete" size="mini" @click="deleteSpe(index)">删除该规格</el-button>
</div>
</div>
<el-button @click="addSpe" class="el-icon-plus" size="mini">添加规格</el-button>
<!-- 添加 规格名称 -->
<el-dialog
title="添加规格名称"
:visible.sync="addSpecNameDialog"
:destroy-on-close="true"
:before-close="cancelSpec"
width="500px"
center
>
<div>
<p class="add-spec-dialog-con">
<span style="margin-right:30px;">输入规格名称:</span>
<span>
<el-input v-model="specName_sm" placeholder="请输入规格名称" size="small" style="width: 280px;"></el-input>
</span>
</p>
</div>
<span slot="footer" class="dialog-footer">
<el-button @click="cancelSpec" size="mini">取 消</el-button>
<el-button type="primary" @click="saveSpecName" size="mini">确 定</el-button>
</span>
</el-dialog>
</div>
</template>
<script>
import descartes from "@/utils/dikaerjs.js";
import { UploadImg } from '@/api/module/goods'
export default {
name: "GoodsSpecifications",
props: {
specificationsdata: {
type: Array,
required: true
}
},
data() {
return {
speData: [], // 商品规格总数据
specsGroup: {
spec_name: '', // 规格名称
spec_value: []
},
speParamName:{
value: ''
},
speOptions: [{
id: '1',
name: '颜色'
}, {
id: '2',
name: '尺码'
}],
localSpeOptions: [],
// 规格名称,value组成的数组
speNameList: [],
addSpecNameDialog: false,
specName_sm: '',
};
},
created() {
this.$set(this.specsGroup.spec_value,this.specsGroup.spec_value.length,this.speParamName);
this.speData = [];
this.$set(this.speData,this.speData.length,this.specsGroup);
},
mounted() {
/** 先获取 本地存储的localStorage **/
this.getLocalSpecNameOption();
// this.speData = [];
// this.$set(this.speData,this.speData.length,this.specsGroup);
// console.log(123,this.specificationsdata)
this.speData = this.specificationsdata;
this.$forceUpdate()
},
watch:{
// "speData.specsGroup": {
// handle: function(val) {
// console.log(22222,val);
// }
// }
},
methods: {
/** 添加规格*/
addSpe() {
let specsGroup = {
spec_name: '', // 规格名称
spec_value: [{value: ''}]
};
this.$set(this.speData,this.speData.length,specsGroup);
},
/** 删除 规格数据 */
deleteSpe(index) {
this.$confirm('是否删除当前规格的所有数据?', '警告', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(async() => {
this.speData.splice(index,1);
this.specCalcResult();
}).catch(() => {})
},
//
/** 规格名称,值改变时, 此组件 lable 和 value 绑定同一个值 */
speNameChange(val,index) {
if( val ) {
// 当有多个值的时候,才校验,规格名称是否重复
if( this.speData.length > 1 ) {
let coypSpeData = [];
coypSpeData = this.speData.concat();
coypSpeData.splice(index,1);
for(let i = 0; i < coypSpeData.length; i++) {
if( val === coypSpeData[i].spec_name ) {
this.speData[index].spec_name = '';
this.$message({type: 'error',message: '此规格名称已经使用,请重新选取或新增其他规格名称'});
break;
}
}
}
}
// 重新 计算 规格组合数据
this.specCalcResult();
},
// 添加规格参数
addSpeParam(index) {
let valueObj = { value: ''}
this.$set(this.speData[index].spec_value,this.speData[index].spec_value.length,valueObj);
},
/** 删除 规格参数 */
deleteSpeParam(index,indexSV) {
// 先判断,删除前是否有值,如果是空,则没有必要再次计算
if( this.speData[index].spec_value[indexSV].value ) {
this.speData[index].spec_value.splice(indexSV,1);
this.specCalcResult();
}else {
this.speData[index].spec_value.splice(indexSV,1);
}
},
/**
* 规格名称 下拉框 出现/隐藏 触发
* 为element-ui的Select和Cascader添加弹层底部操作按钮
* @param visible
* @param refName 设定的ref名称
* @param index 保证操作是当前的下拉框
* @param addname 底部按钮名称, 可不用
*/
visibleChange(visible, refName, index) {
let _this = this;
let realRefName = refName + index;
//console.log(realRefName);
if(visible) {
// 动态绑定 ref的值的时候,下面的dom需要 加一个 [0],非常重要
const speNameDom = _this.$refs[realRefName][0];
let popper = speNameDom.$refs.popper;
if( popper.$el ) {
popper = popper.$el;
}
if(!Array.from(popper.children).some(v => v.className === 'el-template-menu__list')) {
const el = document.createElement('ul');
el.className = 'el-template-menu__list';
el.style = 'border-top\:1px solid rgb(219 225 241)\; padding\:0\;margin\:0\; color\:rgb(64 158 255)\;font-size\: 14px';
el.innerHTML = '<li class="el-cascader-node text-center" style="height:40px;line-height: 40px">'
+ '<span class="el-cascader-node__label"><i class="el-icon-plus"></i>添加规格名称</span>'
+ '</li>';
popper.appendChild(el)
el.onclick = () => {
// 底部按钮的点击事件
_this.addSpeName(index);
// localSttorage 存储 下拉数据
if(speNameDom.toggleDropDownVisible) {
speNameDom.toggleDropDownVisible(false);
} else {
speNameDom.visible = false;
}
}
}
}
},
getLocalSpecNameOption() {
//console.log("先获取:",window.localStorage.getItem('localSpeOptions'));
if(!window.localStorage.getItem('localSpeOptions')) {
this.speOptions = [];
}else {
let options = JSON.parse(window.localStorage.getItem('localSpeOptions'));
this.speOptions = options;
}
},
/* 增加 规格名称 */
addSpeName(index) {
this.addSpecNameDialog = true;
},
// 添加规格名称,对话框的退出
cancelSpec() {
this.specName_sm = '';
this.addSpecNameDialog = false;
},
// 规格名称,保存
saveSpecName() {
let localObj = {
id: 0,
name: ''
};
let localArr = [];
let localJSON = '';
if(this.speOptions.length === 0) {
let localObj = {
id: 0,
name: this.specName_sm
};
localArr.push(localObj);
localJSON = JSON.stringify(localArr);
}else {
let localObj = {
id: this.speOptions.length,
name: this.specName_sm
};
this.speOptions.push(localObj);
localJSON = JSON.stringify(this.speOptions)
}
window.localStorage.setItem('localSpeOptions',localJSON)
this.addSpecNameDialog = false;
this.$message({type:'success',message:'新增成功'});
this.getLocalSpecNameOption();
},
/** 规格参数 失焦时,进行笛卡尔积算法,并渲染 商品售价 */
paramNameInputBlur(index,indexSV) {
// // 如果失焦的input中有值,才再次计算
// if( this.speData[index].spec_value[indexSV].value ) {
this.specCalcResult();
// }
},
/** 规格参数 变化时,对数据进行处理后,再进行笛卡尔积算法,最终计算出商品售价的数据 */
specCalcResult() {
// 商品规格原始数据
let orgSpeData = this.deepClone(this.speData);
// 进行数据处理后的数据,
let proSpeData = this.processData(orgSpeData);
// 需要将 所有规格名称,提出一个数组集合,
this.speNameList = [];
orgSpeData.forEach((item,index)=> {
if( item.spec_name ) {
let obj = { spec_name: '' };
// 注意, item.spec_value是否一定为一个长度大于 1 的数组
if( item.spec_value.length > 0) {
for(let i = 0; i < item.spec_value.length; i++) {
if( item.spec_value[i].value ) {
obj.spec_name = item.spec_name;
this.speNameList.push(obj);
break;
}
}
}
}
})
// console.log("处理后的数据",proSpeData);
// console.log("处理后的nameList",this.speNameList);
// 当处理后的数据,是一个空数组,则不需要再进行 笛卡尔积 计算,可以直接给出计算结果
if (proSpeData.length === 0) {
let goodsPam = {
pamNameList: [],
pamDataList: [],
pamTableList: [],
};
this.$emit('calcSpeTable', goodsPam);
} else {
let goodsParametersList = this.cartesian(proSpeData);
let goodsPam = {
pamNameList: [],
pamDataList: [],
pamTableList: [],
};
goodsPam.pamNameList = this.speNameList;
goodsPam.pamDataList = goodsParametersList;
goodsParametersList.forEach((item)=> {
let prePamTable = [
{ js_price:'', sl_price:'', price:'', sc_price:'', stock:'', weight:'', thumb:'',spec_values: [] }
];
goodsPam.pamTableList.push(prePamTable)
});
this.$emit('calcSpeTable',goodsPam);
}
},
/** 数据处理为,二维数组,供笛卡尔积算法方法使用,对空数据进行过滤 */
processData(list) {
let result = [];
if (list && list.length > 0) {
for (let i = 0; i < list.length; i++) {
let childList = [];
if(list[i].spec_name) {
for(let j = 0; j < list[i].spec_value.length; j++) {
if(list[i].spec_value[j].value) {
childList.push(list[i].spec_value[j].value);
}
}
}
if(childList.length > 0) {
result.push(childList);
}
}
}
return result;
},
/** 笛卡尔积算法 */
cartesian(arr) {
if(arr.length < 2) {
return arr[0] || [];
}else {
return [].reduce.call(arr, function(col, set) {
let res = [];
col.forEach( c => {
set.forEach(s => {
let t = [].concat(Array.isArray(c) ? c : [c]);
t.push(s);
res.push(t);
});
});
return res
});
}
}
}, // methods end
};
</script>
<style scoped>
.goods-specifications {
padding: 15px 0;
margin-top: 18px;
}
.spe-con {
display: flex;
justify-content: flex-start;
}
/* 仿 element 的from-item 样式,注意,size为 samll */
.el-form-item-h {
width:80%;
margin: 0 20px 20px 0;
padding: 20px 0 0;
background: #F7F8FA;
border: 1px solid #E5E5E5;
border-radius: 2px;
}
.el-form-item-h__label {
width: 150px;
text-align: right;
vertical-align: middle;
float: left;
font-size:14px;
color: #606266;
line-height: 32px;
padding: 0 12px 0 0;
box-sizing: border-box;
}
/*.el-form-item-h__label:before {*/
/* content: '';*/
/* color: #ff4949;*/
/* margin-right: 4px;*/
/*}*/
.el-form-item-h__content {
margin-left: 150px;
position: relative;
font-size: 14px;
}
.spe-params-con {
margin: 20px 0;
}
/* 商品规格 - 规格名称 - 参数input */
.spe-params-input-item {
width: 150px;
margin: 0 10px 20px 0;
}
.el-icon-delete-h:hover {
font-size: 14px;
color: #ff4949;
cursor: pointer;
}
.add-spec-dialog-con {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
</style>