fuliqi
2024-01-24 29c1e7eb5ac16e90d8991a86c1c071bc312ec8d9
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
<template>
  <div>
    <div :style="{ borderBottom: '1px solid #E9E9E9',paddingBottom:'8px'}">
      <el-checkbox :indeterminate="indeterminate" v-model="checkAll" @change="handleCheckAllChange">
        全部
      </el-checkbox>
    </div>
    <br />
    <el-checkbox-group class="role-list-wrapper" v-model="valueCheckedAry" @change="handleChange">
      <el-row>
        <el-col :span="8" v-for="item in roleAll" :key="item.id">
          <el-checkbox :label="item.id" :key="item.id">
            {{item.name}}
          </el-checkbox>
        </el-col>
      </el-row>
    </el-checkbox-group>
  </div>
</template>
 
<script>
export default {
  name: "roleSel",
  props: {
    // 显示隐藏
    visible: {
      type: Boolean,
      required: true
    },
    //当前系统用户的所有角色信息
    roleAll: {
      type: Array,
      required: true,
      default: () => {
        return [];
      },
    },
    //选中的ID集合
    checkIdList: {
      type: Array,
      required: true,
      default: () => {
        return [];
      },
    },
    // 全部角色的id集合
    allIdList: {
      type: Array,
      required: true,
      default: () => {
        return [];
      },
    },
    //平台标识
    systemId: {
      type: String,
      required: true,
    }
  },
  data() {
    return {
      // 指定当前是否选中
      checkAll: false,
      // 设置 indeterminate 状态,只负责样式控制: true-显示正方形,false-显示钩或者空(非正方形的)
      indeterminate: false,
      // 选中的角色数组
      valueCheckedAry: [],
    }
  },
  watch: {
    valueCheckedAry: {
      deep: true,
      immediate: true,
      handler(value) {
        this.$emit("updateData", { roleCodes: value, systemId: this.systemId })
      }
    },
    checkIdList: {
      deep: true,
      immediate: true,
      handler(value) {
        // 回显当前角色之前选中的角色
        this.valueCheckedAry = value ? value : [],
          // 去回显样式
          this.handleChange()
      }
    },
    visible(newV) {
      if (!newV) {
        this.restoreSettings();
      }
    }
  },
  methods: {
    /**
     * 全选改变
     */
    handleCheckAllChange(checked) {
      Object.assign(this, {
        valueCheckedAry: checked ? this.allIdList : [],
        indeterminate: false,
        checkAll: checked,
      });
    },
    /**
       * 多选框改变
       * @params:
       * checkedValues:选中的角色
       */
    handleChange() {
      this.indeterminate = !!this.valueCheckedAry.length && this.valueCheckedAry.length < this.roleAll.length;
      if (this.roleAll.length) {
        this.checkAll = this.valueCheckedAry.length === this.roleAll.length;
      }
    },
    /**
     * 还原最初设置
     */
    restoreSettings() {
      this.indeterminate = false
      this.checkAll = false
      this.valueCheckedAry = []
    },
  }
}
</script>
 
<style>
</style>