menuModal.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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/power-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. // 查找叶子节点
  105. findLeaf (data, arr) {
  106. for (let i = 0; i < data.length; i++) {
  107. const node = data[i]
  108. if (node.children) {
  109. this.findLeaf(node.children, arr)
  110. } else {
  111. const hasNode = arr.find(item => {
  112. return item == node.id
  113. })
  114. if (hasNode) {
  115. this.expandedKeys.push(node.id)
  116. this.checkedKeys.push(node.id)
  117. }
  118. }
  119. }
  120. }
  121. },
  122. beforeCreate () {
  123. this.form = this.$form.createForm(this, { name: 'menuModal' })
  124. },
  125. watch: {
  126. visible (newValue, oldValue) {
  127. this.isshow = newValue
  128. },
  129. isshow (newValue, oldValue) {
  130. if (newValue) {
  131. if (this.data) { // 编辑
  132. this.treeData = this.data.allMenuList
  133. this.id = this.data.role.id
  134. this.roleName = '分配权限' + '(' + this.data.role.name + ')'
  135. if (this.data.role.menuIds) {
  136. const arr = this.data.role.menuIds.split(',')
  137. this.checkedData = arr
  138. // 找出叶子节点
  139. this.findLeaf(this.treeData, arr)
  140. }
  141. }
  142. } else {
  143. this.cancel()
  144. }
  145. }
  146. }
  147. }
  148. </script>
  149. <style >
  150. .modalBox{
  151. }
  152. .ant-modal-footer {
  153. display: none;
  154. }
  155. .form-box {
  156. max-height: 600px !important;
  157. overflow: auto;
  158. }
  159. .form-box::-webkit-scrollbar{
  160. width: 6px;
  161. height: 6px ;
  162. }
  163. .form-box::-webkit-scrollbar-thumb{
  164. width: 6px;
  165. height: 6px ;
  166. border-radius: 4px;
  167. -webkit-box-shadow: inset 0 0 2px #d1d1d1;
  168. background: #e4e4e4;
  169. }
  170. </style>