menuModal.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <template>
  2. <div>
  3. <a-modal
  4. centered
  5. class="roleMenuEdit-modal"
  6. :footer="null"
  7. :maskClosable="false"
  8. :title="titleText"
  9. v-model="isshow"
  10. @cancel="isshow=false"
  11. :width="800">
  12. <a-spin :spinning="spinning" tip="Loading...">
  13. <a-form-model
  14. id="roleMenuEdit-form"
  15. ref="ruleForm"
  16. :model="form"
  17. :rules="rules"
  18. :label-col="formItemLayout.labelCol"
  19. :wrapper-col="formItemLayout.wrapperCol"
  20. >
  21. <div style="height: 500px;overflow-y: scroll;">
  22. <a-tree
  23. checkable
  24. @check="onCheck"
  25. @expand="onExpand"
  26. :expandedKeys="expandedKeys"
  27. :autoExpandParent="autoExpandParent"
  28. v-model="checkedKeys"
  29. :treeData="treeData"
  30. />
  31. </div>
  32. <a-form-model-item :label-col="{span: 0}" :wrapper-col="{span: 24}" style="text-align: center;">
  33. <a-button type="primary" id="roleMenuEdit-save" @click="handleSubmit()" :style="{ marginRight: '8px' }">保存</a-button>
  34. <a-button id="roleMenuEdit-cancel" :style="{ marginLeft: '8px' }" @click="isshow=false">取消</a-button>
  35. </a-form-model-item>
  36. </a-form-model>
  37. </a-spin>
  38. </a-modal>
  39. </div>
  40. </template>
  41. <script>
  42. import { commonMixin } from '@/utils/mixin'
  43. import { STable, VSelect } from '@/components'
  44. import { addMenuPower } from '@/api/power-role.js'
  45. export default {
  46. name: 'RoleModal',
  47. mixins: [commonMixin],
  48. components: {
  49. STable, VSelect
  50. },
  51. props: {
  52. visible: {
  53. type: Boolean,
  54. default: false
  55. },
  56. nowData: {
  57. type: Object,
  58. default: function () {
  59. return {}
  60. }
  61. }
  62. },
  63. data () {
  64. return {
  65. spinning: false,
  66. treeData: [],
  67. titleText: '',
  68. expandedKeys: [],
  69. autoExpandParent: true,
  70. checkedKeys: [],
  71. checkedData: [],
  72. isshow: this.visible,
  73. formItemLayout: {
  74. labelCol: { span: 6 },
  75. wrapperCol: { span: 16 }
  76. },
  77. form: {},
  78. rules: {},
  79. id: '' // 角色id
  80. }
  81. },
  82. methods: {
  83. onExpand (expandedKeys) {
  84. this.expandedKeys = expandedKeys
  85. this.autoExpandParent = false
  86. },
  87. onCheck (checkedKeys, info) {
  88. this.checkedData = [...checkedKeys, ...info.halfCheckedKeys]
  89. this.checkedKeys = checkedKeys
  90. },
  91. // 保存提交
  92. handleSubmit () {
  93. const _this = this
  94. this.$refs.ruleForm.validate(valid => {
  95. if (valid) {
  96. if (this.checkedData.length == 0) {
  97. return this.$message.warning('请先选择菜单')
  98. }
  99. const arr = this.checkedData.join(',')
  100. _this.spinning = true
  101. addMenuPower({ id: this.id, menuIds: arr }).then(res => {
  102. if (res.status == 200) {
  103. _this.$message.success(res.message)
  104. _this.$emit('refresh')
  105. setTimeout(function () {
  106. _this.isshow = false
  107. _this.spinning = false
  108. }, 300)
  109. } else {
  110. _this.spinning = false
  111. }
  112. })
  113. } else {
  114. console.log('error submit!!')
  115. return false
  116. }
  117. })
  118. },
  119. clear () {
  120. this.form.resetFields()
  121. this.titleText = ''
  122. this.id = ''
  123. this.checkedKeys = []
  124. this.expandedKeys = []
  125. },
  126. // 选中已勾选菜单
  127. findLeaf (data, arr) {
  128. for (let i = 0; i < data.length; i++) {
  129. const node = data[i]
  130. if (node.children) {
  131. this.findLeaf(node.children, arr)
  132. } else {
  133. node.class = 'leafNode'
  134. const hasNode = arr.find(item => {
  135. return item == node.id
  136. })
  137. if (hasNode) {
  138. this.expandedKeys.push(node.id)
  139. this.checkedKeys.push(node.id)
  140. }
  141. }
  142. }
  143. }
  144. },
  145. watch: {
  146. visible (newValue, oldValue) {
  147. this.isshow = newValue
  148. },
  149. isshow (newValue, oldValue) {
  150. if (!newValue) {
  151. this.$emit('close')
  152. this.$refs.ruleForm.resetFields()
  153. this.id = ''
  154. this.checkedKeys = []
  155. this.expandedKeys = []
  156. this.titleText = '新增角色'
  157. }
  158. },
  159. nowData: {
  160. handler (newValue, oldValue) {
  161. if (this.isshow && newValue) {
  162. this.id = this.nowData.role.id
  163. this.titleText = '分配权限' + '(' + this.nowData.role.name + ')'
  164. // 已选节点
  165. const arr = this.nowData.role.menuIds
  166. this.checkedData = arr ? arr.split(',') : []
  167. // 找出叶子节点
  168. this.findLeaf(this.nowData.allMenuList, this.checkedData)
  169. this.treeData = this.nowData.allMenuList
  170. console.log(this.treeData)
  171. }
  172. },
  173. deep: true
  174. }
  175. }
  176. }
  177. </script>
  178. <style >
  179. .ant-modal-title{
  180. padding-right: 30px;
  181. }
  182. .ant-modal-footer {
  183. display: none;
  184. }
  185. .form-box {
  186. max-height: 600px !important;
  187. overflow: auto;
  188. }
  189. .form-box::-webkit-scrollbar{
  190. width: 6px;
  191. height: 6px ;
  192. }
  193. .form-box::-webkit-scrollbar-thumb{
  194. width: 6px;
  195. height: 6px ;
  196. border-radius: 4px;
  197. -webkit-box-shadow: inset 0 0 2px #d1d1d1;
  198. background: #e4e4e4;
  199. }
  200. .leafNode{
  201. display: inline-block;
  202. }
  203. </style>