organTree.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <template>
  2. <div class="organ-tree">
  3. <el-input v-model="filterText" placeholder="请输入关键字" suffix-icon="el-icon-search"></el-input>
  4. <div class="tree-style-box no-scrollbar">
  5. <el-tree
  6. class="tree-style"
  7. :data="organList"
  8. ref="tree"
  9. node-key="id"
  10. :highlight-current="true"
  11. :props="defaultProps"
  12. :expand-on-click-node="false"
  13. @node-click="treeClick"
  14. default-expand-all
  15. :filter-node-method="filterNode"
  16. >
  17. </el-tree>
  18. </div>
  19. </div>
  20. </template>
  21. <script>
  22. export default {
  23. name: 'organTree',
  24. props: ['buildingType'],
  25. data() {
  26. return {
  27. filterText: '',
  28. organList: [],
  29. defaultProps: {
  30. children: 'children',
  31. label: 'name'
  32. }
  33. };
  34. },
  35. watch: {
  36. filterText(val) {
  37. this.$refs.tree.filter(val);
  38. }
  39. },
  40. methods: {
  41. getOrgTreeList() {
  42. this.$http
  43. .get('/sc-community/assets/tree/community/find', { buildingType: this.buildingType })
  44. .then(({ status, data, msg }) => {
  45. if (status === 0 && data) {
  46. this.dimension(data);
  47. this.organList = data;
  48. this.$nextTick().then(() => {
  49. const firstNode = document.querySelector('.el-tree-node');
  50. firstNode.click();
  51. });
  52. }
  53. });
  54. },
  55. dimension(arr) {
  56. arr.map((item, index) => {
  57. if (!!item.children) {
  58. this.dimension(item.children);
  59. if (item.type == 'unit') {
  60. item.name = this.CheckChinese(item.name, '单元');
  61. } else if (item.type == 'building') {
  62. item.name = this.CheckChinese(item.name, '楼栋');
  63. }
  64. }
  65. });
  66. },
  67. filterNode(value, data) {
  68. if (!value) return true;
  69. return data.name.indexOf(value) !== -1;
  70. },
  71. treeClick(e) {
  72. let unitPa = {
  73. id: e.id,
  74. communityName: '',
  75. communityId: '',
  76. buildingName: '',
  77. buildingId: '',
  78. unitName: '',
  79. unitId: '',
  80. houseName: '',
  81. houseId: '',
  82. type: e.type
  83. };
  84. let newValueIds = e.id.split('-');
  85. let thisObj = this.$refs.tree.getNode(e);
  86. if (e.type == 'building') {
  87. unitPa.communityName = thisObj.parent.data.name;
  88. unitPa.communityId = thisObj.parent.data.value;
  89. unitPa.buildingName = e.name;
  90. unitPa.buildingId = e.value;
  91. } else if (e.type == 'unit') {
  92. unitPa.communityName = thisObj.parent.parent.data.name;
  93. unitPa.communityId = thisObj.parent.parent.data.value;
  94. unitPa.buildingName = thisObj.parent.data.name;
  95. unitPa.buildingId = thisObj.parent.data.value;
  96. unitPa.unitName = e.name;
  97. unitPa.unitId = e.value;
  98. } else if (e.type == 'room') {
  99. unitPa.communityName = newValueIds.length == 4 ? thisObj.parent.parent.parent.data.name : thisObj.parent.parent.data.name;
  100. unitPa.communityId = newValueIds.length == 4 ? thisObj.parent.parent.parent.data.value : thisObj.parent.parent.data.value;
  101. unitPa.buildingName = newValueIds.length == 4 ? thisObj.parent.parent.data.name : thisObj.parent.data.name;
  102. unitPa.buildingId = newValueIds.length == 4 ? thisObj.parent.parent.data.value : thisObj.parent.data.value;
  103. unitPa.unitName = newValueIds.length == 4 ? thisObj.parent.data.name : '';
  104. unitPa.unitId = newValueIds.length == 4 ? thisObj.parent.data.value : '';
  105. unitPa.houseName = e.name;
  106. unitPa.houseId = e.value;
  107. } else {
  108. unitPa.communityName = e.name;
  109. unitPa.communityId = e.value;
  110. }
  111. this.$emit('organId', unitPa);
  112. },
  113. CheckChinese(val, name) {
  114. var reg = new RegExp('[\\u4E00-\\u9FFF]+', 'g');
  115. let newVal = val;
  116. if (!reg.test(val)) {
  117. newVal = val + name;
  118. }
  119. return newVal;
  120. }
  121. },
  122. created() {
  123. this.getOrgTreeList();
  124. }
  125. };
  126. </script>
  127. <style lang="scss" scoped>
  128. .organ-tree {
  129. width: 260px;
  130. background: #ffffff;
  131. padding: 20px;
  132. box-sizing: border-box;
  133. float: left;
  134. height: 100%;
  135. overflow: auto;
  136. &::before {
  137. clear: both;
  138. }
  139. .tree-style-box {
  140. margin-top: 20px;
  141. max-height: calc(100vh - 200px);
  142. overflow: scroll;
  143. }
  144. }
  145. </style>