menuModal.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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="cancel" 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/powerRole-md.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. console.log(this.checkedData)
  79. if (this.checkedData.length == 0) {
  80. return this.$message.warning('请先选择菜单')
  81. }
  82. const arr = this.checkedData.join(',')
  83. console.log({ id: this.id, menuIds: arr })
  84. addMenuPower({ id: this.id, menuIds: arr }).then(res => {
  85. console.log(res, 'res--save')
  86. if (res.status + '' === '200') {
  87. this.$message.success(res.message ? res.message : '保存成功')
  88. this.$emit('refresh')
  89. setTimeout(function () {
  90. _this.cancel()
  91. }, 300)
  92. } else {
  93. // this.$message.error(res.message)
  94. }
  95. })
  96. }
  97. })
  98. },
  99. clear () {
  100. this.form.resetFields()
  101. this.roleName = ''
  102. this.id = ''
  103. this.checkedKeys = []
  104. this.expandedKeys = []
  105. },
  106. // 查找叶子节点
  107. findLeaf (data, arr) {
  108. for (let i = 0; i < data.length; i++) {
  109. const node = data[i]
  110. if (node.children) {
  111. this.findLeaf(node.children, arr)
  112. } else {
  113. const hasNode = arr.find(item => {
  114. return item == node.id
  115. })
  116. if (hasNode) {
  117. this.expandedKeys.push(node.id)
  118. this.checkedKeys.push(node.id)
  119. }
  120. }
  121. }
  122. }
  123. },
  124. beforeCreate () {
  125. this.form = this.$form.createForm(this, { name: 'menuModal' })
  126. },
  127. watch: {
  128. visible (newValue, oldValue) {
  129. this.isshow = newValue
  130. },
  131. isshow (newValue, oldValue) {
  132. if (newValue) {
  133. if (this.data) { // 编辑
  134. this.treeData = this.data.allMenuList
  135. this.id = this.data.role.id
  136. this.roleName = '分配权限' + '(' + this.data.role.name + ')'
  137. if (this.data.role.menuIds) {
  138. const arr = this.data.role.menuIds.split(',')
  139. this.checkedData = arr
  140. // 找出叶子节点
  141. this.findLeaf(this.treeData, arr)
  142. }
  143. }
  144. } else {
  145. this.cancel()
  146. }
  147. }
  148. }
  149. }
  150. </script>
  151. <style >
  152. .ant-modal-title{
  153. padding-right: 30px;
  154. }
  155. .ant-modal-footer {
  156. display: none;
  157. }
  158. .form-box {
  159. max-height: 600px !important;
  160. overflow: auto;
  161. }
  162. .form-box::-webkit-scrollbar{
  163. width: 6px;
  164. height: 6px ;
  165. }
  166. .form-box::-webkit-scrollbar-thumb{
  167. width: 6px;
  168. height: 6px ;
  169. border-radius: 4px;
  170. -webkit-box-shadow: inset 0 0 2px #d1d1d1;
  171. background: #e4e4e4;
  172. }
  173. </style>