organiza.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <template>
  2. <div>
  3. <a-card
  4. :body-style="{padding: '24px 32px'}"
  5. :bordered="false"
  6. title="组织架构"
  7. >
  8. <template slot="extra">
  9. <a-button type="primary" @click="openModal">新增</a-button>
  10. </template>
  11. <tree
  12. :tree-data="treeData"
  13. defaultExpandAll
  14. :replaceFields="replaceFields"
  15. @select="onSelect"
  16. @check="onCheck"
  17. >
  18. <span :slot="item.id" v-for="item in flatTreeData" :key="item.id">
  19. <div>
  20. <span>{{ item.title }}</span>
  21. <span style="margin-left: 10px" v-if="item.id === currentClickNode">
  22. <a @click.stop="handclick(item)">编辑</a>
  23. <a-divider type="vertical" />
  24. <a-popconfirm
  25. title="你确定删除这个部门吗?"
  26. ok-text="Yes"
  27. cancel-text="No"
  28. @confirm="deleteDepart(item)"
  29. >
  30. <a @click.stop="() => {}">删除</a>
  31. </a-popconfirm>
  32. </span>
  33. </div>
  34. </span>
  35. </tree>
  36. </a-card>
  37. <!-- 新增的树节点 -->
  38. <a-modal
  39. v-model="visible"
  40. :width="500"
  41. title="Basic Modal"
  42. @ok="postDepart"
  43. :confirm-loading="confirmLoading"
  44. >
  45. <div>
  46. <a-row class="department">
  47. <a-col :span="4">
  48. 上级部门
  49. </a-col>
  50. <a-col
  51. :span="18"
  52. >
  53. <a-tree-select
  54. v-model="parentLabel"
  55. @select="selectTreeNode"
  56. treeNodeFilterProp="title"
  57. show-search
  58. style="width: 100%"
  59. :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
  60. placeholder="请选择上级部门"
  61. allow-clear
  62. :tree-data="treeData"
  63. tree-default-expand-all
  64. :replaceFields="replaceFields"
  65. />
  66. </a-col>
  67. </a-row>
  68. <a-row class="department">
  69. <a-col :span="4">
  70. 部门名称
  71. </a-col>
  72. <a-col
  73. :span="18"
  74. >
  75. <a-input
  76. v-model.trim="departLabel"
  77. placeholder="请输入部门名称" />
  78. </a-col>
  79. </a-row>
  80. </div>
  81. </a-modal>
  82. <a-modal
  83. v-model="editTreeNodeVisabled"
  84. title="编辑节点"
  85. @ok="putDepart"
  86. :confirm-loading="confirmLoading"
  87. >
  88. <a-input v-model="editTreeNodeValue" ></a-input>
  89. </a-modal>
  90. </div>
  91. </template>
  92. <script>
  93. import { Tree } from 'ant-design-vue'
  94. import { deleteDepart, getDepart, postDepart, putDepart } from '@/api/role'
  95. export default {
  96. name: 'Organiza',
  97. components: {
  98. Tree
  99. },
  100. computed: {
  101. replaceFields () {
  102. return { children: 'children', title: 'label', key: 'id', value: 'label' }
  103. }
  104. },
  105. async created () {
  106. await this.getDepart()
  107. this.currentClickNode = this.treeData[0].id
  108. },
  109. watch: {
  110. currentClickNode () {
  111. this.$emit('currentClickNode', this.currentClickNode)
  112. }
  113. },
  114. data () {
  115. return {
  116. editTreeNodeValue: '',
  117. editTreeNodeId: '',
  118. editTreeNodeVisabled: false,
  119. flatTreeData: [],
  120. // tree选择
  121. treeExpandedKeys: [],
  122. parentId: '',
  123. parentLabel: '',
  124. // ---------------------------
  125. visible: false,
  126. confirmLoading: false,
  127. departLabel: '', // add or edit时 部门名称
  128. treeData: [],
  129. currentClickNode: ''
  130. }
  131. },
  132. methods: {
  133. // 编辑节点
  134. async putDepart () {
  135. const $par = {
  136. id: this.editTreeNodeId,
  137. label: this.editTreeNodeValue
  138. }
  139. this.confirmLoading = true
  140. const { code, data } = await putDepart($par)
  141. this.confirmLoading = false
  142. if (code === 0) {
  143. console.log(data)
  144. data ? this.$message.success('编辑成功') : this.$message.error('编辑失败')
  145. }
  146. this.editTreeNodeVisabled = false
  147. this.getDepart()
  148. },
  149. // 选择树节点
  150. selectTreeNode (value, node, extra) {
  151. console.log(value, node, extra)
  152. const { key } = extra.selectedNodes[0]
  153. this.parentId = key
  154. },
  155. // 删除部门
  156. async deleteDepart (item) {
  157. const { code, data } = await deleteDepart(item.id)
  158. if (code === 0) {
  159. console.log(data)
  160. data ? this.$message.success('删除成功') : this.$message.error('删除失败')
  161. }
  162. this.getDepart()
  163. },
  164. // 新增部门
  165. async postDepart () {
  166. const $par = {
  167. parentId: this.parentId,
  168. label: this.departLabel,
  169. children: [],
  170. id: ''
  171. }
  172. this.confirmLoading = true
  173. const { code, data } = await postDepart($par)
  174. this.confirmLoading = false
  175. if (code === 0) {
  176. console.log(data)
  177. }
  178. this.getDepart()
  179. this.visible = false
  180. },
  181. // 查询部门
  182. async getDepart () {
  183. const { code, data } = await getDepart()
  184. if (code === 0) {
  185. console.log(data)
  186. this.treeData = data
  187. this.transferTreeData()
  188. }
  189. },
  190. // 将树节点拍平, 便于使用slot自定义渲染树 , 同时把treeData全部加上slots
  191. transferTreeData () {
  192. const arr = []
  193. const recursionTreeData = (treeData) => {
  194. console.log(treeData, 1)
  195. treeData.forEach(item => {
  196. console.log(item, 'item')
  197. if (Array.isArray(item.children) && item.children.length > 0) {
  198. recursionTreeData(item.children)
  199. }
  200. item.slots = { title: item.id }
  201. const { label, id } = item
  202. arr.push({ title: label, id })
  203. })
  204. }
  205. recursionTreeData(this.treeData)
  206. this.flatTreeData = arr
  207. },
  208. handleOk () {},
  209. handclick (item) {
  210. this.editTreeNodeValue = item.title
  211. this.editTreeNodeId = item.id
  212. this.editTreeNodeVisabled = true
  213. },
  214. // 当前选中的树节点的key
  215. onSelect (selectedKeys, info) {
  216. console.log('selected', selectedKeys, info)
  217. this.currentClickNode = selectedKeys[0]
  218. },
  219. onCheck (checkedKeys, info) {
  220. console.log('onCheck', checkedKeys, info)
  221. },
  222. openModal () {
  223. this.visible = true
  224. this.departLabel = ''
  225. }
  226. }
  227. }
  228. </script>
  229. <style scoped lang="less">
  230. .department {
  231. display: flex;
  232. align-items: center;
  233. margin-top: 10px;
  234. }
  235. ::v-deep .ant-tree li .ant-tree-node-content-wrapper.ant-tree-node-selected {
  236. background-color: #fff;
  237. }
  238. </style>