index2.vue 3.8 KB

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