index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <template>
  2. <div class="tree-wrapper">
  3. <el-tree
  4. :data="treeList"
  5. :props="defaultProps"
  6. v-loading="treeLoading"
  7. :node-key="nodeKey"
  8. ref="tree"
  9. :highlight-current="true"
  10. :expand-on-click-node="false"
  11. @node-click="handleNodeClick"
  12. v-bind="$attrs"
  13. :default-expand-all="defaultExpandAll"
  14. :default-expanded-keys="defaultExpandedKeys"
  15. >
  16. </el-tree>
  17. </div>
  18. </template>
  19. <script>
  20. import { getProduceTreeByCode, getTreeByPid } from '@/api/custom';
  21. import { getProjectAndCategoryLevelTree } from '@/api/project-manage/index.js';
  22. // let originId = '';
  23. // let originType = '';
  24. export default {
  25. props: {
  26. // treeList私有化处理
  27. treeFormate: {
  28. type: Function,
  29. default: null
  30. },
  31. defaultProps: {
  32. type: Object,
  33. default: function () {
  34. return {
  35. children: 'children',
  36. value: 'id',
  37. label: 'name'
  38. };
  39. }
  40. },
  41. defaultExpandAll: {
  42. type: Boolean,
  43. default: function () {
  44. return true;
  45. }
  46. },
  47. defaultExpandedKeys: {
  48. type: Array,
  49. default: () => {
  50. return [];
  51. }
  52. },
  53. // 初始请求treeList
  54. init: {
  55. type: Boolean,
  56. default: true
  57. },
  58. isFirstRefreshTable: {
  59. type: Boolean,
  60. default: true
  61. },
  62. id: {
  63. type: String,
  64. default: '0'
  65. },
  66. code: {
  67. type: String,
  68. default: ''
  69. },
  70. nodeKey: {
  71. type: String,
  72. default: 'id'
  73. },
  74. isProjects: {
  75. type: Boolean,
  76. default: false
  77. }
  78. // appendRoot: {
  79. // type: Boolean,
  80. // default: false
  81. // },
  82. },
  83. data() {
  84. return {
  85. treeList: [],
  86. treeLoading: false,
  87. parentName: '',
  88. parentId: '',
  89. currentKey: ''
  90. };
  91. },
  92. async mounted() {
  93. if (this.init) {
  94. if (this.code) {
  95. let typeObj = await getProduceTreeByCode(this.code);
  96. await this.getTreeData(typeObj[0]?.id);
  97. } else {
  98. await this.getTreeData(this.id);
  99. }
  100. }
  101. },
  102. methods: {
  103. getInstance() {
  104. return this.$refs.tree;
  105. },
  106. // 获取树结构数据
  107. async getTreeData(id, defChecked) {
  108. try {
  109. this.treeLoading = true;
  110. const res = this.isProjects
  111. ? await getProjectAndCategoryLevelTree()
  112. : await getTreeByPid(id);
  113. this.treeLoading = false;
  114. if (res?.code === '0') {
  115. this.treeList = res.data;
  116. this.$emit('setRootId', res.data[0].id);
  117. if (this.treeFormate) {
  118. this.treeList = this.treeFormate(this.treeList);
  119. }
  120. this.$nextTick(() => {
  121. if(this.isProjects){
  122. this.setCurrentKey(defChecked);
  123. }
  124. // 默认高亮第一级树节点
  125. if (this.treeList[0] && this.isFirstRefreshTable) {
  126. this.setCurrentKey(this.treeList[0].id);
  127. this.handleNodeClick(
  128. this.treeList[0],
  129. this.$refs.tree.getCurrentNode()
  130. );
  131. }
  132. });
  133. return this.treeList;
  134. }
  135. } catch (error) {}
  136. this.treeLoading = false;
  137. },
  138. handleNodeClick(data, node) {
  139. this.$emit('handleNodeClick', data, node);
  140. },
  141. // 设置默认高亮行
  142. setCurrentKey(id) {
  143. this.currentKey = id;
  144. this.$refs.tree.setCurrentKey(this.currentKey);
  145. },
  146. // 获取树的选中状态
  147. getSelectList() {
  148. const selectList = this.$refs.tree.getCurrentNode();
  149. return selectList;
  150. }
  151. }
  152. };
  153. </script>
  154. <style lang="scss" scoped>
  155. .tree-wrapper {
  156. width: 100%;
  157. height: 100%;
  158. overflow: auto;
  159. :deep(.el-tree) {
  160. display: inline-block;
  161. min-width: 100%;
  162. }
  163. }
  164. </style>