km-knowledge-draftstree.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <template>
  2. <a-card :class="[$style.treewrap, { [$style.collapse]: fold }]">
  3. <a-spin v-if="icontype === 'left'" :spinning="spinning" :class="$style.spin" />
  4. <a-empty v-if="empty" />
  5. <a-tree
  6. v-if="treeData.length > 0"
  7. show-icon
  8. :expanded-keys="expandedKeys"
  9. :auto-expand-parent="autoExpandParent"
  10. :tree-data="treeData"
  11. :replace-fields="replacefields"
  12. :default-selected-keys="defaultselectedkey"
  13. @select="onSelect"
  14. @expand="onExpand"
  15. >
  16. <template slot="title" slot-scope="{ text, props }">
  17. <span :title="text">{{ text }}</span
  18. ><span v-if="props.count > 0">({{ props.count }})</span>
  19. </template>
  20. </a-tree>
  21. <a-button type="primary" :class="$style.fold" @click="foldClick">
  22. <a-icon :type="icontype" />
  23. </a-button>
  24. </a-card>
  25. </template>
  26. <script>
  27. import KmKnowledageService from '../km-knowledage-service'
  28. import components from './_import-components/km-knowledge-draftstree-import'
  29. // 给所有对象增加scopedSlots属性
  30. const addSlot = (obj) => {
  31. for (let i = 0; i < obj.length; i++) {
  32. Object.assign(obj[i], { scopedSlots: { title: 'title' } })
  33. // 因叶子节点和非叶子节点id可能有重复,所以在这里处理一下
  34. if (obj[i].id !== '0' && obj[i].id !== 'other' && obj[i].id !== 'oaDocReceiptExchange') {
  35. if (obj[i].leaf) {
  36. obj[i].id = '0' + obj[i].id
  37. } else {
  38. obj[i].id = '1' + obj[i].id
  39. }
  40. }
  41. if (obj[i].children) {
  42. addSlot(obj[i].children)
  43. }
  44. }
  45. }
  46. export default {
  47. name: 'KmKnowledgeDraftstree',
  48. components,
  49. props: {
  50. dataType: {
  51. type: String,
  52. required: true,
  53. },
  54. },
  55. data() {
  56. return {
  57. spinning: true,
  58. empty: false,
  59. icontype: 'left',
  60. fold: false,
  61. treeData: [],
  62. expandedKeys: [], // 默认展开的节点
  63. autoExpandParent: true,
  64. replacefields: {
  65. // 转换树的默认字段title和key
  66. title: 'text',
  67. key: 'id',
  68. },
  69. defaultselectedkey: [], // 默认选中的节点
  70. selectedkey: [], // 选中节点
  71. selectNode: null,
  72. }
  73. },
  74. watch: {
  75. dataType() {
  76. this.getTreeData()
  77. },
  78. },
  79. mounted() {
  80. this.getTreeData()
  81. },
  82. methods: {
  83. // 获取工作树数据
  84. getTreeData() {
  85. this.treeData = []
  86. this.spinning = true
  87. KmKnowledageService.getFrameWorktree(this.dataType).then((res) => {
  88. this.spinning = false
  89. const newArr = res.data
  90. addSlot(newArr)
  91. if (newArr[0].children.length > 0) {
  92. this.treeData = newArr
  93. this.empty = false
  94. } else {
  95. this.treeData = []
  96. this.empty = true
  97. }
  98. this.expandedKeys = [newArr[0].id] // 默认展开顶结点
  99. this.defaultselectedkey = [newArr[0].id] // 默认选中节点
  100. this.selectNode = newArr[0]
  101. this.$emit('treeSelectd', res.data[0], this.dataType)
  102. this.$emit('treeSelectValue', res.data[0], this.dataType)
  103. })
  104. },
  105. // 刷新树,选中原来的节点,不发送选中节点
  106. refresh() {
  107. this.treeData = []
  108. this.spinning = true
  109. KmKnowledageService.getFrameWorktree(this.dataType).then((res) => {
  110. this.spinning = false
  111. const newArr = res.data
  112. addSlot(newArr)
  113. if (newArr[0].children.length > 0) {
  114. this.treeData = newArr
  115. this.selectNode = newArr[0]
  116. this.empty = false
  117. } else {
  118. this.treeData = []
  119. this.selectNode = null
  120. this.empty = true
  121. }
  122. this.expandedKeys = [newArr[0].id] // 默认展开顶结点
  123. this.defaultselectedkey = this.selectedkey
  124. })
  125. },
  126. // 展开/收起节点时触发
  127. onExpand(expandedKeys) {
  128. this.expandedKeys = expandedKeys
  129. this.autoExpandParent = false
  130. },
  131. // 树形控件选中时通知父组件,父组件根据选中的业务类型筛选数据
  132. onSelect(selectedKeys, info) {
  133. this.selectedkey = selectedKeys
  134. this.selectNode = info.node.dataRef
  135. this.$emit('treeSelectd', info, this.dataType)
  136. this.$emit('treeSelectValue', info, this.dataType)
  137. },
  138. // 小箭头点击事件
  139. foldClick() {
  140. this.fold = !this.fold
  141. if (this.fold) {
  142. this.icontype = 'right'
  143. } else {
  144. this.icontype = 'left'
  145. }
  146. },
  147. },
  148. }
  149. </script>
  150. <style module lang="scss">
  151. @use '@/common/design' as *;
  152. .spin {
  153. width: 100%;
  154. line-height: 30;
  155. }
  156. .treewrap {
  157. position: relative;
  158. display: flex;
  159. flex-direction: column;
  160. width: 20%;
  161. min-height: 100%;
  162. margin-right: $padding-lg;
  163. transition: width 0.2s;
  164. .fold {
  165. position: absolute;
  166. top: calc(50% - 30px);
  167. right: -15px;
  168. z-index: 2;
  169. width: 15px;
  170. height: 75px;
  171. padding: 0;
  172. border-radius: 0 10px 10px 0;
  173. }
  174. :global(.ant-tree) {
  175. overflow: hidden;
  176. text-overflow: ellipsis;
  177. white-space: nowrap;
  178. }
  179. :global(.ant-card-body) {
  180. background: $white;
  181. }
  182. }
  183. .collapse {
  184. width: 0;
  185. :global(.ant-card-body) {
  186. background: transparent;
  187. :global(.ant-empty) {
  188. display: none;
  189. }
  190. }
  191. }
  192. </style>