roleTree.vue
3.6 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
<template>
<div class="role-tree">
<el-tree
:data="resources.menus"
show-checkbox
ref="tree"
node-key="authId"
:default-expand-all="true"
:check-strictly="true"
@check-change="treeCheckChange"
:expand-on-click-node="true"
>
<span class="role-tree-node" slot-scope="{ node, data }">
<span>{{ node.label }}</span>
<div class="role-tree-auth">
<template v-if="data.auth">
<el-checkbox-group size="mini" v-model="checked">
<el-checkbox
v-for="(btn,index) in data.auth"
:key="index"
:label="btn.authId"
@change="(sta)=>btnChange(node,btn,sta)"
>{{btn.label}}</el-checkbox>
</el-checkbox-group>
</template>
</div>
</span>
</el-tree>
</div>
</template>
<script>
export default {
data() {
return { checked: [] }
},
props: {
resources: {
type: Object,
default: null
},
authIds: [],
close: Boolean
},
watch: {
close(value) {
// !value && this.reset();
},
authIds: {
handler(value) {
if (!value) return
const chk = []
const treeChk = ['101']
this.$nextTick(() => {
value.forEach(v => {
v.authId += ''
const node = this.find(v.authId)
if (!node) return true
const check = node.hasOwnProperty('label') ? chk : treeChk
check.push(v.authId)
})
this.$refs.tree.setCheckedKeys(treeChk)
this.checked = chk
})
},
immediate: true
}
},
methods: {
treeCheckChange(node, checked, childChecked) {
// if(node.parentId ==='0') return
// this.checked=checked?[...this.resources.all.filter(res=> res.parentId === node.authId),...this.checked]:this.checked.filter(obj=>obj.parentId!==node.authId)
// !checked && (this.checked = this.checked.filter(obj=>this.resources.all.find( res => res.authId === obj).parentId!==node.authId))
// this.setCheckedNodes()
},
btnChange(node, data, sta) {
const checked = sta // || this.checked.some(child => child.parentId === node.key && child.authId !== data.authId);
checked && this.$refs.tree.setChecked(node.key, checked, true)
// this.setCheckedNodes()
},
getCheckedNodes() {
const { getCheckedKeys, getHalfCheckedKeys } = this.$refs.tree
let checkedIds = [
...getCheckedKeys(),
...getHalfCheckedKeys(),
...this.checked
]
checkedIds = checkedIds.map(node => {
const { authId, parentId } = this.resources.all.find(
res => res.authId === node
)
return { authId, parentId }
})
return checkedIds
// this.$emit('update:authIds', checkedIds)
},
reset() {
this.checked = []
this.$refs.tree.setCheckedKeys([])
// this.$emit('update:authIds', [])
},
find(authId) {
return this.resources.all.find(res => res.authId === authId)
}
}
}
</script>
<style lang="scss">
.role-tree {
padding-left: 30px;
.role-tree-node {
flex: 1;
display: flex;
align-items: center;
justify-content: space-between;
font-size: 14px;
padding: 8px;
}
.el-tree-node__content {
height: 34px;
.el-checkbox {
margin-right: 5px;
.el-checkbox__label {
padding: 0;
}
}
}
&-auth {
word-break: break-all;
word-wrap: break-word;
white-space: normal;
position: absolute;
left: 200px;
background-color: rgba(0, 0, 0, 0.05);
padding: 5px 0;
}
}
</style>