leftTree.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. >
  15. </el-tree>
  16. </div>
  17. </template>
  18. <script>
  19. import { getBusinessTypes } from '@/api/indicator';
  20. // let originId = '';
  21. // let originType = '';
  22. export default {
  23. props: {
  24. // treeList私有化处理
  25. treeFormate: {
  26. type: Function,
  27. default: null
  28. },
  29. defaultProps: {
  30. type: Object,
  31. default: function () {
  32. return {
  33. children: 'children',
  34. value: 'businessType',
  35. label: 'businessName'
  36. };
  37. }
  38. },
  39. defaultExpandAll: {
  40. type: Boolean,
  41. default: function () {
  42. return true;
  43. }
  44. },
  45. // 初始请求treeList
  46. init: {
  47. type: Boolean,
  48. default: true
  49. },
  50. id: {
  51. type: String,
  52. default: '0'
  53. },
  54. nodeKey: {
  55. type: String,
  56. default: 'id'
  57. },
  58. ids: {
  59. type: String,
  60. default: ''
  61. }
  62. // appendRoot: {
  63. // type: Boolean,
  64. // default: false
  65. // },
  66. },
  67. data() {
  68. return {
  69. treeList: [],
  70. treeLoading: false,
  71. parentName: '',
  72. parentId: ''
  73. };
  74. },
  75. mounted() {
  76. if (this.init) {
  77. this.getTreeData();
  78. }
  79. },
  80. methods: {
  81. getInstance() {
  82. return this.$refs.tree;
  83. },
  84. // 获取树结构数据
  85. async getTreeData() {
  86. try {
  87. this.treeLoading = true;
  88. const res = await getBusinessTypes();
  89. let data = [
  90. {
  91. id: 1,
  92. businessName: '全部',
  93. businessType: null,
  94. children: res
  95. }
  96. ];
  97. console.log(data, 'data');
  98. this.treeList = data;
  99. this.$nextTick(() => {
  100. // 默认高亮第一级树节点
  101. if (this.treeList[0]) {
  102. this.setCurrentKey(this.treeList[0].id);
  103. }
  104. });
  105. } catch (error) {
  106. this.treeLoading = false;
  107. }
  108. this.treeLoading = false;
  109. },
  110. handleNodeClick(data, type) {
  111. this.$emit('handleNodeClick', data, type);
  112. },
  113. // 设置默认高亮行
  114. setCurrentKey(id) {
  115. this.$refs.tree?.setCurrentKey(id);
  116. },
  117. // 获取树的选中状态
  118. getSelectList() {
  119. const selectList = this.$refs.tree.getCurrentNode();
  120. return selectList;
  121. }
  122. }
  123. };
  124. </script>
  125. <style lang="scss" scoped>
  126. .tree-wrapper {
  127. width: 100%;
  128. height: 100%;
  129. overflow: auto;
  130. :deep(.el-tree) {
  131. display: inline-block;
  132. min-width: 100%;
  133. }
  134. }
  135. </style>