menuModal.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <template>
  2. <div>
  3. <a-modal class="modalBox" :title="roleName" v-model="isshow" @cancle="cancel" :centered="true">
  4. <a-form class="form-box" style="max-height: 600px;" :form="form" @submit="handleSubmit">
  5. <a-tree
  6. checkable
  7. @check="onCheck"
  8. @expand="onExpand"
  9. :expandedKeys="expandedKeys"
  10. :autoExpandParent="autoExpandParent"
  11. v-model="checkedKeys"
  12. :treeData="treeData"
  13. />
  14. </a-form>
  15. <a-form-item :wrapper-col="{ offset: 15 }">
  16. <a-button type="primary" @click="handleSubmit()" id="menuModal-handleSubmit">
  17. 保存
  18. </a-button>
  19. <a-button :style="{ marginLeft: '8px' }" @click="handleCancel()" id="menuModal-handleCancel">
  20. 取消
  21. </a-button>
  22. </a-form-item>
  23. </a-modal>
  24. </div>
  25. </template>
  26. <script>
  27. import { STable, VSelect } from '@/components'
  28. import { addMenuPower } from '@/api/supervision-role.js'
  29. export default {
  30. name: 'RoleModal',
  31. components: {
  32. STable, VSelect
  33. },
  34. props: {
  35. visible: {
  36. type: Boolean,
  37. default: false
  38. },
  39. data: {
  40. type: Object,
  41. default: function () {
  42. return {}
  43. }
  44. }
  45. },
  46. data () {
  47. return {
  48. treeData: [],
  49. roleName: '',
  50. expandedKeys: [],
  51. autoExpandParent: true,
  52. checkedKeys: [],
  53. checkedData: [],
  54. isshow: this.visible,
  55. formLayout: 'horizontal',
  56. form: this.$form.createForm(this, { name: 'menuModal' }),
  57. id: '' // 角色id
  58. }
  59. },
  60. methods: {
  61. onExpand (expandedKeys) {
  62. this.expandedKeys = expandedKeys
  63. this.autoExpandParent = false
  64. },
  65. onCheck (checkedKeys, info) {
  66. this.checkedData = [...checkedKeys, ...info.halfCheckedKeys]
  67. this.checkedKeys = checkedKeys
  68. },
  69. cancel (e) {
  70. this.clear()
  71. this.$emit('close')
  72. },
  73. // 保存提交
  74. handleSubmit () {
  75. const _this = this
  76. this.form.validateFields((err, values) => {
  77. if (!err) {
  78. if (this.checkedData.length == 0) {
  79. return this.$message.warning('请先选择菜单')
  80. }
  81. const arr = this.checkedData.join(',')
  82. addMenuPower({ id: this.id, menuIds: arr }).then(res => {
  83. if (res.status == 200) {
  84. this.$message.success(res.message)
  85. this.$emit('refresh')
  86. setTimeout(function () {
  87. _this.cancel()
  88. }, 300)
  89. }
  90. })
  91. }
  92. })
  93. },
  94. // 取消
  95. handleCancel () {
  96. this.cancel()
  97. },
  98. clear () {
  99. this.form.resetFields()
  100. this.roleName = ''
  101. this.id = ''
  102. this.checkedKeys = []
  103. },
  104. findLeaf (data, arr) {
  105. for (let i = 0; i < data.length; i++) {
  106. const node = data[i]
  107. if (node.children) {
  108. this.findLeaf(node.children, arr)
  109. } else {
  110. const hasNode = arr.find(item => {
  111. return item == node.id
  112. })
  113. if (hasNode) {
  114. this.checkedKeys.push(node.id)
  115. }
  116. }
  117. }
  118. }
  119. },
  120. beforeCreate () {
  121. this.form = this.$form.createForm(this, { name: 'menuModal' })
  122. },
  123. watch: {
  124. visible (newValue, oldValue) {
  125. this.isshow = newValue
  126. },
  127. isshow (newValue, oldValue) {
  128. if (newValue) {
  129. if (this.data) { // 编辑
  130. console.log(this.data)
  131. this.treeData = this.data.allMenuList
  132. this.id = this.data.role.id
  133. this.roleName = '分配权限' + '(' + this.data.role.name + ')'
  134. if (this.data.role.menuIds) {
  135. const arr = this.data.role.menuIds.split(',')
  136. const temp = []
  137. arr.map(item => {
  138. temp.push(Number(item))
  139. })
  140. this.checkedData = temp
  141. this.expandedKeys = temp
  142. // 找出叶子节点
  143. this.findLeaf(this.treeData, temp)
  144. console.log(this.treeData, '-----梳妆')
  145. }
  146. }
  147. } else {
  148. this.cancel()
  149. }
  150. }
  151. }
  152. }
  153. </script>
  154. <style >
  155. .modalBox{
  156. }
  157. .ant-modal-footer {
  158. display: none;
  159. }
  160. .form-box {
  161. max-height: 600px !important;
  162. overflow: auto;
  163. }
  164. .form-box::-webkit-scrollbar{
  165. width: 6px;
  166. height: 6px ;
  167. }
  168. .form-box::-webkit-scrollbar-thumb{
  169. width: 6px;
  170. height: 6px ;
  171. border-radius: 4px;
  172. -webkit-box-shadow: inset 0 0 2px #d1d1d1;
  173. background: #e4e4e4;
  174. }
  175. </style>