role-permission.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <template>
  2. <div v-loading="loading">
  3. <div class="white-box admin-role-box">
  4. <el-table :data="tableData" border style="width: 100%">
  5. <el-table-column label="模块" width="180">
  6. <template slot-scope="scope">
  7. <el-checkbox :indeterminate="isIndeterminate[scope.row.id]" v-model="mainCheck[scope.row.id]"
  8. @change="handleCheckAllChange(scope.row.id, scope.row.childPermission)">
  9. {{scope.row.mainPermission.name}}
  10. </el-checkbox>
  11. </template>
  12. </el-table-column>
  13. <el-table-column label="权限">
  14. <template slot-scope="scope">
  15. <el-checkbox-group v-model="checkedValue" class="checkgroup"
  16. @change="handlePermissionChange(scope.row.id, scope.row.childPermission)">
  17. <el-checkbox v-for="item in scope.row.childPermission" :label="item.path" :key="item.name">
  18. {{item.name}}
  19. </el-checkbox>
  20. </el-checkbox-group>
  21. </template>
  22. </el-table-column>
  23. </el-table>
  24. <div style="margin-top: 20px">
  25. <el-button type="primary" :loading="submitButtonStat" @click="onSubmit" style="float: right;">Submit<!-- 提交 --></el-button>
  26. </div>
  27. </div>
  28. </div>
  29. </template>
  30. <script>
  31. import tool from '@/utils/tool'
  32. import { getRolePermission,editRolePermission } from '@/api/filter'
  33. export default {
  34. name: 'role-permission',
  35. created () {
  36. },
  37. data () {
  38. return {
  39. tableData: null,
  40. checkedValue: [],
  41. mainCheck: {},
  42. isIndeterminate: {},
  43. loading: true,
  44. submitButtonStat: false,
  45. }
  46. },
  47. mounted () {
  48. getRolePermission(this.$route.params.id).then(response => {
  49. this.tableData = response.data
  50. this.checkedValue = []
  51. for (let i of response.data) {
  52. this.mainCheck[i] = false
  53. for (let j of i.childPermission) {
  54. if (j.isChecked) {
  55. this.checkedValue.push(j.path)
  56. }
  57. }
  58. this.initCheckValue(i.id, i.childPermission)
  59. }
  60. this.loading = false
  61. }).catch(err => {
  62. console.log('err---------' + err)
  63. })
  64. },
  65. methods: {
  66. handleCheckAllChange (id, child) {
  67. //this.mainCheck[id] = !this.mainCheck[id];
  68. if (this.mainCheck[id]) {
  69. this.$set(this.isIndeterminate, id, false)
  70. for (let i in child) {
  71. tool.removeFromArr(this.checkedValue, child[i].path)
  72. this.checkedValue.push(child[i].path)
  73. }
  74. } else {
  75. this.$set(this.isIndeterminate, id, false)
  76. for (let i in child) {
  77. tool.removeFromArr(this.checkedValue, child[i].path)
  78. }
  79. }
  80. },
  81. handlePermissionChange (id, child) {
  82. this.initCheckValue(id, child)
  83. },
  84. onSubmit () {
  85. this.submitButtonStat = true
  86. editRolePermission(this.$route.params.id, {
  87. permission: this.checkedValue
  88. }).then(response => {
  89. this.submitButtonStat = false
  90. this.$message({message: response.data, type: 'success'})
  91. }).catch(err => {
  92. this.submitButtonStat = false
  93. console.log('err---------' + err)
  94. })
  95. },
  96. initCheckValue (id, child) {
  97. let flag = true
  98. for (let i in child) {
  99. if (this.checkedValue.find(item => child[i].path === item) === undefined) {
  100. flag = false
  101. }
  102. }
  103. if (flag) {
  104. this.$set(this.isIndeterminate, id, false)
  105. this.$set(this.mainCheck, id, true)
  106. } else {
  107. this.$set(this.isIndeterminate, id, true)
  108. this.$set(this.mainCheck, id, false)
  109. let mainFlag = false
  110. for (let i in this.checkedValue) {
  111. if (child.find(item => this.checkedValue[i] === item.path) !== undefined) {
  112. mainFlag = true
  113. }
  114. }
  115. if (!mainFlag) {
  116. this.$set(this.isIndeterminate, id, false)
  117. this.$set(this.mainCheck, id, false)
  118. }
  119. }
  120. }
  121. }
  122. }
  123. </script>
  124. <style scoped>
  125. </style>