liuxiaolong
2019-05-06 2ab7a76a38fbf8fa107bf6371f5410ba54e1d394
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
<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>
      <b-btn
        class="ml10"
        :variant="strictly ? 'outline-primary' : 'primary'"
        @click="strictly = !strictly"
      >
        {{ strictly ? '父子不关联' : '父子关联'}}
      </b-btn>
    </div>
    <Tree
      ref="tree"
      node-key="id"
      show-checkbox
      :check-strictly="strictly"
      :data="rolesList"
      highlight-current
      :props="defaultProps"
    />
  </b-modal>
</template>
 
<script>
/**
 * 权限菜单弹窗
 * @param { Array } rolesList 权限菜单树形结构
 * @param { Array } rolePermission 权限菜单回显数组,为权限菜单对应id
 * @return { String } submit 返回选中权限id
 * @return { Function } showModel 打开弹窗
 * @return { Function } hideModal 关闭弹窗
 */
 
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()
      this.strictly = true
    },
    hideModel() {
      this.$refs.myModalRef.hide()
    },
    handleOk(e) {
      e.preventDefault()
      let arr = this.$refs.tree.getCheckedKeys()
      arr = [ ...this.$refs.tree.getHalfCheckedKeys(), ...arr ]
      const str = arr.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>