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