TwoLevelSelectComponent.vue
4.33 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
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
<template>
<view class="two-level-select-box">
<view
class="select"
@click.stop="isSelect = !isSelect"
>
{{ currentLabel }}
<view class="sprite-icon icon-arrow-bottom"></view>
</view>
<view
class="pop-box"
v-if="isSelect"
>
<!-- 一级菜单 -->
<view
v-for="(item, index) in selectList"
:key="index"
class="type-item flex"
:class="getItemClass(index, item)"
@click="chooseFirstLevel(index, item)"
>
<view class="item-label">{{ item[labelKey] }}</view>
</view>
<!-- 二级菜单 -->
<view
class="second-level-box"
v-if="showSecondLevel && currentFirstLevel && currentFirstLevel.children"
>
<view
v-for="(child, childIndex) in currentFirstLevel.children"
:key="childIndex"
class="type-item flex"
:class="childIndex === selectSecondIndex ? 'active' : ''"
@click.stop="chooseSecondLevel(childIndex, child)"
>
{{ child[labelKey] }}
</view>
</view>
</view>
</view>
</template>
<script>
export default {
props: {
selectList: Array,
labelKey: {
type: String,
default: 'label'
}
},
name: 'TwoLevelSelectComponent',
data() {
return {
selectFirstIndex: 0,
selectSecondIndex: -1,
isSelect: false,
showSecondLevel: false,
currentFirstLevel: null
}
},
computed: {
currentLabel() {
const firstItem = this.selectList[this.selectFirstIndex]
if (!firstItem) return ''
// 如果有选中的二级分类
if (this.selectSecondIndex >= 0 && firstItem.children && firstItem.children[this.selectSecondIndex]) {
return firstItem.children[this.selectSecondIndex][this.labelKey]
}
// 否则显示一级分类
return firstItem[this.labelKey]
}
},
methods: {
getItemClass(index, item) {
const classes = []
if (index === this.selectFirstIndex && this.selectSecondIndex === -1) {
classes.push('active')
}
if (item.children && item.children.length) {
classes.push('has-children')
}
return classes.join(' ')
},
hideSelect() {
this.isSelect = false
this.showSecondLevel = false
},
chooseFirstLevel(index, item) {
// 如果有二级菜单,显示二级菜单
if (item.children && item.children.length) {
this.showSecondLevel = true
this.currentFirstLevel = item
this.selectFirstIndex = index
} else {
// 没有二级菜单,直接选中
this.selectFirstIndex = index
this.selectSecondIndex = -1
this.isSelect = false
this.showSecondLevel = false
this.$emit('chooseType', { item, index, level: 1 })
}
},
chooseSecondLevel(childIndex, child) {
this.selectSecondIndex = childIndex
this.isSelect = false
this.showSecondLevel = false
this.$emit('chooseType', {
item: child,
index: childIndex,
parentIndex: this.selectFirstIndex,
parent: this.currentFirstLevel,
level: 2
})
}
}
}
</script>
<style scoped lang='scss'>
.two-level-select-box {
position: relative;
z-index: 5;
.select {
display: flex;
align-items: center;
justify-content: flex-end;
height: toRpx(84);
border-radius: toRpx(16);
opacity: 1;
background: #f2f2f2;
padding: 0 toRpx(24) 0 toRpx(40);
}
.pop-box {
position: absolute;
top: toRpx(100);
left: 0;
width: 100%;
background: #ffffff;
box-shadow: 0 0 toRpx(16) 0 #99999933;
border-radius: toRpx(16);
padding: toRpx(16) 0;
text-align: center;
animation: show-view-opacity 0.15s forwards;
.type-item {
padding: 0 toRpx(28);
margin-bottom: toRpx(8);
height: toRpx(84);
justify-content: center;
align-items: center;
&:last-child {
margin-bottom: 0;
}
&.active {
background-color: #ecf1ff;
}
}
}
.second-level-box {
position: absolute;
top: 0;
left: 100%;
width: 100%;
background: #ffffff;
box-shadow: 0 0 toRpx(16) 0 #99999933;
border-radius: toRpx(16);
padding: toRpx(16) 0;
.type-item {
justify-content: center;
}
}
}
</style>