assetTree.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. :default-expanded-keys="current && current.id ? [current.id] : []"
  13. >
  14. </el-tree>
  15. </div>
  16. </template>
  17. <script>
  18. import { treeByPid } from '@/api/produce/workOrder';
  19. export default {
  20. props: {
  21. defaultProps: {
  22. type: Object,
  23. default: function () {
  24. return {
  25. children: 'children',
  26. value: 'id',
  27. label: 'name'
  28. };
  29. }
  30. },
  31. // 初始请求treeList
  32. init: {
  33. type: Boolean,
  34. default: true
  35. },
  36. treeIds: {
  37. type: String,
  38. default: ''
  39. },
  40. nodeKey: {
  41. type: String,
  42. default: 'id'
  43. }
  44. },
  45. data() {
  46. return {
  47. treeList: [],
  48. treeLoading: false,
  49. current: {},
  50. currentKey: ''
  51. };
  52. },
  53. mounted() {
  54. if (this.init) {
  55. this.getTreeData();
  56. }
  57. },
  58. methods: {
  59. // 获取树结构数据
  60. async getTreeData() {
  61. try {
  62. this.treeLoading = true;
  63. const res = await treeByPid({ ids: this.treeIds });
  64. this.treeLoading = false;
  65. if (res?.code === '0') {
  66. this.treeList = res.data;
  67. this.$emit('setRootId', res.data[0]);
  68. this.$nextTick(() => {
  69. // 默认高亮第一级树节点
  70. if (this.treeList[0]) {
  71. this.setCurrentKey(this.treeList[0]);
  72. this.current = this.treeList[0];
  73. }
  74. });
  75. return this.treeList;
  76. }
  77. } catch (error) {
  78. console.log(error);
  79. }
  80. this.treeLoading = false;
  81. },
  82. handleNodeClick(data, node) {
  83. this.$emit('handleNodeClick', data, node);
  84. },
  85. // 设置默认高亮行
  86. setCurrentKey(id) {
  87. this.currentKey = id;
  88. this.$refs.tree.setCurrentKey(this.currentKey);
  89. },
  90. // 获取树的选中状态
  91. getSelectList() {
  92. const selectList = this.$refs.tree.getCurrentNode();
  93. return selectList;
  94. }
  95. }
  96. };
  97. </script>
  98. <style lang="scss" scoped>
  99. .tree-wrapper {
  100. width: 100%;
  101. height: 100%;
  102. overflow: auto;
  103. :deep(.el-tree) {
  104. display: inline-block;
  105. min-width: 100%;
  106. }
  107. }
  108. </style>