这篇文章主要介绍了一个基于elementUI的el-select和el-tree组合起来的下拉树形控件,支持单选和多选,暂时不支持懒加载。

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 多选
<tree-select v-model="value" multiple collapse-tags :data="options"></tree-select>

// 单选
<tree-select v-model="value" :data="options"></tree-select>

// 数据
options: [
{
label: "一级 1",
id: 1,
children: [
{
label: '二级 -1',
id: 2
}
]
}
]

完整组件

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
<template>
<el-select
ref='selTreeRef'
class="tree-select"
popper-class="tree-opt-box"
v-model='selectId'
:props='defProps'
:multiple="multiple"
:collapseTags="collapseTags"
:clearable="clearable"
:placeholder='placeholder'
@clear='handleClear'
@remove-tag="handleRemoveTag">

<!-- 设置一个空option 不然selectData为空下拉不显示 -->
<el-option hidden value="" label="" key="s1"></el-option>
<el-option hidden v-for="(item, index) in selectData" :value="item[cId]" :label="item[cLabel]" :key="'s2'+index"></el-option>

<el-tree
ref="treeRef"
:data='data'
:show-checkbox="multiple"
:node-key="cId"
:props='defProps'
:expand-on-click-node='false'
:check-on-click-node='true'
@node-click='handleNodeClick'
@check="handCheck"
>
</el-tree>
</el-select>
</template>
<script>
import {cloneDeep, castArray} from 'lodash'
export default {
name: 'TreeSelect',
props: {
defProps: {
type: Object,
default:() => {
return {
children: 'children',
label: 'label',
id: 'id'
}
}
},
value: [String, Number, Object, Array],
multiple: Boolean, // 默认 false
clearable: Boolean, // 默认 false
collapseTags: Boolean, // 默认 false
data: { // tree数据
type: Array,
default: () => {
return []
}
},
placeholder: {
type: String,
default: '请选择'
}
},
data () {
return {
selectId: '',
selectData: [],
cId: this.defProps['id'],
cLabel: this.defProps['label'],
}
},
mounted () {
this.$nextTick(_ => {
if(!this.value) return
this.setDefaultValue(this.value)
})
},
watch: {
value: function (v) {
this.setDefaultValue(v)
}
},
methods: {
/**
* @Description: 返显值
* @param {*} v
* @return {*}
*/
setDefaultValue (v) {
if (this.multiple) {
v = castArray(v)
this.$refs.treeRef.setCheckedKeys(v)
this.handCheckSetValue()
} else {
this.$refs.treeRef.setCurrentKey(v)
this.$nextTick(_ => {
this.handleNodeSetValue()
})
}
},

/**
* @Description: 单选点击事件
* @param {*} data
* @return {*}
*/
handleNodeClick (data) {
if (this.multiple) return
// 隐藏下拉框
this.$refs.selTreeRef.blur()
this.$emit('input', this.handleNodeSetValue())
},

/**
* @Description: 单选设置值
* @param {*}
* @return {*}
*/
handleNodeSetValue () {
let cNode = this.$refs.treeRef.getCurrentNode()
this.selectData = cNode ? [cNode] : []
let val = cNode ? cNode[this.cId] : null
this.selectId = val
return val
},

/**
* @Description: 多选勾选
* @param {*} curNode
* @param {*} checkedNodes
* @return {*}
*/
handCheck (curNode, checkedNodes) {
if (!this.multiple) return
this.$emit('input', this.handCheckSetValue())
},

/**
* @Description: 多选设置值
* @param {*}
* @return {*}
*/
handCheckSetValue () {
this.selectData = cloneDeep(this.$refs.treeRef.getCheckedNodes(true, false))
let val = this.selectData.map(item => item[this.cId])
val = castArray(val)
this.selectId = val
return val
},

/**
* @Description: 清空
* @param {*}
* @return {*}
*/
handleClear () {
this.$emit('input', this.multiple ? [] : null)
},

/**
* @Description: 多选模式移除tag
* @param {*} v
* @return {*}
*/
handleRemoveTag (v) {
this.$emit('input', this.selectId)
}
}
}
</script>
<style lang="scss" scoped>
.tree-select {
/deep/.el-input--medium .el-input__inner {
height: 30px;
line-height: 30px;
border-radius: 6px;
border: 1px solid #eaeaea;
color: #464646;
padding-left: 5px;
&:focus {
border-color: #5587f0;
}
}
/deep/.el-icon-arrow-up:before {
content: '\e78f';
}
/deep/.el-input--medium .el-input__icon {
line-height: 30px;
}
}
.tree-opt-box {
.el-tree {
border-radius: 6px;
color: #464646;
/deep/.el-tree-node__content {
height: 30px;
padding-right: 12px;
}
}

// 隐藏option
.el-select-dropdown__item.selected{
display: none !important;
}
}
</style>
<style lang="scss">
.tree-opt-box {
overflow: hidden !important;
}
</style>