-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.html
140 lines (133 loc) · 4.15 KB
/
index.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>sku</title>
<style>
.selected {
color: red;
}
</style>
</head>
<body>
<div id="app">
<h3>预览</h3>
<sku :keys="keys" :values="paths"></sku>
<hr>
<h3>数据(点击删除)</h3>
<div v-for="(title, i) in titles">
<span style="cursor: pointer;" @click="delKey(i)">{{ title }}</span>:
<span>
<button @click="delOption(i, j)" v-for="(name, j) in options[i]">
{{ name }}
</button>
<a @click="addOption(i)" style="cursor: pointer;">添加值</a>
</span>
</div>
<a style="cursor: pointer;" @click="addKey">添加属性</a>
<hr>
<h3>价格库存配置</h3>
<table>
<tr>
<th>路径</th>
<th v-for="title in titles">{{ title }}</th>
<th>价格</th>
<th>库存</th>
</tr>
<tr v-for="path in paths">
<td>{{ path.symbols.join(SKU_SEP) }}</td>
<td v-for="opt in path.values">
{{ opt }}
</td>
<td>
<input v-model="path.price" type="number" step="0.01"/>
</td>
<td>
<input v-model="path.stock" type="number"/>
</td>
</tr>
</table>
</div>
<script src="js/vue.js"></script>
<script src="js/sku_component.js"></script>
<script>
Vue.config.devtools = true
let vue = new Vue({
data: {
titles: ['颜色', '内存', '运营商'],
options: [['白色', '黑色', '金色'], ['128g', '64g', '32g', '16g'], ['电信', '联通', '移动']],
SKU_SEP,
},
components: {
Sku,
},
computed: {
keys() {
let keys = {}
let symbol = 0
for (let i in this.titles) {
let option = {}
for (let j in this.options[i]) {
option[this.options[i][j]] = symbol++
}
keys[this.titles[i]] = option
}
return keys
},
paths() {
let arr = this.titles.map((v, k) => this.options[k].map((l, k) => [this.keys[v][l], l, v,]))
let path = [], paths = {}, len = arr.length
let func = (arr, n) => {
for (let i of arr[n]) {
path.push(i)
if (n !== len - 1) {
func(arr, n + 1)
} else {
paths[path.map(v => v[0]).sort().join(SKU_SEP)] = {
stock: 1,
price: 1,
symbols: path.map(v => v[0]),
values: path.map(v => v[1]),
titles: path.map(v => v[2]),
}
}
path.pop()
}
}
func(arr, 0)
return paths
},
},
methods: {
addOption(i) {
let name = prompt('请输入属性名')
if (name) {
this.options[i].push(name)
}
},
delOption(i, j) {
if (confirm('确定要删除?')) {
this.options[i].splice(j, 1)
}
},
delKey(index) {
if (confirm('确定要删除?')) {
this.titles.splice(index, 1)
this.options.splice(index, 1)
}
},
addKey() {
let name = prompt('请输入标题名')
if (name) {
this.titles.push(name)
this.options.push([])
}
},
},
}).$mount("#app")
</script>
</body>
</html>