index.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. export default {
  21. props: {
  22. // treeList私有化处理
  23. treeFormate: {
  24. type: Function,
  25. default: null
  26. },
  27. defaultProps: {
  28. type: Object,
  29. default: function () {
  30. return {
  31. children: 'children',
  32. value: 'id',
  33. label: 'name'
  34. };
  35. }
  36. },
  37. defaultExpandAll: {
  38. type: Boolean,
  39. default: function () {
  40. return false;
  41. }
  42. },
  43. // 初始请求treeList
  44. init: {
  45. type: Boolean,
  46. default: true
  47. },
  48. // 接口分父级id 和 类型筛选
  49. paramsType: {
  50. type: String,
  51. default: 'id'
  52. },
  53. type: {
  54. type: String,
  55. default: ''
  56. },
  57. id: {
  58. type: String,
  59. default: '0'
  60. },
  61. nodeKey: {
  62. type: String,
  63. default: 'id'
  64. }
  65. // appendRoot: {
  66. // type: Boolean,
  67. // default: false
  68. // },
  69. },
  70. data() {
  71. return {
  72. treeList: [],
  73. treeLoading: false,
  74. parentName: '',
  75. parentId: '',
  76. currentKey: ''
  77. };
  78. },
  79. mounted() {
  80. if (this.init) {
  81. this.getTreeData();
  82. }
  83. },
  84. methods: {
  85. getInstance() {
  86. return this.$refs.tree;
  87. },
  88. // 获取树结构数据
  89. async getTreeData() {
  90. try {
  91. this.treeLoading = true;
  92. const requestApi =
  93. this.paramsType === 'type'
  94. ? getTreeByPid.bind(null, this.type)
  95. : getTreeByPid.bind(null, this.id);
  96. const res = await requestApi();
  97. this.treeLoading = false;
  98. if (res?.code === '0') {
  99. this.treeList = res.data;
  100. this.$emit('setRootId', res.data[0].id);
  101. if (this.treeFormate) {
  102. this.treeList = this.treeFormate(this.treeList);
  103. }
  104. this.$nextTick(() => {
  105. // 默认高亮第一级树节点
  106. //defaultExpandAll父组件传过来为false 不展开所有树,就默认展开根节点的子节点
  107. if (!this.defaultExpandAll) {
  108. // 手动展开第一级树节点的子节点
  109. const rootNode = this.$refs.tree.getNode(this.treeList[0]);
  110. if (rootNode) {
  111. rootNode.expanded = true;
  112. }
  113. }
  114. if (this.treeList[0]) {
  115. this.setCurrentKey(this.treeList[0].id);
  116. this.handleNodeClick(
  117. this.treeList[0],
  118. this.$refs.tree.getCurrentNode()
  119. );
  120. }
  121. });
  122. return this.treeList;
  123. }
  124. } catch (error) {
  125. console.log(error);
  126. }
  127. this.treeLoading = false;
  128. },
  129. // 递归 - 往树children里面添加parentName
  130. // _setParentName (tree) {
  131. // let data = tree;
  132. // for (let i = 0; i < data.length; i++) {
  133. // if (data[i].parentId === '0') {
  134. // this.parentName = data[i].name;
  135. // originId = data[i].id;
  136. // originType = data[i].type;
  137. // }
  138. // data[i]['originId'] = originId;
  139. // data[i]['originType'] = originType;
  140. // data[i]['parentId'] = data[i]['parentId'];
  141. // if (data[i].children && data[i].children.length > 0) {
  142. // this._setParentName(data[i].children);
  143. // }
  144. // }
  145. // return data;
  146. // },
  147. handleNodeClick(data, node) {
  148. this.$emit('handleNodeClick', data, node);
  149. },
  150. // 设置默认高亮行
  151. setCurrentKey(id) {
  152. this.currentKey = id;
  153. this.$refs.tree.setCurrentKey(this.currentKey);
  154. },
  155. // 获取树的选中状态
  156. getSelectList() {
  157. const selectList = this.$refs.tree.getCurrentNode();
  158. return selectList;
  159. }
  160. }
  161. };
  162. </script>
  163. <style lang="scss" scoped>
  164. .tree-wrapper {
  165. width: 100%;
  166. height: 100%;
  167. overflow: auto;
  168. :deep(.el-tree) {
  169. display: inline-block;
  170. min-width: 100%;
  171. }
  172. }
  173. </style>