index2.vue
11.9 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<template>
<div class="el-tree-select" :class="selectClass">
<!-- 下拉文本 -->
<el-select
ref="select"
v-model="labels"
v-popover:popover
v-bind="selectParams"
:disabled="disabled"
popper-class="select-option"
:popper-append-to-body="false"
:filterable="false"
:multiple="selectParams.multiple"
:title="labels"
:style="styles"
class="el-tree-select-input"
@clear="selectClearFun"
@focus="popoverShowFun"
>
<el-option :value="labels">
<!-- 弹出框 -->
<el-popover
ref="popover"
v-model="visible"
:placement="placement"
:popper-class="popperClass"
:width="width"
trigger="click"
>
<el-input
v-model="keywords"
class="selectInput"
:placeholder="selectParams.placeholder"
suffix-icon="el-icon-search"
/>
<!-- <el-input
v-model="keywords"
class="selectInput"
:placeholder="selectParams.placeholder"
suffix-icon="el-icon-search"
@change="searchFun"
/> -->
<el-scrollbar
tag="div"
wrap-class="el-select-dropdown__wrap"
view-class="el-select-dropdown__list"
class="is-empty"
>
<!-- 树列表 -->
<el-tree
v-show="data.length > 0"
ref="tree"
:check-strictly="checkStrictly"
:default-expand-all="false"
:accordion="true"
:data="data"
:node-key="propsValue"
:draggable="false"
:current-node-key="ids.length > 0 ? ids[0] : ''"
:filter-node-method="filterFuntion"
:render-content="treeRenderFun"
@node-click="treeNodeClickFun"
/>
<!-- 暂无数据 -->
<div v-if="data.length === 0" class="no-data">暂无数据</div>
</el-scrollbar>
</el-popover>
</el-option>
</el-select>
</div>
</template>
<script lang="ts">
import { Component, Vue, Prop, Watch } from 'vue-property-decorator'
import { ElForm } from 'element-ui/types/form'
import { Tree as ElTree } from 'element-ui'
@Component({
name: 'TreeSelect'
})
// @group api
export default class extends Vue {
@Prop({
default: () => {
return ''
}
})
private value!: ['', [], Number]
@Prop({
default: () => {
return {}
}
})
private styles!: {}
@Prop({
default: () => {
return ''
}
})
private selectClass!: string
@Prop({
default: () => {
return ''
}
})
private popoverClass!: string
@Prop({
default: () => {
return false
}
})
private disabled!: boolean
@Prop({
default: () => {
return 'bottom'
}
})
private placement!: string
@Prop() private treeRenderFun!: Function
@Prop({
default: () => {
return {
clearable: true,
disabled: false,
placeholder: '请选择'
}
}
})
private selectParams!: {
multiple: boolean
}
@Prop({
default: () => {
return {
clickParent: false,
filterable: false,
data: [],
props: {
children: 'children',
label: 'label',
code: 'code',
value: 'flowId',
disabled: 'disabled'
}
}
}
})
private treeParams!: {
data: any
props: any
clickParent: any
}
@Prop()
private orgData!: {}
@Prop() private orgName!: ''
@Prop() private orgId!: ''
@Prop() private userIds!: []
private propsValue: string = 'flowId'
private propsLabel: string = 'name'
private propsCode: any // 可能有空的情况
private propsDisabled: string = 'disabled'
private propsChildren: string = 'children'
private data = [] as any[]
private keywords: string = ''
private labels = '' as any // 存储名称,用于下拉框显示内容
private ids = [] as any[] // 存储id
private visible = false // popover v-model
private width = 150
private el: any = this.$refs
private checkStrictly = false
@Watch('ids')
getIds(val: any) {
if (val !== undefined) {
this.$nextTick(() => {
this.setSelectNodeFun(val)
})
}
}
@Watch('userIds')
getuserIds(val: any) {
this.labels = val
if (this.selectParams.multiple) {
this.labels = []
this.ids = val
} else {
this.labels = ''
this.ids = val instanceof Array ? val : [val]
}
}
@Watch('value')
getvalue(val: any) {
if (this.ids !== val) {
this.setMultipleFun()
if (this.selectParams.multiple) {
this.ids = [...val]
} else {
this.ids = val === '' ? [] : [val]
}
}
}
@Watch('keywords')
getkeywords(value: any) {
(this.$refs.tree as ElTree).filter(value)
}
@Watch('orgId')
getorgName(value: any) {
if (value !== '') {
const { props, data } = this.treeParams
if (data !== null) {
// 不让选择父级
// if (
// !(
// this.el.tree.getNode(value).data.children &&
// this.el.tree.getNode(value).data.children.length !== 0
// )
// ) {
// this.labels = this.el.tree.getNode(value).data[ this.treeParams.props.label ]
// }
// 可以选择父级
this.labels = this.el.tree.getNode(value).data[ this.treeParams.props.label ]
}
} else {
this.labels = ''
}
}
// @Watch('ogrId')
// getorgId(val: any) {
// this.labels = val
// }
get popperClass() {
let _c = 'el-tree-select-popper ' + this.popoverClass
return this.disabled ? _c + ' disabled ' : _c
}
/// 生命周期
created() {
const { props, data } = this.treeParams
this.setMultipleFun()
this.propsValue = props.value
this.propsLabel = props.label
this.propsCode = props.code || null // 可能为空
this.propsDisabled = props.disabled
this.propsChildren = props.children
this.data = data.length > 0 ? [...data] : []
// this.getuserIds
// this.treeDataUpdateFun(this.data)
}
mounted() {
this.updateH()
}
// 根据类型判断单选,多选
setMultipleFun() {
let multiple = false
if (this.value instanceof Array) {
multiple = true
}
this.$set(this.selectParams, 'multiple', multiple)
}
// 根据id筛选当前树名称,以及选中树列表
setSelectNodeFun(ids: any) {
if (!this.el.tree) {
throw new Error('找不到tree dom')
}
const { multiple } = this.selectParams
// 长度为0,清空选择
if (ids.length === 0 || this.data.length === 0) {
this.labels = multiple ? [] : ''
if (multiple) {
this.el.tree.setCheckedKeys([])
} else {
this.el.tree.setCurrentKey(null)
}
return
}
if (multiple) {
// element-ui bug. 如果是父子节点全选 el.setCheckedKeys([非全量id]);之后el.getCheckedNodes()还是全量
this.el.tree.getCheckedNodes().forEach((item: any) => {
this.el.tree.setChecked(item, false)
})
ids.forEach((id: any) => {
this.el.tree.setChecked(id, true)
})
const nodes = this.el.tree.getCheckedNodes()
if (this.propsCode) {
// 如果有code labels=code(name)
this.labels =
nodes.map((item: any) =>
item[this.propsCode]
? item[this.propsLabel] + '(' + item[this.propsCode] + ')'
: item[this.propsLabel]
) || []
} else {
this.labels =
nodes.map((item: any) => {
// if (!(item.children && item.children.length !== 0)) {
return item[this.propsLabel]
// }
}) || []
}
} else {
this.el.tree.setCurrentKey(ids[0])
const node = this.el.tree.getCurrentNode()
if (node) {
if (this.propsCode) {
// 如果有code labels=code(name)
this.labels = node[this.propsCode]
? node[this.propsLabel] + '(' + node[this.propsCode] + ')'
: node[this.propsLabel]
} else {
this.labels = node[this.propsLabel]
}
} else {
this.labels = ''
}
}
this.updatePopoverLocationFun()
}
// 更新popover位置
updatePopoverLocationFun() {
// dom高度还没有更新,做一个延迟
// const el:any = this.$refs
setTimeout(() => {
this.el.popover.updatePopper()
}, 50)
}
// 树过滤
filterFuntion(value: string, data: any, node: any) {
if (!value) {
return true
}
return data.label && data.label.indexOf(value) !== -1
}
// 树点击
treeNodeClickFun(data: any, node: any, vm: any) {
// this.$set(node,'checked',false)
this.$emit('getValue', data.id)
const { multiple } = this.selectParams
const { clickParent } = this.treeParams
const { propsValue, propsChildren, propsDisabled } = this
if (data[propsDisabled]) {
// 禁用
return
}
if (node.checked) {
const value = data[this.propsValue]
this.ids = this.ids.filter(id => id !== value)
} else {
if (!multiple) {
// 多选,不关闭,单选,判断是否允许点击父级关闭弹出框
if (!clickParent) {
const children = data[propsChildren]
// 如果不允许点击父级,自身为末级,允许点击之后关闭
if (!children || children.length === 0) {
this.ids = [data[propsValue]]
this.visible = false
} else {
// 不允许父级,阻止继续派发
return false
}
} else {
this.ids = [data[propsValue]]
this.visible = false
}
} else {
this.ids.push(data[propsValue])
}
}
this.emitFun()
// this.$emit('node-click', data, node, vm)
}
// 下拉框清空数据
selectClearFun() {
this.ids = []
const { multiple } = this.selectParams
// 下拉框清空,对外抛出``this.$emit('input', multiple ? [] : '');`
this.$emit('input', multiple ? [] : '')
// 下拉框清空,对外抛出``this.$emit('select-clear');`
this.$emit('select-clear')
this.updatePopoverLocationFun()
}
// 判断类型,抛出当前选中id
emitFun() {
const { multiple } = this.selectParams
this.$emit(
'input',
multiple ? this.ids : this.ids.length > 0 ? this.ids[0] : ''
)
this.updatePopoverLocationFun()
}
// 更新宽度
updateH() {
this.$nextTick(() => {
this.width = this.el.select.$el.getBoundingClientRect().width
})
}
// 显示弹出框的时候容错,查看是否和el宽度一致
popoverShowFun(val: any) {
this.updateH()
}
treeDataUpdateFun(data: any) {
this.data = data
// 数据更新完成之后,判断是否回显内容
if (data.length > 0) {
setTimeout(() => {
this.setSelectNodeFun(this.ids)
}, 300)
}
}
// filterFun(val: any) {
// ;(this.$refs.tree as ElTree).filter(val)
// }
}
</script>
<style scoped>
.el-tree {
color: #424250;
}
.el-scrollbar .el-scrollbar__view .el-select-dropdown__item {
height: auto;
max-height: 274px;
padding: 0;
overflow: hidden;
overflow-y: auto;
}
.el-select-dropdown__item.selected {
font-weight: normal;
}
ul li >>> .el-tree .el-tree-node__content {
height: 32px;
}
.el-tree-node__label {
font-weight: normal;
}
.el-tree >>> .is-current .el-tree-node__label {
color: #424250;
}
.el-tree >>> .is-current .el-tree-node__children .el-tree-node__label {
color: #606266;
font-weight: normal;
}
.selectInput {
padding: 0 5px;
box-sizing: border-box;
}
</style>
<style>
.el-tree-node.is-current > .el-tree-node__content {
background: #ecf5ff;
}
.el-tree-node__expand-icon {
color: #909399;
}
.el-tree-node__content > .el-tree-node__expand-icon {
padding-left: 4px;
padding-right: 4px;
}
.el-tree-node__children .el-tree-node > .el-tree-node__content {
padding-left: 14px !important;
}
</style>