index.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. if (this.treeFormate) {
  103. this.treeList = this.treeFormate(this.treeList);
  104. }
  105. this.$nextTick(() => {
  106. // 默认高亮第一级树节点
  107. if (this.treeList[0]) {
  108. this.setCurrentKey(this.treeList[0].id);
  109. this.handleNodeClick(
  110. this.treeList[0],
  111. this.$refs.tree.getCurrentNode()
  112. );
  113. }
  114. });
  115. return this.treeList;
  116. }
  117. } catch (error) {}
  118. this.treeLoading = false;
  119. },
  120. // 递归 - 往树children里面添加parentName
  121. // _setParentName (tree) {
  122. // let data = tree;
  123. // for (let i = 0; i < data.length; i++) {
  124. // if (data[i].parentId === '0') {
  125. // this.parentName = data[i].name;
  126. // originId = data[i].id;
  127. // originType = data[i].type;
  128. // }
  129. // data[i]['originId'] = originId;
  130. // data[i]['originType'] = originType;
  131. // data[i]['parentId'] = data[i]['parentId'];
  132. // if (data[i].children && data[i].children.length > 0) {
  133. // this._setParentName(data[i].children);
  134. // }
  135. // }
  136. // return data;
  137. // },
  138. handleNodeClick (data, node) {
  139. this.$emit('handleNodeClick', data, node);
  140. },
  141. // 设置默认高亮行
  142. setCurrentKey (id) {
  143. this.currentKey = id;
  144. this.$refs.tree.setCurrentKey(this.currentKey);
  145. },
  146. // 获取树的选中状态
  147. getSelectList () {
  148. const selectList = this.$refs.tree.getCurrentNode();
  149. return selectList;
  150. }
  151. }
  152. };
  153. </script>
  154. <style lang="scss" scoped>
  155. .tree-wrapper {
  156. width: 100%;
  157. height: 100%;
  158. overflow: auto;
  159. :deep(.el-tree) {
  160. display: inline-block;
  161. min-width: 100%;
  162. }
  163. }
  164. </style>