index.vue
7.56 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
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
<!--
* @Author: your name
* @Date: 2022-03-07 14:32:50
* @LastEditTime: 2025-03-11 17:12:12
* @LastEditors: 'wangzhihui' '1733904062@qq.com'
* @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
* @FilePath: \scrm-micro-settings\src\views\baseInfo\editInfo.vue
-->
<template>
<div v-loading="loading">
<div class="select-input-box" @click="switchBrand">
<div>
<template v-for="(item, idx) in getCheckedArr">
<el-tag effect="plain" type="info" :key="idx" closable :disable-transitions="false" @close="removeItem(item, idx)" v-if="idx < 3">
{{ item.label }}
</el-tag>
</template>
<el-popover placement="top" width="600" trigger="hover" class="select-popover">
<div class="select-popover-box">
<el-tag effect="plain" class="popover-item" v-for="(item, idx) in getCheckedArr" type="info" :key="idx" closable :disable-transitions="false" @close="removeItem(item, idx)">
{{ item.label }}
</el-tag>
</div>
<el-tag effect="plain" slot="reference" type="info" v-show="getCheckedArr.length > 3">+ {{ getCheckedArr.length - 3 }}</el-tag>
</el-popover>
</div>
<div>
<i v-show="!brandTree" class="el-icon-arrow-up" style="color:#909399"></i>
<i v-show="brandTree" class="el-icon-arrow-down" style="color:#909399"></i>
</div>
</div>
<div class="tree-box" v-show="brandTree">
<el-input class="tree-input" prefix-icon="el-icon-search" placeholder="输入关键字进行过滤" v-model="filterText"></el-input>
<el-tree class="filter-tree" @check="handleCheck" :data="brandListTree" :props="defaultProps" node-key="id" show-checkbox :filter-node-method="filterNode" ref="tree"></el-tree>
<div class="tree-footer">
<div class="tree-footer-checkbox">
<el-checkbox v-model="checkedAll" @change="selectAll">全部</el-checkbox>
</div>
<div class="tree-footer-reset" @click="selectClear">重置</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'SelectBrand',
data() {
return {
loading: false,
checkedAll: false,
filterText: '',
treeArr: [],
getCheckedArr: [],
defaultProps: {
children: 'children',
label: 'label'
},
brandTree: false
}
},
props: {
brandListTree: {
type: Array
}
},
watch: {
filterText(val) {
this.$refs.tree.filter(val)
},
treeArr: {
handler() {
const getCheckedNodes = this.$refs.tree.getCheckedNodes()
this.getCheckedArr = getCheckedNodes.reduce((prev, cur) => {
if (Object.keys(cur).includes('children') && cur.children && cur.children.length > 0) {
return prev
}
return prev.concat(cur)
}, [])
},
deep: true
}
},
methods: {
getMerchantBrandList() {
let getCheckedNodes = this.$refs.tree.getCheckedNodes()
const getCheckedKeys = this.$refs.tree.getCheckedKeys()
const getHalfCheckedNodes = this.$refs.tree.getHalfCheckedNodes()
let merchantBrandList = getHalfCheckedNodes.map(car => {
let merchantBrandId = []
car.children.forEach(item => {
if (getCheckedKeys.includes(item.id)) {
merchantBrandId.push(item.id)
}
})
return {
cars: car.id,
merchantBrandId
}
})
let checkNodes = getCheckedNodes.filter(item => item.children)
let noCheckNodes = getCheckedNodes
.filter(item => item.children === null)
.map(i => {
return {
cars: i.id
}
})
let merchantBrandList2 = checkNodes.map(car => {
let merchantBrandId = []
car.children.forEach(item => {
if (getCheckedKeys.includes(item.id)) {
merchantBrandId.push(item.id)
}
})
return {
cars: car.id,
merchantBrandId
}
})
return merchantBrandList.concat(merchantBrandList2).concat(noCheckNodes)
},
//勾选
handleCheck() {
this.treeArr = this.getTreeArray()
if (this.$refs.tree.getCheckedKeys().length == 0) {
this.checkedAll = false
}
if (this.$refs.tree.getCheckedKeys().length == this.treeArr.length) {
this.checkedAll = true
}
},
getCheckList(checkArr = []) {
checkArr.forEach(item => (item = parseInt(item)))
this.$refs.tree.setCheckedKeys(checkArr)
this.$nextTick(() => {
this.handleCheck()
})
},
//获取选择项
getTreeArray() {
let tree_arr = []
this.brandListTree.forEach(k => {
let child = k.children
if (child && child.length > 0) {
child.forEach(j => {
tree_arr.push(j.id)
})
} else {
tree_arr.push(k.id)
}
})
return tree_arr
},
//全选
selectAll() {
this.loading = true
this.treeArr = this.getTreeArray()
if (this.getCheckedArr.length == this.treeArr.length) {
this.checkedAll = false
this.$refs.tree.setCheckedKeys([])
} else {
this.checkedAll = true
this.$refs.tree.setCheckedKeys(this.treeArr)
}
setTimeout(() => {
this.loading = false
}, 1000)
},
//重置
selectClear() {
this.treeArr = []
this.checkedAll = false
this.$refs.tree.setCheckedKeys([])
},
//删除
removeItem(checkItem, index) {
this.loading = true
this.getCheckedArr.splice(index, 1)
const getCheckedNodes = this.$refs.tree.getCheckedNodes()
let getCheckedArr = getCheckedNodes.reduce((prev, cur) => {
if (Object.keys(cur).includes('children')) {
return prev
}
return prev.concat(cur)
}, [])
getCheckedArr = getCheckedArr.filter(item => {
return item.id != checkItem.id
})
this.$refs.tree.setCheckedNodes(getCheckedArr)
if (getCheckedArr.length == 0) {
this.checkedAll = false
}
this.$nextTick(() => {
this.loading = false
})
},
//过滤
filterNode(value, data) {
if (!value) return true
return data.label.indexOf(value) !== -1
},
// 显示隐藏
switchBrand() {
this.brandTree = !this.brandTree
this.$emit('clickBrand', this.brandTree)
}
}
}
</script>
<style lang="scss" scoped>
.select-input-box {
width: 400px;
// height: 38px;
background: #ffffff;
border-radius: 4px;
border: 1px solid #e5e5e5;
box-sizing: border-box;
padding: 8px;
display: flex;
justify-content: space-between;
align-items: center;
// /deep/ .el-tag {
// margin-right: 5px;
// height: 26px;
// line-height: 26px;
// box-sizing: border-box;
// }
}
.select-popover {
&-box {
display: flex;
flex-wrap: wrap;
max-height: 500px;
overflow-y: auto;
.popover-item {
margin-right: 5px;
margin-bottom: 5px;
height: 26px;
line-height: 26px;
box-sizing: border-box;
}
}
}
.tree-box {
margin-top: 10px;
padding: 15px 15px 0px;
background: #ffffff;
// box-shadow: 0px 6px 20px 0px rgba(122, 122, 122, 0.2);
border-radius: 4px;
.tree-input {
margin-bottom: 10px;
}
.filter-tree {
height: 300px;
overflow-y: auto;
}
.tree-footer {
border-top: 1px solid #e5e5e5;
height: 48px;
padding: 0px 15px;
display: flex;
justify-content: space-between;
align-items: center;
}
}
.tree-footer-reset {
cursor: pointer;
}
</style>