newIndex.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <template>
  2. <div class="tree-wrapper">
  3. <!-- <el-input placeholder="输入关键字进行过滤" v-model="filterText">
  4. </el-input> -->
  5. <el-tree
  6. :data="treeList"
  7. :props="defaultProps"
  8. v-loading="treeLoading"
  9. :node-key="nodeKey"
  10. ref="tree"
  11. :highlight-current="true"
  12. :expand-on-click-node="false"
  13. @node-click="handleNodeClick"
  14. v-bind="$attrs"
  15. :default-expand-all="defaultExpandAll"
  16. :filter-node-method="filterNode"
  17. >
  18. </el-tree>
  19. </div>
  20. </template>
  21. <script>
  22. import { getTreeByIds, getProduceTreeByPid } from '@/api/classifyManage';
  23. export default {
  24. props: {
  25. // treeList私有化处理
  26. treeFormate: {
  27. type: Function,
  28. default: null
  29. },
  30. defaultProps: {
  31. type: Object,
  32. default: function () {
  33. return {
  34. children: 'children',
  35. value: 'id',
  36. label: 'name'
  37. };
  38. }
  39. },
  40. defaultExpandAll: {
  41. type: Boolean,
  42. default: function () {
  43. return true;
  44. }
  45. },
  46. // 初始请求treeList
  47. init: {
  48. type: Boolean,
  49. default: true
  50. },
  51. treeIds: {
  52. type: Array,
  53. default: () => []
  54. },
  55. typeS: {
  56. type: Array,
  57. default: () => []
  58. },
  59. nodeKey: {
  60. type: String,
  61. default: 'id'
  62. }
  63. },
  64. watch: {
  65. // filterText(val) {
  66. // this.$refs.tree.filter(val);
  67. // }
  68. },
  69. data() {
  70. return {
  71. // filterText: '',
  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. filterNode(value, data) {
  86. if (!value) return true;
  87. return data.name.indexOf(value) !== -1;
  88. },
  89. getInstance() {
  90. return this.$refs.tree;
  91. },
  92. // 获取树结构数据
  93. async getTreeData() {
  94. try {
  95. this.treeLoading = true;
  96. const res =
  97. this.typeS.length > 0
  98. ? await getProduceTreeByPid(this.typeS.toString())
  99. : await getTreeByIds({ ids: this.treeIds.join(',') });
  100. this.treeLoading = false;
  101. if (res?.code === '0') {
  102. this.treeList = res.data;
  103. this.$emit('setRootId', res.data[0].id);
  104. if (this.treeFormate) {
  105. this.treeList = this.treeFormate(this.treeList);
  106. }
  107. this.$nextTick(() => {
  108. // 默认高亮第一级树节点
  109. if (this.treeList[0]) {
  110. this.setCurrentKey(this.treeList[0].id);
  111. this.handleNodeClick(
  112. this.treeList[0],
  113. this.$refs.tree.getCurrentNode()
  114. );
  115. }
  116. });
  117. return this.treeList;
  118. }
  119. } catch (error) {
  120. console.log(error);
  121. }
  122. this.treeLoading = false;
  123. },
  124. handleNodeClick(data, node) {
  125. this.$emit('handleNodeClick', data, node);
  126. },
  127. // 设置默认高亮行
  128. setCurrentKey(id) {
  129. this.currentKey = id;
  130. this.$refs.tree.setCurrentKey(this.currentKey);
  131. },
  132. // 获取树的选中状态
  133. getSelectList() {
  134. const selectList = this.$refs.tree.getCurrentNode();
  135. return selectList;
  136. }
  137. }
  138. };
  139. </script>
  140. <style lang="scss" scoped>
  141. .tree-wrapper {
  142. width: 100%;
  143. height: 100%;
  144. overflow: auto;
  145. :deep(.el-tree) {
  146. display: inline-block;
  147. min-width: 100%;
  148. }
  149. }
  150. </style>