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
| <template>
| <b-modal
| title="权限设置"
| ref="myModalRef"
| ok-title="保存"
| @ok="handleOk"
| @cancel="clearName"
| cancel-title="取消"
| >
| <div class="btn-content">
| <b-btn
| :variant="btnTypeAll ? 'primary' : 'outline-primary'"
| @click="handleAllSelect"
| >
| 全选
| </b-btn>
| </div>
| <Tree
| ref="tree"
| node-key="id"
| show-checkbox
| :check-strictly="strictly"
| :data="rolesList"
| highlight-current
| :props="defaultProps"
| />
| </b-modal>
| </template>
|
| <script>
| import { Tree } from 'element-ui'
| export default {
| props: {
| rolesList: Array,
| rolePermission: Array
| },
| data: () => ({
| strictly: true, // * 是否父子关联
| defaultProps: {
| children: 'child',
| label: 'name'
| },
| btnTypeAll: false
| }),
| methods: {
| showModel() {
| this.$refs.myModalRef.show()
| },
| hideModel() {
| this.$refs.myModalRef.hide()
| },
| handleOk(e) {
| e.preventDefault()
| const str = this.$refs.tree.getCheckedKeys().join(',')
| this.$emit('submit', str)
| },
| // * 全选按钮回调
| handleAllSelect() {
| this.btnTypeAll = !this.btnTypeAll
| let arr = []
| if (this.btnTypeAll) {
| // 全选
| this.getAllRolesId(this.rolesList, (id) => {
| arr.push(id)
| })
| this.$refs.tree.setCheckedKeys(arr)
| } else {
| this.$refs.tree.setCheckedKeys([])
| }
| },
| // * 递归查数据
| getAllRolesId(arr, fn) {
| return arr.map(item => {
| if (item.child && item.child.length) {
| this.getAllRolesId(item.child, fn)
| }
| fn(item.id)
| })
| },
| clearName() {
|
| }
| },
| watch: {
| rolePermission(val) {
| this.$refs.tree.setCheckedKeys(val)
| }
| },
| components: {
| Tree
| }
| }
| </script>
|
| <style lang="scss" scoped>
| .btn-content {
| display: flex;
| justify-content: flex-end;
| }
| </style>
|
|