levelTree.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <div class="hui-left-tree">
  3. <div class="hui-left-tree-title">
  4. <span class="hui-left-tree-sub">资产列表</span>
  5. </div>
  6. <div class="hui-left-tree-content">
  7. <el-tree :data="treeData" :props="defaultProps">
  8. <div :class="nowData.id === data.id ? 'custom-tree-node active':'custom-tree-node'"
  9. @click.stop="filterDevice(data)" slot-scope="{ node, data }">
  10. <i :class="'iconfont ' + (data.deviceId ? 'huifont-shebeiguanli' : 'huifont-xiangmuguanli')">
  11. </i>
  12. <div class="label">{{node.label}}</div>
  13. </div>
  14. </el-tree>
  15. </div>
  16. </div>
  17. </template>
  18. <script>
  19. import {
  20. initDevicePartList,
  21. getDevicePartList
  22. } from '@/api/property'
  23. export default {
  24. props: ['type'],
  25. data() {
  26. return {
  27. treeData: [],
  28. nowData: {},
  29. defaultProps: {
  30. children: 'children',
  31. label: 'name'
  32. }
  33. }
  34. },
  35. mounted() {
  36. this.initDeviceList();
  37. },
  38. methods: {
  39. initDeviceList() {
  40. getDevicePartList(this.$store.getters.organization.id, this.$store.getters.project.id).then(res => {
  41. if (res.state) {
  42. if (res.data.length === 0) {
  43. initDevicePartList(this.$store.getters.organization.id, this.$store.getters.project.id)
  44. .then(res => {
  45. if (res.state) this.initDeviceList();
  46. })
  47. } else {
  48. this.treeData = res.data;
  49. this.returnChildren(this.treeData, []);
  50. }
  51. }
  52. })
  53. },
  54. returnChildren(data, parentData) {
  55. if (this.type === 1) {
  56. data.forEach(item => {
  57. item['parentData'] = [...parentData, item.name];
  58. if (item.children && item.deviceList) {
  59. let obj = item.deviceList.map(res => {
  60. res['deviceId'] = res.id;
  61. res['id'] = -res.id;
  62. return res;
  63. })
  64. item.children = item.children.concat(obj);
  65. }
  66. if (item.children && item.children.length > 0) this.returnChildren(item.children,
  67. [...parentData, item.name]);
  68. });
  69. } else {
  70. data.forEach(item => {
  71. item['parentData'] = [...parentData, item.name];
  72. if (item.children && item.children.length > 0) this.returnChildren(item.children, [...
  73. parentData, item.name
  74. ]);
  75. });
  76. }
  77. },
  78. filterDevice(data) {
  79. let option = {};
  80. if (this.nowData.id === data.id) {
  81. this.nowData = {};
  82. } else {
  83. this.nowData = data;
  84. if (this.nowData.deviceId) {
  85. option['deviceId'] = this.nowData.deviceId;
  86. } else {
  87. option['deviceLevelIds'] = this.getStorageIds([data]);
  88. }
  89. }
  90. this.$emit('filterDevice', option, data.parentData);
  91. },
  92. getStorageIds(array) {
  93. //获取档案库id
  94. let ids = [];
  95. function getIds(array, ids) {
  96. array.forEach(item => {
  97. if (item.id) {
  98. ids.push(item.id);
  99. }
  100. if (item.children) {
  101. ids = getIds(item.children, ids);
  102. }
  103. })
  104. return ids;
  105. }
  106. if (Array.isArray(array)) {
  107. if (array.length == 0) {
  108. return [];
  109. }
  110. } else {
  111. return [];
  112. }
  113. ids = getIds(array, ids);
  114. return ids;
  115. },
  116. },
  117. }
  118. </script>
  119. <style>
  120. </style>