select-category.vue
1.51 KB
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
<template>
<Cascader :data="categoryList" :value="handleValue" change-on-select @on-change="selectChange"></Cascader>
</template>
<script>
import ApiService from 'service/api'
export default {
props: {
value: {
type: Array,
default() {
return []
}
}
},
data() {
let self = this
return {
handleValue: self.value.map(i => i.value),
categoryList: []
}
},
created() {
this.api = new ApiService()
this.getSortList()
},
methods: {
getSortList() {
this.api.getSortList().then(result => {
this.categoryList = result.list.map(i => {
let sortItem = {
value: i.id,
label: i.sortName
}
if (i.subList) {
sortItem.children = i.subList.map(s => {
return {
value: s.id,
label: s.sortName
}
})
}
return sortItem
})
})
},
selectChange(val) {
const len = val.length
const max = val[0] || ''
const mid = val[1] || ''
let maxItem = this.categoryList.find(i => i.value == max) || {}
let midItem = maxItem && maxItem.children && maxItem.children.find(i => i.value == mid) || {}
this.$emit('input', [
{value: max, label: maxItem.label || ''},
{value: mid, label: midItem.label || ''}
])
}
},
watch: {
value(newValue) {
this.handleValue = newValue.map(v => v.value)
}
}
}
</script>
<style>
</style>