index.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 { getTreeByPid } from '@/api/classifyManage';
  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: 'id',
  35. label: 'name'
  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: Array,
  52. default: () => {
  53. return ['9'];
  54. }
  55. },
  56. nodeKey: {
  57. type: String,
  58. default: 'id'
  59. }
  60. // appendRoot: {
  61. // type: Boolean,
  62. // default: false
  63. // },
  64. },
  65. data() {
  66. return {
  67. treeList: [],
  68. treeLoading: false,
  69. parentName: '',
  70. parentId: '',
  71. currentKey: ''
  72. };
  73. },
  74. mounted() {
  75. if (this.init) {
  76. this.getTreeData();
  77. }
  78. },
  79. methods: {
  80. getInstance() {
  81. return this.$refs.tree;
  82. },
  83. // 获取树结构数据
  84. async getTreeData() {
  85. try {
  86. this.treeLoading = true;
  87. const res = await getTreeByPid(0);
  88. this.treeLoading = false;
  89. if (res?.code === '0') {
  90. this.treeList = res.data.filter(item=>this.id.includes(item.id));
  91. this.$emit('setRootId', res.data[0].id);
  92. if (this.treeFormate) {
  93. this.treeList = this.treeFormate(this.treeList);
  94. }
  95. this.$nextTick(() => {
  96. // 默认高亮第一级树节点
  97. if (this.treeList[0]) {
  98. this.setCurrentKey(this.treeList[0].id);
  99. this.handleNodeClick(
  100. this.treeList[0],
  101. this.$refs.tree.getCurrentNode()
  102. );
  103. }
  104. });
  105. return this.treeList;
  106. }
  107. } catch (error) {}
  108. this.treeLoading = false;
  109. },
  110. // 递归 - 往树children里面添加parentName
  111. // _setParentName (tree) {
  112. // let data = tree;
  113. // for (let i = 0; i < data.length; i++) {
  114. // if (data[i].parentId === '0') {
  115. // this.parentName = data[i].name;
  116. // originId = data[i].id;
  117. // originType = data[i].type;
  118. // }
  119. // data[i]['originId'] = originId;
  120. // data[i]['originType'] = originType;
  121. // data[i]['parentId'] = data[i]['parentId'];
  122. // if (data[i].children && data[i].children.length > 0) {
  123. // this._setParentName(data[i].children);
  124. // }
  125. // }
  126. // return data;
  127. // },
  128. handleNodeClick(data, node) {
  129. this.$emit('handleNodeClick', data, node);
  130. },
  131. // 设置默认高亮行
  132. setCurrentKey(id) {
  133. this.currentKey = id;
  134. this.$refs.tree.setCurrentKey(this.currentKey);
  135. },
  136. // 获取树的选中状态
  137. getSelectList() {
  138. const selectList = this.$refs.tree.getCurrentNode();
  139. return selectList;
  140. }
  141. }
  142. };
  143. </script>
  144. <style lang="scss" scoped>
  145. .tree-wrapper {
  146. width: 100%;
  147. height: 100%;
  148. overflow: auto;
  149. :deep(.el-tree) {
  150. display: inline-block;
  151. min-width: 100%;
  152. }
  153. }
  154. </style>