main.vue
1.77 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
<template>
<div class="tree" :style="treeStyle">
<gl-tree
ref='tree'
highlight-current
:data='data'
:show-checkbox='showCheckbox'
node-key='id'
:props='props'
:default-expand-all='defaultExpandAll'
:default-checked-keys="defaultCheckedKeys"
@check='check'
@node-click='nodeClick'
></gl-tree>
</div>
</template>
<script>
import { findMenuTree, findDepartTree } from '@/api/api'
export default {
name: 'Tree',
data() {
return {
data: null,
props: {
label: 'text'
}
}
},
props: {
showCheckbox: Boolean,
value: null,
defaultExpandAll: Boolean,
treeStyle: null,
defaultCheckedKeys: {
type: Array,
default: () => []
},
isDepart: Boolean
},
watch: {
defaultCheckedKeys(val) {
this.loadingTree()
}
},
methods: {
nodeClick(data, treeData, vue) {
this.$emit('node-click', data, treeData, vue, this.props)
},
check(data, treeData) {
this.$emit('input', { data, treeData })
},
loadingTree() {
!this.isDepart
? findMenuTree.req().then(res => {
this.data = fn(res, '#')
}).catch(err => {
console.log(err)
})
: findDepartTree.req().then(res => {
this.data = fn(res, '#')
}).catch(err => {
console.log(err)
})
}
},
mounted() {
this.loadingTree()
}
}
function fn(data, pid) {
var result = []
var temp = null
for (var i in data) {
if (data[i].parent == pid) {
result.push(data[i])
temp = fn(data, data[i].id)
if (temp.length > 0) {
data[i].children = temp
}
}
}
return result
}
</script>
<style scoped>
.tree {
height: 550px;
overflow-y: scroll;
}
</style>