LeftSidebar.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <div class="left-sidebar">
  3. <el-tabs v-model="localActiveTab" type="border-card" @tab-click="handleTabClick">
  4. <!-- 设备类型 -->
  5. <el-tab-pane label="设备类型" name="org">
  6. <el-tree
  7. :data="orgTreeData"
  8. :props="treeProps"
  9. node-key="id"
  10. highlight-current
  11. @node-click="handleNodeClick"
  12. style="height: calc(100vh - 180px)"
  13. default-expand-all
  14. ref="treeRef"
  15. >
  16. <template #default="{ node, data }">
  17. <span class="custom-tree-node">
  18. <i
  19. :class="[
  20. 'el-icon',
  21. data.nodeType === 'category' ? 'el-icon-folder' : 'el-icon-s-platform'
  22. ]"
  23. :style="{
  24. color: data.nodeType === 'category' ? '#E6A23C' : '#409EFF',
  25. marginRight: '5px'
  26. }"
  27. ></i>
  28. <span>{{ node.label }}</span>
  29. <span v-if="data.nodeType!='category'">{{ node.codeNumber?' '+node.codeNumber:'' }}</span>
  30. </span>
  31. </template>
  32. </el-tree>
  33. </el-tab-pane>
  34. <!-- 用能分区 -->
  35. <el-tab-pane label="用能分区" name="energy">
  36. <el-tree
  37. :data="treeData"
  38. :props="treeProps"
  39. node-key="id"
  40. default-expand-all
  41. ref="treeRef1"
  42. highlight-current
  43. @node-click="handleNodeClick"
  44. style="height: calc(100vh - 180px)"
  45. ></el-tree>
  46. </el-tab-pane>
  47. </el-tabs>
  48. </div>
  49. </template>
  50. <script>
  51. import { getTreeAndExampleByPid } from '@/api/classifyManage';
  52. import { basicAreaPageAPI } from '@/api/factoryModel';
  53. export default {
  54. name: 'LeftSidebar',
  55. props: {
  56. // 当前激活的标签页
  57. activeTab: {
  58. type: String,
  59. default: 'org'
  60. }
  61. },
  62. data() {
  63. return {
  64. treeProps: {
  65. children: 'children',
  66. label: 'name'
  67. },
  68. localActiveTab: this.activeTab,
  69. // 设备类型树数据
  70. orgTreeData: [],
  71. // 用能分区树数据
  72. treeData: []
  73. };
  74. },
  75. watch: {
  76. activeTab(val) {
  77. this.localActiveTab = val;
  78. },
  79. localActiveTab(val) {
  80. this.$emit('update:activeTab', val);
  81. }
  82. },
  83. created() {
  84. this.fetchData();
  85. },
  86. methods: {
  87. // 获取数据
  88. fetchData() {
  89. // 获取设备类型数据
  90. getTreeAndExampleByPid('29').then((res) => {
  91. this.orgTreeData = res.data || [];
  92. if (this.orgTreeData.length) {
  93. this.$nextTick(() => {
  94. this.$refs.treeRef && this.$refs.treeRef.setCurrentKey(this.orgTreeData[0].id);
  95. });
  96. // 通知父组件第一个节点数据
  97. this.$emit('first-node', this.orgTreeData[0], 'org');
  98. }
  99. });
  100. // 获取用能分区数据
  101. basicAreaPageAPI({
  102. pageNum: 1,
  103. size: 9999
  104. }).then((res) => {
  105. this.treeData = this.$util.toTreeData({
  106. data: res,
  107. idField: 'id',
  108. parentIdField: 'parentId'
  109. });
  110. });
  111. },
  112. handleTabClick() {
  113. this.$emit('tab-change', this.localActiveTab);
  114. // 切换标签页时默认选中第一个节点
  115. this.$nextTick(() => {
  116. if (this.localActiveTab === 'org' && this.orgTreeData.length) {
  117. this.$refs.treeRef && this.$refs.treeRef.setCurrentKey(this.orgTreeData[0].id);
  118. this.$emit('node-click', this.orgTreeData[0], 'org');
  119. } else if (this.localActiveTab === 'energy' && this.treeData.length) {
  120. this.$refs.treeRef1 && this.$refs.treeRef1.setCurrentKey(this.treeData[0].id);
  121. this.$emit('node-click', this.treeData[0], 'energy');
  122. }
  123. });
  124. },
  125. handleNodeClick(data) {
  126. this.$emit('node-click', data, this.localActiveTab);
  127. },
  128. // 对外暴露方法,用于设置当前选中节点
  129. setCurrentKey(key, tab) {
  130. if (tab === 'org') {
  131. this.$refs.treeRef && this.$refs.treeRef.setCurrentKey(key);
  132. } else {
  133. this.$refs.treeRef1 && this.$refs.treeRef1.setCurrentKey(key);
  134. }
  135. }
  136. }
  137. };
  138. </script>
  139. <style lang="scss" scoped>
  140. .left-sidebar {
  141. height: 100%;
  142. ::v-deep .el-tabs {
  143. height: 100%;
  144. display: flex;
  145. flex-direction: column;
  146. .el-tabs__content {
  147. flex: 1;
  148. overflow: auto;
  149. }
  150. }
  151. .custom-tree-node {
  152. display: flex;
  153. align-items: center;
  154. font-size: 14px;
  155. }
  156. }
  157. </style>