roleTree.vue
3.35 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
<template>
<div class="role-tree">
<el-tree
:data="resources.menus"
show-checkbox
ref="tree"
node-key="authId"
default-expand-all
@check-change="checkChange"
:expand-on-click-node="false"
>
<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)=>change(node,btn,sta)"
>{{btn.label}}</el-checkbox>
</el-checkbox-group>
</template>
</div>
</span>
</el-tree>
</div>
</template>
<script>
import util from "@/libs/util.js";
export default {
data() {
return { checked: [] };
},
props: {
resources: {
type: Object,
default: {}
},
defaultCheck: [],
close: false
},
mounted() {
// this.$event.$on("e-default-role-check", roles => {
// console.log(3);
// });
},
watch: {
close(value) {
// !value && this.reset();
},
defaultCheck: {
handler(value) {
let chk = [],
treeChk = [];
value.forEach(v => {
if (v.parentId.length >= 5) {
// TODO 不科学
chk.push(v.authId);
} else {
treeChk.push(v.authId);
}
});
setTimeout(() => {
this.$refs.tree.setCheckedKeys(treeChk);
this.checked = chk;
}, 0);
},
immediate: true
}
},
methods: {
checkChange(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)
this.setCheckedNodes();
},
change(node, data, sta) {
// let 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();
},
setCheckedNodes() {
let checkedNodes = [
...this.$refs.tree.getCheckedKeys(),
...this.$refs.tree.getHalfCheckedKeys(),
...this.checked
];
checkedNodes = checkedNodes.map(node => {
const { authId, parentId } = this.resources.all.find(res => res.authId === node);
return { authId, parentId };
});
this.$emit("update:checkedNodes", checkedNodes);
},
reset() {
this.checked = [];
this.$refs.tree.setCheckedKeys([]);
this.$emit("update:checkedNodes", []);
}
}
};
</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,.05);
padding: 5px 0;
}
}
</style>